1 package org.example.customer.utility;
2
3 /*
4 * This is free and unencumbered software released into the public domain.
5 * Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software,
6 * either in source code form or as a compiled binary, for any purpose, commercial or
7 * non-commercial, and by any means.
8 *
9 * In jurisdictions that recognize copyright laws, the author or authors of this
10 * software dedicate any and all copyright interest in the software to the public domain.
11 * We make this dedication for the benefit of the public at large and to the detriment of
12 * our heirs and successors. We intend this dedication to be an overt act of relinquishment in
13 * perpetuity of all present and future rights to this software under copyright law.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES
18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * For more information, please refer to: https://unlicense.org/
22 */
23
24 import java.io.Serializable;
25
26 import org.apache.commons.lang3.builder.EqualsBuilder;
27 import org.apache.commons.lang3.builder.HashCodeBuilder;
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30
31 /**
32 * The Customer Entity for the Customer application.
33 <br>
34 <br>
35 * This class represents the common Id field in the Customer Database
36 *
37 * @author Jonathan Earl
38 * @version 1.0
39 *
40 */
41 public class CustomerEntity
42 implements Serializable
43 {
44 private static final long serialVersionUID = 1L;
45
46 private static final Logger LOG = LogManager.getLogger();
47
48 private int myId;
49
50 /**
51 * The default constructor for the CustomerEntity class.
52 * <p>
53 * The initial values are:
54 * <ul>
55 * <li>id: 1</li>
56 * </ul>
57 *
58 */
59 public CustomerEntity()
60 {
61 LOG.debug("Starting the default Constructor");
62 final int initialId = 1;
63
64 setId(initialId);
65 }
66
67 /**
68 * Returns the id value for the Customer.
69 *
70 * @return the id value for the customer
71 */
72 public int getId()
73 {
74 LOG.debug("returning the Id: " + myId);
75 return myId;
76 }
77
78 /**
79 * Sets the id value for the Customer.
80 * <p>
81 * The business rules are:
82 * <ul>
83 * <li>the id must be 1 or greater</li>
84 * </ul>
85 *
86 * @param id the value to set into the customer id field
87 * @throws IllegalArgumentException if the id is invalid
88 */
89 public void setId(final int id)
90 {
91 LOG.debug("setting the Id: " + id);
92 final int min = 1;
93
94 if (id < min)
95 {
96 LOG.error("Id must be greater then zero");
97 throw new IllegalArgumentException("Id must be greater then zero");
98 }
99 this.myId = id;
100 }
101
102 /**
103 * The hashCode() method of the CustomerEntity class.
104 * <p>
105 * <strong>This method uses:</strong>
106 * <ul>
107 * <li>id</li>
108 * </ul>
109 *
110 * @see java.lang.Object#hashCode()
111 * @return the hashCode value for this CustomerEntity object
112 */
113 @Override
114 public int hashCode()
115 {
116 LOG.debug("building HashCode");
117 return new HashCodeBuilder()
118 .append(getId())
119 .toHashCode();
120 }
121
122 /**
123 * The equals() method of the CustomerEntity class.
124 * <p>
125 * <strong>This method uses:</strong>
126 * <ul>
127 * <li>id</li>
128 * </ul>
129 *
130 * @see java.lang.Object#equals(Object obj)
131 * @param obj the incoming object to compare against
132 * @return true if the fields being compared are equal
133 */
134 @Override
135 public boolean equals(final Object obj)
136 {
137 LOG.debug("checking equals");
138 if (obj instanceof CustomerEntity)
139 {
140 final CustomerEntity other = (CustomerEntity) obj;
141 return new EqualsBuilder()
142 .append(getId(), other.getId())
143 .isEquals();
144 }
145 else
146 {
147 return false;
148 }
149 }
150
151 /**
152 * The toString method for the CustomerEntity class.
153 *
154 * this method will return:<br>
155 * CustomerEntity [Id=xxx]
156 */
157 @Override
158 public String toString()
159 {
160 return "CustomerEntity [Id=" + getId() + "]";
161 }
162 }