View Javadoc
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  
25  import java.io.Serializable;
26  
27  import org.apache.commons.lang3.builder.EqualsBuilder;
28  import org.apache.commons.lang3.builder.HashCodeBuilder;
29  import org.apache.logging.log4j.LogManager;
30  import org.apache.logging.log4j.Logger;
31  import org.example.websecurity.XssSanitizer;
32  import org.example.websecurity.XssSanitizerImpl;
33  
34  /**
35   * The Customer Phone for the Customer application.
36   <br>
37   <br>
38   * This class represents the common Phone/Fax field in the Customer Database
39   *
40   * @author Jonathan Earl
41   * @version 1.0
42   *
43   */
44  public class Phone implements Serializable
45  {
46      private static final long serialVersionUID = 1L;
47  
48      private static final Logger LOG = LogManager.getLogger();
49  
50      private String myNumber;
51  
52      private XssSanitizer mySanitizer;
53  
54      /**
55       * The default constructor for the Phone class.
56       * <p>
57       * The initial values are:
58       * <ul>
59       * <li>number: null</li>
60       * </ul>
61       */
62      public Phone()
63      {
64          this(new XssSanitizerImpl());
65          LOG.debug("Finishing the default Constructor");
66      }
67  
68      /**
69       * The overloaded constructor for the Phone class that takes an XssSanitizer
70       * as input.
71       * <p>
72       * The initial values are:
73       * <ul>
74       *   <li>number: null</li>
75       * </ul>
76       * 
77       * @param sanitizer the XssSanitizer used by this instance
78       */
79      public Phone(final XssSanitizer sanitizer)
80      {
81          LOG.debug("Starting the overloaded Constructor");
82          final String initialPhone = null;
83  
84          mySanitizer = sanitizer;
85  
86          setNumber(initialPhone);
87      }
88  
89      /**
90       * Returns the number value for the Phone.
91       * 
92       * @return the number value for the Phone
93       */
94      public String getNumber()
95      {
96          LOG.debug("returning the Number: " + myNumber);
97          return myNumber;
98      }
99  
100     /**
101      * Sets the number value for the Phone.
102      * <p>
103      * The business rules are:
104      * <ul>
105      * <li>the number <strong>may</strong> be null</li>
106      * <li>the number must <strong>not</strong> be empty</li>
107      * <li>the number must min length of 2 chars</li>
108      * <li>the number must max length of 20 chars</li>
109      * <li>XSS strings within the number will be removed</li>
110      * </ul>
111      * 
112      * @param number the value to set into the number field
113      * @throws IllegalArgumentException if the number is invalid
114      */
115     public void setNumber(final String number)
116     {
117         LOG.debug("setting the Number");
118         final int max = 30;
119         final int min = 2;
120 
121         if (number == null)
122         {
123             LOG.debug("Number is set to null");
124             this.myNumber = null;
125             return;
126         }
127 
128         String safeNumber = mySanitizer.sanitizeInput(number);
129         if (safeNumber.isEmpty())
130         {
131             LOG.error("Number must not be empty");
132             throw new IllegalArgumentException("Number must not be empty");
133         }
134         if (safeNumber.length() > max || safeNumber.length() < min)
135         {
136             LOG.error("Number must be between 2 and 40 chars in length");
137             throw new IllegalArgumentException("Number must be between 2 and 20 chars in length");
138         }
139         LOG.debug("setting the Phone to: " + safeNumber);
140         this.myNumber = safeNumber;
141     }
142     
143     /**
144      * The hashCode() method of the Phone class.
145      * <p>
146      * <strong>This method uses:</strong>
147      * <ul>
148      *  <li>number</li>
149      * </ul>
150      * 
151      * @see java.lang.Object#hashCode()
152      * @return the hashCode value for this Phone object
153      */
154     @Override
155     public int hashCode()
156     {
157         LOG.debug("building HashCode");
158         return new HashCodeBuilder()
159                 .append(getNumber())
160                 .toHashCode();
161     }
162 
163     /**
164      * The equals() method of the Phone class.
165      * <p>
166      * <strong>This method uses:</strong>
167      * <ul>
168      *  <li>number</li>
169      * </ul>
170      * 
171      * @see java.lang.Object#equals(Object obj)
172      * @param obj the incoming object to compare against
173      * @return true if the fields being compared are equal
174      */
175     @Override
176     public boolean equals(final Object obj)
177     {
178         LOG.debug("checking equals");
179         if (obj instanceof Phone)
180         {
181             final Phone other = (Phone) obj;
182             return new EqualsBuilder()
183                     .append(getNumber(), other.getNumber())
184                     .isEquals();
185         }
186         else
187         {
188             return false;
189         }
190     }
191 
192     /**
193      * The toString method for the Phone class.
194      * 
195      * this method will return:<br>
196      * Phone [Number=XXX]
197      */
198     @Override
199     public String toString()
200     {
201         return "Phone [Number=" + myNumber + "]";
202     }
203 
204 }