1   /*
2    * Copyright 2006-2009 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sourceforge.domian.test.domain;
17  
18  
19  import java.util.Arrays;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.lang.builder.EqualsBuilder;
24  import org.apache.commons.lang.builder.HashCodeBuilder;
25  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
26  import static org.apache.commons.lang.math.RandomUtils.nextLong;
27  import org.apache.commons.lang.NotImplementedException;
28  
29  import net.sourceforge.domian.entity.Entity;
30  
31  
32  /** Entity with pure business-key equality... just to see what happens. */
33  public class OrderLine implements Entity {
34  
35      protected final String entityId = String.valueOf(nextLong()); // Not particularly safe...
36  
37      /** For possible use of RDBMS optimistic locking. */
38      protected Integer version;
39  
40      protected long orderLineId;
41      protected boolean paymentReceived = false;    // Deliberately testing autoboxing behaviour
42      protected Boolean processed = false;          // Deliberately testing autoboxing behaviour
43      protected Boolean cancelled = false;          // Deliberately testing autoboxing behaviour
44  
45      /* Needed by misc. reflection-based stuff... */
46      public OrderLine() {}
47  
48      public OrderLine(final Long id) {
49          this.orderLineId = id; // Autoboxed
50      }
51  
52      public long getOrderLineId() {
53          return orderLineId;
54      }
55  
56      public boolean isPaymentReceived() {
57          return paymentReceived;
58      }
59  
60      public OrderLine paymentReceived(final boolean paymentReceived) {
61          this.paymentReceived = paymentReceived;
62          return this;
63      }
64  
65      public Boolean isProcessed() {
66          return processed;
67      }
68  
69      public OrderLine processed(final boolean processed) {
70          this.processed = processed;
71          return this;
72      }
73  
74      public Boolean isCancelled() {
75          return cancelled;
76      }
77  
78      public OrderLine cancelled(final boolean cancelled) {
79          this.cancelled = cancelled;
80          return this;
81      }
82  
83      /* Deliberately testing autoboxing behaviour */
84      public Boolean pending() {
85          return !isPaymentReceived();
86      }
87  
88      /* Deliberately testing autoboxing behaviour */
89      public boolean isPending() {
90          return pending();
91      }
92  
93      /* Deliberately testing method accessability */
94      Boolean privateIsPending() {
95          return pending();
96      }
97  
98      @Override
99      public String getEntityId() {
100         return this.entityId;
101     }
102 
103     public Integer getVersion() {
104         throw new NotImplementedException();
105     }
106 
107     public void setVersion(Integer version) {
108         throw new NotImplementedException();
109     }
110 
111     public Boolean isEntity() {
112         return Boolean.TRUE;
113     }
114 
115     public List getKey() {
116         return Collections.unmodifiableList(Arrays.asList(orderLineId));
117     }
118 
119     public static List getKeyProperties() {
120         return Collections.unmodifiableList(Arrays.asList("orderLineId"));
121     }
122 
123     public Integer getInitialNonZeroOddNumber() {
124         return 2753;
125     }
126 
127     public Integer getMultiplierNonZeroOddNumber() {
128         return 7739;
129     }
130 
131     @Override
132     public int hashCode() {
133         final HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(getInitialNonZeroOddNumber(),
134                                                                     getMultiplierNonZeroOddNumber());
135         for (Object partialKey : getKey()) {
136             if (isEntity()) {
137                 if (partialKey != null) {
138                     hashCodeBuilder.append(partialKey);
139                 } else {
140                     return System.identityHashCode(this);
141                 }
142 
143             } else { // Value object
144                 if (partialKey != null) {
145                     hashCodeBuilder.append(partialKey);
146                 }
147             }
148         }
149         return hashCodeBuilder.toHashCode();
150     }
151 
152     @Override
153     public boolean equals(final Object otherObject) {
154         if (otherObject == null) {
155             return false;
156         }
157         if (!(otherObject instanceof OrderLine)) {
158             return false;
159         }
160         OrderLine otherOrderLine = (OrderLine) otherObject;
161         return new EqualsBuilder().append(this.orderLineId, otherOrderLine.orderLineId).isEquals();
162     }
163 
164     @Override
165     public String toString() {
166         return new ReflectionToStringBuilder(this).toString();
167     }
168 }