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  package org.org.usurper.model;
20  
21  /**
22   * The Class SpecificPropertyDefinition is an immutable value object describing a target specific property.
23   */
24  public class SpecificPropertyDefinition implements ITargetDefinition {
25  
26      public static final String CLASS_PROPERTY_SEPARATOR = ".";
27  
28      private final Class<?> targetClass;
29      private final String targetProperty;
30  
31      /**
32       * Instantiates a new SpecificPropertyDefinition from a Class object and a property name.
33       * 
34       * @param targetClass
35       *            the target class
36       * @param targetProperty
37       *            the target property
38       */
39      public SpecificPropertyDefinition(final Class<?> targetClass, final String targetProperty) {
40          super();
41          this.targetClass = targetClass;
42          this.targetProperty = targetProperty;
43      }
44  
45      /**
46       * Instantiates a new SpecificPropertyDefinition from a String description.<br>
47       * This String description is made of the class name and a dot ".".<br>
48       * example:<br>
49       * org.org.usurper.DummyVO.propertyToBeSet where propertyToBeSet is property of the class org.org.usurper.DummyVO.
50       * 
51       * @param targetPropertyDescription
52       *            the target property description
53       */
54      public SpecificPropertyDefinition(final String targetPropertyDescription) {
55          super();
56          String targetClassName = targetPropertyDescription.substring(0, targetPropertyDescription.lastIndexOf(CLASS_PROPERTY_SEPARATOR));
57          try {
58              this.targetClass = Class.forName(targetClassName);
59          } catch (ClassNotFoundException e) {
60              throw new IllegalArgumentException("Impossible to resolve class in: " + targetPropertyDescription, e);
61          }
62  
63          String targetPropertyName = targetPropertyDescription.substring(targetPropertyDescription.lastIndexOf(CLASS_PROPERTY_SEPARATOR) + 1, targetPropertyDescription.length());
64          this.targetProperty = targetPropertyName;
65      }
66  
67      /**
68       * Gets the target class.
69       * 
70       * @return the target class
71       */
72      public Class<?> getTargetClass() {
73          return targetClass;
74      }
75  
76      /**
77       * Gets the target property.
78       * 
79       * @return the target property
80       */
81      public String getTargetProperty() {
82          return targetProperty;
83      }
84  
85      /**
86       * Gets the property path string.
87       * 
88       * @return the property path string
89       */
90      public String getPropertyPathString() {
91          return buildPropertyPathString(targetClass, targetProperty);
92      }
93  
94      public static String buildPropertyPathString(Class<?> targetClass, String targetPropertyName) {
95          return targetClass.getName() + CLASS_PROPERTY_SEPARATOR + targetPropertyName;
96      }
97  
98      @Override
99      public String toString() {
100         return buildPropertyPathString(targetClass, targetProperty);
101     }
102 
103     @Override
104     public boolean equals(Object obj) {
105         if (this == obj)
106             return true;
107         if ((obj == null) || (obj.getClass() != this.getClass()))
108             return false;
109         // object must be Test at this point
110         SpecificPropertyDefinition other = (SpecificPropertyDefinition) obj;
111         return targetProperty.equals(other.targetProperty) && targetClass.equals(other.targetClass);
112     }
113 
114     @Override
115     public int hashCode() {
116         int hash = 7;
117         hash = 31 * hash + (null == targetClass ? 0 : targetClass.hashCode());
118         hash = 31 * hash + (null == targetProperty ? 0 : targetProperty.hashCode());
119         return hash;
120     }
121 }