1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17  package org.apache.commons.betwixt;
18  
19  import java.io.Serializable;
20  import java.math.BigDecimal;
21  import java.math.BigInteger;
22  import java.sql.Date;
23  import java.sql.Time;
24  import java.sql.Timestamp;
25  import java.util.ArrayList;
26  import java.util.Enumeration;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  
34  /** <p><code>CustomerBean</code> is a sample bean for use by the test cases.</p>
35    *
36    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
37    * @author <a href="mailto:michael.davey@coderage.org">Michael Davey</a>
38    * @version $Revision: 438373 $
39    */
40  public class CustomerBean implements Serializable {
41  
42      /** Logger */
43      private static final Log log = LogFactory.getLog( CustomerBean.class );
44      
45      private String id;
46      private String name;
47      private String nickName;
48      private String[] emails;
49      private int[] numbers;
50      private AddressBean address;
51      private Map projectMap;
52      private List locations = new ArrayList();
53  	private Date date;
54  	private Time time;
55  	private Timestamp timestamp;
56  	private BigDecimal bigDecimal;
57  	private BigInteger bigInteger;
58  	    
59      public CustomerBean() {
60      }
61  
62      public String getID() {
63          return id;
64      }
65      
66      public String getNickName() {
67         return nickName;
68      }
69  
70      
71      public String getName() {
72          return name;
73      }
74      
75      public String[] getEmails() {
76          return emails;
77      }
78  
79      public int[] getNumbers() {
80          return numbers;
81      }
82  
83      public AddressBean getAddress() {
84          return address;
85      }
86  
87      public Map getProjectMap() {
88          return projectMap;
89      }
90      
91      public Iterator getProjectNames() {
92          if ( projectMap == null ) {
93              return null;
94          }
95          return projectMap.keySet().iterator();
96      }
97      
98      public Enumeration getProjectURLs() {
99          if ( projectMap == null ) {
100             return null;
101         }
102         return new IteratorEnumeration( projectMap.values().iterator() );
103     }
104     
105     public List getLocations() {
106         return locations;
107     }
108     
109     /** An indexed property */
110     public String getLocation(int index) {
111         return (String) locations.get(index);
112     }
113     
114     public void setID(String id) {
115         this.id = id;
116     }
117     
118     public void setName(String name) {
119         this.name = name;
120     }
121  
122     public void setNickName(String nickName) {
123         this.nickName = nickName;
124     }    
125     
126     public void setEmails(String[] emails) {
127         this.emails = emails;
128     }
129     
130     public void addEmail(String email) {
131         int newLength = (emails == null) ? 1 : emails.length+1;
132         String[] newArray = new String[newLength];
133         for (int i=0; i< newLength-1; i++) {
134             newArray[i] = emails[i];
135         }
136         newArray[newLength-1] = email;
137         emails = newArray;
138     }    
139     
140     public void setNumbers(int[] numbers) {
141         this.numbers = numbers;
142     }
143 
144     public void addNumber(int number) {
145         if ( log.isDebugEnabled() ) {
146             log.debug( "Adding number: " + number );
147         }
148         
149         int newLength = (numbers == null) ? 1 : numbers.length+1;
150         int[] newArray = new int[newLength];
151         for (int i=0; i< newLength-1; i++) {
152             newArray[i] = numbers[i];
153         }
154         newArray[newLength-1] = number;
155         numbers = newArray;
156     }
157     
158     public void setAddress(AddressBean address) {
159         this.address = address;
160         
161         if ( log.isDebugEnabled() ) {
162             log.debug( "Setting the address to be: " + address );
163         }
164     }
165 
166     public void setProjectMap(Map projectMap) {
167         this.projectMap = projectMap;
168     }
169     
170     public void addLocation(String location) {
171         locations.add(location);
172     }
173     
174     /** An indexed property */
175     public void setLocation(int index, String location) {
176         if ( index == locations.size() ) {
177             locations.add( location );
178         }
179         else {
180             locations.set(index, location);
181         }
182     }
183 
184     public String toString() {
185         return "[" + this.getClass().getName() + ": ID=" + id + ", name=" + name
186                 + ", address=" + address + "]";
187     }
188     
189     public boolean equals( Object obj ) {
190         if ( obj == null ) return false;
191         return this.hashCode() == obj.hashCode();
192     }
193     
194     public int hashCode() {
195         return toString().hashCode();
196     }
197 	/**
198 	 * Returns the date.
199 	 * @return Date
200 	 */
201 	public Date getDate() {
202 		return date;
203 	}
204 
205 	/**
206 	 * Returns the time.
207 	 * @return Time
208 	 */
209 	public Time getTime() {
210 		return time;
211 	}
212 
213 	/**
214 	 * Returns the timestamp.
215 	 * @return Timestamp
216 	 */
217 	public Timestamp getTimestamp() {
218 		return timestamp;
219 	}
220 
221 	/**
222 	 * Sets the date.
223 	 * @param date The date to set
224 	 */
225 	public void setDate(Date date) {
226 		this.date = date;
227 	}
228 
229 	/**
230 	 * Sets the time.
231 	 * @param time The time to set
232 	 */
233 	public void setTime(Time time) {
234 		this.time = time;
235 	}
236 
237 	/**
238 	 * Sets the timestamp.
239 	 * @param timestamp The timestamp to set
240 	 */
241 	public void setTimestamp(Timestamp timestamp) {
242 		this.timestamp = timestamp;
243 	}
244 
245 	/**
246 	 * Returns the bigDecimal.
247 	 * @return BigDecimal
248 	 */
249 	public BigDecimal getBigDecimal() {
250 		return bigDecimal;
251 	}
252 
253 	/**
254 	 * Returns the bigInteger.
255 	 * @return BigInteger
256 	 */
257 	public BigInteger getBigInteger() {
258 		return bigInteger;
259 	}
260 
261 	/**
262 	 * Sets the bigDecimal.
263 	 * @param bigDecimal The bigDecimal to set
264 	 */
265 	public void setBigDecimal(BigDecimal bigDecimal) {
266 		this.bigDecimal = bigDecimal;
267 	}
268 
269 	/**
270 	 * Sets the bigInteger.
271 	 * @param bigInteger The bigInteger to set
272 	 */
273 	public void setBigInteger(BigInteger bigInteger) {
274 		this.bigInteger = bigInteger;
275 	}
276 
277     /** 
278      * Adapter to make an {@link Iterator Iterator} instance appear to be
279      * an {@link Enumeration Enumeration} instance.
280      * Originate in commons collections
281      * 
282      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
283      */
284     private static final class IteratorEnumeration implements Enumeration {
285         
286         /** The iterator being decorated. */
287         private Iterator iterator;
288         
289         /**
290          * Constructs a new <code>IteratorEnumeration</code> that will not 
291          * function until {@link #setIterator(Iterator) setIterator} is  
292          * invoked.
293          */
294         public IteratorEnumeration() {
295             super();
296         }
297 
298         /**
299          * Constructs a new <code>IteratorEnumeration</code> that will use
300          * the given iterator. 
301          * 
302          * @param iterator  the iterator to use
303          */
304         public IteratorEnumeration( Iterator iterator ) {
305             super();
306             this.iterator = iterator;
307         }
308 
309         // Iterator interface
310         //-------------------------------------------------------------------------
311 
312         /**
313          *  Returns true if the underlying iterator has more elements.
314          *
315          *  @return true if the underlying iterator has more elements
316          */
317         public boolean hasMoreElements() {
318             return iterator.hasNext();
319         }
320 
321         /**
322          *  Returns the next element from the underlying iterator.
323          *
324          *  @return the next element from the underlying iterator.
325          *  @throws java.util.NoSuchElementException  if the underlying iterator has no
326          *    more elements
327          */
328         public Object nextElement() {
329             return iterator.next();
330         }
331 
332         // Properties
333         //-------------------------------------------------------------------------
334 
335         /**
336          *  Returns the underlying iterator.
337          * 
338          *  @return the underlying iterator
339          */
340         public Iterator getIterator() {
341             return iterator;
342         }
343 
344         /**
345          *  Sets the underlying iterator.
346          *
347          *  @param iterator  the new underlying iterator
348          */
349         public void setIterator( Iterator iterator ) {
350             this.iterator = iterator;
351         }
352         
353     }
354 
355 
356 }