View Javadoc

1   /*
2    ORG Usurper is a random value object generator library 
3    Copyright (C) 2007  Pierre-Antoine Grégoire
4    
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9    
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  
20  /**
21   * 
22   */
23  package org.org.usurper.handlers.additional;
24  
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import org.org.usurper.handlers.basic.AbstractSpecificPropertyHandler;
29  import org.org.usurper.handlers.exceptions.SpecificPropertyHandlingException;
30  import org.org.usurper.model.HandledBeanProperty;
31  import org.org.usurper.model.HandledConstructorArg;
32  import org.org.usurper.model.SpecificPropertyDefinition;
33  
34  /**
35   * This handler will give a specific property a value amongst a list of values.<br>
36   * This value will be picked in a parameterized list of values.<br>
37   * The handler will iterate the list until every value is attributed and will then start again from the beginning.
38   * 
39   * @author pagregoire
40   */
41  public class ListOfValuesSpecificPropertyHandler extends AbstractSpecificPropertyHandler {
42  
43      private List<?> values;
44  
45    
46      private Iterator<?> valuesIterator;
47  
48  
49      /**
50       * @param specificPropertyDefinition
51       * @param values
52       */
53      public ListOfValuesSpecificPropertyHandler(SpecificPropertyDefinition specificPropertyDefinition, List<?> values) {
54          super(specificPropertyDefinition);
55          if (values == null || values.size() == 0) {
56              throw new SpecificPropertyHandlingException("The list of values should not be null nor empty!");
57          }
58          this.values = values;
59      }
60  
61      /**
62       * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledBeanProperty)
63       */
64      public Object handle(HandledBeanProperty handledBeanProperty) {
65          return doHandle();
66      }
67  
68      /**
69       * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledConstructorArg)
70       */
71      public Object handle(HandledConstructorArg handledBeanProperty) {
72          return doHandle();
73      }
74  
75      /**
76       * @return
77       */
78      private Object doHandle() {
79          Object result = null;
80          if (this.valuesIterator == null) {
81              this.valuesIterator = this.values.iterator();
82          }
83          if (!this.valuesIterator.hasNext()) {
84              this.valuesIterator = null;
85              this.valuesIterator = this.values.iterator();
86          }
87          result = valuesIterator.next();
88          return result;
89      }
90  
91  }