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  package org.org.usurper.handlers.basic;
21  
22  import java.util.Random;
23  
24  import org.org.usurper.handlers.IHandler;
25  import org.org.usurper.model.HandledBeanProperty;
26  import org.org.usurper.model.HandledConstructorArg;
27  
28  /**
29   * This type returns a random value of a given enum type.
30   * 
31   * @author pagregoire
32   */
33  public final class EnumHandler implements IHandler {
34  
35      /**
36       * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledBeanProperty)
37       */
38      public Object handle(HandledBeanProperty handledBeanProperty) {
39          return doHandle(handledBeanProperty.getPropertyClass());
40      }
41  
42      /**
43       * @see org.org.usurper.handlers.IHandler#handle(org.org.usurper.model.HandledConstructorArg)
44       */
45      public Object handle(HandledConstructorArg handledConstructorArg) {
46          return doHandle(handledConstructorArg.getConstructorArgClass());
47      }
48  
49      /**
50       * Does the handling for a given class.
51       * 
52       * @param entityClass
53       *            the entity class
54       * 
55       * @return the object
56       */
57      private Object doHandle(Class<?> entityClass) {
58          Object[] enumValues = entityClass.getEnumConstants();
59          int random = new Random().nextInt(enumValues.length);
60          return enumValues[random];
61      }
62  
63  }