From: <iro...@us...> - 2009-09-25 22:17:31
|
Revision: 162 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=162&view=rev Author: iroberts Date: 2009-09-25 22:17:23 +0000 (Fri, 25 Sep 2009) Log Message: ----------- strip off carriage returns Modified Paths: -------------- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/PropertyElement.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/AutoProperty.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/DefaultPojomaticPolicy.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojoFormat.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojomaticPolicy.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/Property.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PropertyFormat.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/AbstractPropertyElement.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyAccessor.java trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyField.java Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,158 +1,158 @@ -package org.pojomatic; - -import org.pojomatic.diff.DifferenceFromNull; -import org.pojomatic.diff.Differences; -import org.pojomatic.diff.NoDifferences; -import org.pojomatic.internal.PojomatorImpl; -import org.pojomatic.internal.SelfPopulatingMap; - -/** - * Static methods for implementing the {@link java.lang.Object#equals(Object)}, - * {@link java.lang.Object#hashCode()} and {@link java.lang.Object#toString()} methods on a - * annotated POJO. The actual work for a given class is done by a {@link Pojomator} created for - * that class. This class is careful to create only a single {@code Pojomator} per POJO class. - * The overhead for looking up the {@code Pojomator} by POJO class is light, so a typical use in a - * POJO class would be - * <p style="background-color:#EEEEFF; margin: 1em"> - * <code> - * <font color="#646464">@Override</font> <font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>int </b></font><font color="#000000">hashCode() {</font><br> - * <font color="#7f0055"><b>return </b></font><font color="#000000">Pojomatic.hashCode(</font><font color="#7f0055"><b>this</b></font><font color="#000000">);</font><br /> - * <font color="#000000">}</font><br> - * <br/> - * <font color="#646464">@Override</font> <font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>boolean </b></font><font color="#000000">equals(Object other) {</font><br> - * <font color="#7f0055"><b>return </b></font><font color="#000000">Pojomatic.equals(</font><font color="#7f0055"><b>this</b></font><font color="#000000">, other);</font><br /> - * <font color="#000000">}</font><br> - * <br/> - * <font color="#646464">@Override</font> <font color="#7f0055"><b>public </b></font><font color="#000000">String toString() {</font><br> - * <font color="#7f0055"><b>return </b></font><font color="#000000">Pojomatic.toString(</font><font color="#7f0055"><b>this</b></font><font color="#000000">);</font><br /> - * <font color="#000000">}</font><br> - * <br/> - * </code> - * </p> - * Under the covers, these methods are referencing a {@link org.pojomatic.Pojomator Pojomator} instance - * which is created lazily and cached on a per-class basis. The performance penalty for this is - * negligible, but if profiling suggests that it is a bottleneck, one can do this by hand: - * <p style="background-color:#EEEEFF; margin: 1em"> - * <code> - * <font color="#ffffff"> </font><font color="#7f0055"><b>private final static </b></font><font color="#000000">Pojomator<Manual> POJOMATOR = Pojomatic.pojomator</font><font color="#000000">(</font><font color="#000000">Manual.</font><font color="#7f0055"><b>class</b></font><font color="#000000">)</font><font color="#000000">;</font><br /> - * <font color="#ffffff"></font><br /> - * <font color="#ffffff"> </font><font color="#646464">@Override </font><font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>boolean </b></font><font color="#000000">equals</font><font color="#000000">(</font><font color="#000000">Object other</font><font color="#000000">) {</font><br /> - * <font color="#ffffff"> </font><font color="#7f0055"><b>return </b></font><font color="#000000">POJOMATOR.doEquals</font><font color="#000000">(</font><font color="#000000">this, other</font><font color="#000000">)</font><font color="#000000">;</font><br /> - * <font color="#ffffff"> </font><font color="#000000">}</font><br /> - * <font color="#ffffff"></font><br /> - * <font color="#ffffff"> </font><font color="#646464">@Override </font><font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>int </b></font><font color="#000000">hashCode</font><font color="#000000">() {</font><br /> - * <font color="#ffffff"> </font><font color="#7f0055"><b>return </b></font><font color="#000000">POJOMATOR.doHashCode</font><font color="#000000">(</font><font color="#7f0055"><b>this</b></font><font color="#000000">)</font><font color="#000000">;</font><br /> - * <font color="#ffffff"> </font><font color="#000000">}</font><br /> - * <font color="#ffffff"></font><br /> - * <font color="#ffffff"> </font><font color="#646464">@Override </font><font color="#7f0055"><b>public </b></font><font color="#000000">String toString</font><font color="#000000">() {</font><br /> - * <font color="#ffffff"> </font><font color="#7f0055"><b>return </b></font><font color="#000000">POJOMATOR.doToString</font><font color="#000000">(</font><font color="#7f0055"><b>this</b></font><font color="#000000">)</font><font color="#000000">;</font><br /> - * <font color="#ffffff"> </font><font color="#000000">}</font> - * </code> - * </p> - * - * @see Pojomator - */ -public class Pojomatic { - - private static SelfPopulatingMap<Class<?>, Pojomator<?>> POJOMATORS = - new SelfPopulatingMap<Class<?>, Pojomator<?>>() { - @Override - @SuppressWarnings("unchecked") - // compiler does not know that the type parameter to Pojomator is the same as the type - // parameter to Class - protected Pojomator<?> create(Class<?> key) { - return new PojomatorImpl(key); - } - }; - - private Pojomatic() {} - - /** - * Compute the {@code toString} representation for a POJO. - * @param <T> the type of the POJO - * @param pojo the POJO - must not be null - * @return the {@code toString} representation of {@code pojo}. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic - * @see Pojomator#doToString(Object) - */ - public static <T> String toString(T pojo) throws IllegalArgumentException { - return pojomator(getClass(pojo)).doToString(pojo); - } - - /** - * Compute the {@code hashCode} for a POJO. - * @param <T> the type of the POJO - * @param pojo the POJO - must not be null - * @return the {@code hashCode} for {@code pojo}. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic - * @see Pojomator#doHashCode(Object) - */ - public static <T> int hashCode(T pojo) throws IllegalArgumentException { - return pojomator(getClass(pojo)).doHashCode(pojo); - } - - /** - * Compute whether {@code pojo} and {@code other} are equal to each other in the sense of - * {@code Object}'s {@code equals} method. - * @param <T> the type of the POJO - * @param pojo the POJO - must not be null - * @param other the object to compare to for equality - * @return whether {@code pojo} and {@code other} are equal to each other in the sense of - * {@code Object}'s {@code equals} method. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic - * @see Pojomator#doEquals(Object, Object) - */ - public static <T> boolean equals(T pojo, Object other) throws IllegalArgumentException { - return pojomator(getClass(pojo)).doEquals(pojo, other); - } - - - /** - * Compute the differences between {@code pojo} and {@code other} among the properties - * examined by {@link #equals(Object, Object)} for type {@code T}. - * - * @param <T> the static type of the first object to compare - * @param <S> the static type of the first object to compare - * @param pojo the instance to diff against - * @param other the instance to diff - * @return the list of differences (possibly empty) between {@code instance} and {@code other} - * among the properties examined by {@link #equals(Object, Object)} for type {@code T}. - * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use - * with Pojomatic - */ - public static <T, S extends T> Differences diff(T pojo, S other) - throws NullPointerException, IllegalArgumentException { - if (pojo == null) { - if (other != null) { - return new DifferenceFromNull(other); - } - else { //both null - return NoDifferences.getInstance(); - } - } - - return pojomator(getClass(pojo)).doDiff(pojo, other); - } - - /** - * Get the {@code Pojomator} for {@code pojoClass}. The same instance will be returned every time - * for a given value of {@code pojoClass}. - * @param <T> the type represented by {@code pojoClass} - * @param pojoClass the class to create a {@code Pojomator} for. - * @return a {@code Pojomator<T>} - * @throws IllegalArgumentException if {@code pojoClass} has no properties annotated for use - * with Pojomatic - */ - @SuppressWarnings("unchecked") // compiler does not know that the type parameter to Pojomator is T - public static <T> Pojomator<T> pojomator(Class<T> pojoClass) throws IllegalArgumentException { - return (Pojomator<T>) POJOMATORS.get(pojoClass); - } - - @SuppressWarnings("unchecked") // Since Object.getClass returns Class<?> - private static <T> Class<T> getClass(T pojo) { - return (Class<T>) pojo.getClass(); - } -} +package org.pojomatic; + +import org.pojomatic.diff.DifferenceFromNull; +import org.pojomatic.diff.Differences; +import org.pojomatic.diff.NoDifferences; +import org.pojomatic.internal.PojomatorImpl; +import org.pojomatic.internal.SelfPopulatingMap; + +/** + * Static methods for implementing the {@link java.lang.Object#equals(Object)}, + * {@link java.lang.Object#hashCode()} and {@link java.lang.Object#toString()} methods on a + * annotated POJO. The actual work for a given class is done by a {@link Pojomator} created for + * that class. This class is careful to create only a single {@code Pojomator} per POJO class. + * The overhead for looking up the {@code Pojomator} by POJO class is light, so a typical use in a + * POJO class would be + * <p style="background-color:#EEEEFF; margin: 1em"> + * <code> + * <font color="#646464">@Override</font> <font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>int </b></font><font color="#000000">hashCode() {</font><br> + * <font color="#7f0055"><b>return </b></font><font color="#000000">Pojomatic.hashCode(</font><font color="#7f0055"><b>this</b></font><font color="#000000">);</font><br /> + * <font color="#000000">}</font><br> + * <br/> + * <font color="#646464">@Override</font> <font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>boolean </b></font><font color="#000000">equals(Object other) {</font><br> + * <font color="#7f0055"><b>return </b></font><font color="#000000">Pojomatic.equals(</font><font color="#7f0055"><b>this</b></font><font color="#000000">, other);</font><br /> + * <font color="#000000">}</font><br> + * <br/> + * <font color="#646464">@Override</font> <font color="#7f0055"><b>public </b></font><font color="#000000">String toString() {</font><br> + * <font color="#7f0055"><b>return </b></font><font color="#000000">Pojomatic.toString(</font><font color="#7f0055"><b>this</b></font><font color="#000000">);</font><br /> + * <font color="#000000">}</font><br> + * <br/> + * </code> + * </p> + * Under the covers, these methods are referencing a {@link org.pojomatic.Pojomator Pojomator} instance + * which is created lazily and cached on a per-class basis. The performance penalty for this is + * negligible, but if profiling suggests that it is a bottleneck, one can do this by hand: + * <p style="background-color:#EEEEFF; margin: 1em"> + * <code> + * <font color="#ffffff"> </font><font color="#7f0055"><b>private final static </b></font><font color="#000000">Pojomator<Manual> POJOMATOR = Pojomatic.pojomator</font><font color="#000000">(</font><font color="#000000">Manual.</font><font color="#7f0055"><b>class</b></font><font color="#000000">)</font><font color="#000000">;</font><br /> + * <font color="#ffffff"></font><br /> + * <font color="#ffffff"> </font><font color="#646464">@Override </font><font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>boolean </b></font><font color="#000000">equals</font><font color="#000000">(</font><font color="#000000">Object other</font><font color="#000000">) {</font><br /> + * <font color="#ffffff"> </font><font color="#7f0055"><b>return </b></font><font color="#000000">POJOMATOR.doEquals</font><font color="#000000">(</font><font color="#000000">this, other</font><font color="#000000">)</font><font color="#000000">;</font><br /> + * <font color="#ffffff"> </font><font color="#000000">}</font><br /> + * <font color="#ffffff"></font><br /> + * <font color="#ffffff"> </font><font color="#646464">@Override </font><font color="#7f0055"><b>public </b></font><font color="#7f0055"><b>int </b></font><font color="#000000">hashCode</font><font color="#000000">() {</font><br /> + * <font color="#ffffff"> </font><font color="#7f0055"><b>return </b></font><font color="#000000">POJOMATOR.doHashCode</font><font color="#000000">(</font><font color="#7f0055"><b>this</b></font><font color="#000000">)</font><font color="#000000">;</font><br /> + * <font color="#ffffff"> </font><font color="#000000">}</font><br /> + * <font color="#ffffff"></font><br /> + * <font color="#ffffff"> </font><font color="#646464">@Override </font><font color="#7f0055"><b>public </b></font><font color="#000000">String toString</font><font color="#000000">() {</font><br /> + * <font color="#ffffff"> </font><font color="#7f0055"><b>return </b></font><font color="#000000">POJOMATOR.doToString</font><font color="#000000">(</font><font color="#7f0055"><b>this</b></font><font color="#000000">)</font><font color="#000000">;</font><br /> + * <font color="#ffffff"> </font><font color="#000000">}</font> + * </code> + * </p> + * + * @see Pojomator + */ +public class Pojomatic { + + private static SelfPopulatingMap<Class<?>, Pojomator<?>> POJOMATORS = + new SelfPopulatingMap<Class<?>, Pojomator<?>>() { + @Override + @SuppressWarnings("unchecked") + // compiler does not know that the type parameter to Pojomator is the same as the type + // parameter to Class + protected Pojomator<?> create(Class<?> key) { + return new PojomatorImpl(key); + } + }; + + private Pojomatic() {} + + /** + * Compute the {@code toString} representation for a POJO. + * @param <T> the type of the POJO + * @param pojo the POJO - must not be null + * @return the {@code toString} representation of {@code pojo}. + * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use + * with Pojomatic + * @see Pojomator#doToString(Object) + */ + public static <T> String toString(T pojo) throws IllegalArgumentException { + return pojomator(getClass(pojo)).doToString(pojo); + } + + /** + * Compute the {@code hashCode} for a POJO. + * @param <T> the type of the POJO + * @param pojo the POJO - must not be null + * @return the {@code hashCode} for {@code pojo}. + * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use + * with Pojomatic + * @see Pojomator#doHashCode(Object) + */ + public static <T> int hashCode(T pojo) throws IllegalArgumentException { + return pojomator(getClass(pojo)).doHashCode(pojo); + } + + /** + * Compute whether {@code pojo} and {@code other} are equal to each other in the sense of + * {@code Object}'s {@code equals} method. + * @param <T> the type of the POJO + * @param pojo the POJO - must not be null + * @param other the object to compare to for equality + * @return whether {@code pojo} and {@code other} are equal to each other in the sense of + * {@code Object}'s {@code equals} method. + * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use + * with Pojomatic + * @see Pojomator#doEquals(Object, Object) + */ + public static <T> boolean equals(T pojo, Object other) throws IllegalArgumentException { + return pojomator(getClass(pojo)).doEquals(pojo, other); + } + + + /** + * Compute the differences between {@code pojo} and {@code other} among the properties + * examined by {@link #equals(Object, Object)} for type {@code T}. + * + * @param <T> the static type of the first object to compare + * @param <S> the static type of the first object to compare + * @param pojo the instance to diff against + * @param other the instance to diff + * @return the list of differences (possibly empty) between {@code instance} and {@code other} + * among the properties examined by {@link #equals(Object, Object)} for type {@code T}. + * @throws IllegalArgumentException if {@code pojo}'s class has no properties annotated for use + * with Pojomatic + */ + public static <T, S extends T> Differences diff(T pojo, S other) + throws NullPointerException, IllegalArgumentException { + if (pojo == null) { + if (other != null) { + return new DifferenceFromNull(other); + } + else { //both null + return NoDifferences.getInstance(); + } + } + + return pojomator(getClass(pojo)).doDiff(pojo, other); + } + + /** + * Get the {@code Pojomator} for {@code pojoClass}. The same instance will be returned every time + * for a given value of {@code pojoClass}. + * @param <T> the type represented by {@code pojoClass} + * @param pojoClass the class to create a {@code Pojomator} for. + * @return a {@code Pojomator<T>} + * @throws IllegalArgumentException if {@code pojoClass} has no properties annotated for use + * with Pojomatic + */ + @SuppressWarnings("unchecked") // compiler does not know that the type parameter to Pojomator is T + public static <T> Pojomator<T> pojomator(Class<T> pojoClass) throws IllegalArgumentException { + return (Pojomator<T>) POJOMATORS.get(pojoClass); + } + + @SuppressWarnings("unchecked") // Since Object.getClass returns Class<?> + private static <T> Class<T> getClass(T pojo) { + return (Class<T>) pojo.getClass(); + } +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/PropertyElement.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/PropertyElement.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/PropertyElement.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,37 +1,37 @@ -package org.pojomatic; - -import java.lang.reflect.AnnotatedElement; - -/** - * A "property" on a class. In this context, all a property is is a means of obtaining a value from - * an instance. - */ -public interface PropertyElement { - - /** - * Get the name of this property. - * @return the name of this property. - */ - String getName(); - - /** - * Get the value held by this property from the given instance. - * - * @param instance the instance to get the value from - * @return the value held by the instance - */ - Object getValue(Object instance); - - /** - * Get the original annotated element that this property is derived from. - * @return the original annotated element that this property is derived from. - */ - AnnotatedElement getElement(); - - /** - * Get the class object representing the class or interface declaring this property. - * @return the declaring class or interface of this property. - */ - Class<?> getDeclaringClass(); - -} +package org.pojomatic; + +import java.lang.reflect.AnnotatedElement; + +/** + * A "property" on a class. In this context, all a property is is a means of obtaining a value from + * an instance. + */ +public interface PropertyElement { + + /** + * Get the name of this property. + * @return the name of this property. + */ + String getName(); + + /** + * Get the value held by this property from the given instance. + * + * @param instance the instance to get the value from + * @return the value held by the instance + */ + Object getValue(Object instance); + + /** + * Get the original annotated element that this property is derived from. + * @return the original annotated element that this property is derived from. + */ + AnnotatedElement getElement(); + + /** + * Get the class object representing the class or interface declaring this property. + * @return the declaring class or interface of this property. + */ + Class<?> getDeclaringClass(); + +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/AutoProperty.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/AutoProperty.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/AutoProperty.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,30 +1,30 @@ -package org.pojomatic.annotations; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import org.pojomatic.Pojomatic; - -/** - * Assigns the defaults for {@link Pojomatic} at the class level and provides a way to - * configure the automatic detection of properties. - * Note that this can is overridden (case by case) by the {@link Property}} annotation. - */ -@Target(TYPE) -@Retention(RUNTIME) -@Documented -public @interface AutoProperty { - /** - * Include properties in everything by default (unless otherwise stated by {@link Property}). - */ - public DefaultPojomaticPolicy policy() default DefaultPojomaticPolicy.ALL; - - /** - * Specifies whether to auto-detect properties by their fields, getters or not at all. - */ - public AutoDetectPolicy autoDetect() default AutoDetectPolicy.FIELD; -} +package org.pojomatic.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.pojomatic.Pojomatic; + +/** + * Assigns the defaults for {@link Pojomatic} at the class level and provides a way to + * configure the automatic detection of properties. + * Note that this can is overridden (case by case) by the {@link Property}} annotation. + */ +@Target(TYPE) +@Retention(RUNTIME) +@Documented +public @interface AutoProperty { + /** + * Include properties in everything by default (unless otherwise stated by {@link Property}). + */ + public DefaultPojomaticPolicy policy() default DefaultPojomaticPolicy.ALL; + + /** + * Specifies whether to auto-detect properties by their fields, getters or not at all. + */ + public AutoDetectPolicy autoDetect() default AutoDetectPolicy.FIELD; +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/DefaultPojomaticPolicy.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/DefaultPojomaticPolicy.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/DefaultPojomaticPolicy.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,78 +1,78 @@ -package org.pojomatic.annotations; - -import java.util.Collections; -import java.util.EnumSet; -import java.util.Set; - -import org.pojomatic.Pojomatic; -import org.pojomatic.internal.PropertyRole; - -/** - * A policy for defining which sets of {@link Pojomatic} operations - * ({@code equals}, {@code hashCode} and {@code toString}) should use all properties by default. - * This is set class-wide using {@link AutoProperty}. - * @see PojomaticPolicy - */ -public enum DefaultPojomaticPolicy { - - /** - * Use all properties for both {@code hashCode} and {@code equals} by default. - * Anything included in {@code public int hashCode()} should also be included in - * {@code public boolean equals(Object)} to preserve the general - * contract of {@link Object#hashCode()}. - * - * @see Object#hashCode() - * @see Object#equals(Object) - */ - HASHCODE_EQUALS(PropertyRole.HASH_CODE, PropertyRole.EQUALS), - - /** - * Use all properties for {@code equals} only by default. - * - * @see Object#equals(Object) - */ - EQUALS(PropertyRole.EQUALS), - - /** - * Use all properties for both {@code equals} and {@code toString} by default. - * - * @see Object#equals(Object) - * @see Object#toString() - */ - EQUALS_TO_STRING(PropertyRole.EQUALS, PropertyRole.TO_STRING), - - /** - * Use all properties for both {@code toString} only by default. - * - * @see Object#toString() - */ - TO_STRING(PropertyRole.TO_STRING), - - /** - * Use all properties for {@code hashCode}, {@code equals} and {@code toString} by default. - */ - ALL(PropertyRole.EQUALS, PropertyRole.HASH_CODE, PropertyRole.TO_STRING), - - /** - * Do not use any properties for any of {@code hashCode}, {@code equals} or {@code toString} - * by default. - */ - NONE(); - - /** - * @return the roles this specified by this policy. - */ - public Set<PropertyRole> getRoles() { - return roles; - } - - private DefaultPojomaticPolicy(PropertyRole... roles) { - Set<PropertyRole> roleSet = EnumSet.noneOf(PropertyRole.class); - for (PropertyRole role: roles) { - roleSet.add(role); - } - this.roles = Collections.unmodifiableSet(roleSet); - } - - private final Set<PropertyRole> roles; -} +package org.pojomatic.annotations; + +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + +import org.pojomatic.Pojomatic; +import org.pojomatic.internal.PropertyRole; + +/** + * A policy for defining which sets of {@link Pojomatic} operations + * ({@code equals}, {@code hashCode} and {@code toString}) should use all properties by default. + * This is set class-wide using {@link AutoProperty}. + * @see PojomaticPolicy + */ +public enum DefaultPojomaticPolicy { + + /** + * Use all properties for both {@code hashCode} and {@code equals} by default. + * Anything included in {@code public int hashCode()} should also be included in + * {@code public boolean equals(Object)} to preserve the general + * contract of {@link Object#hashCode()}. + * + * @see Object#hashCode() + * @see Object#equals(Object) + */ + HASHCODE_EQUALS(PropertyRole.HASH_CODE, PropertyRole.EQUALS), + + /** + * Use all properties for {@code equals} only by default. + * + * @see Object#equals(Object) + */ + EQUALS(PropertyRole.EQUALS), + + /** + * Use all properties for both {@code equals} and {@code toString} by default. + * + * @see Object#equals(Object) + * @see Object#toString() + */ + EQUALS_TO_STRING(PropertyRole.EQUALS, PropertyRole.TO_STRING), + + /** + * Use all properties for both {@code toString} only by default. + * + * @see Object#toString() + */ + TO_STRING(PropertyRole.TO_STRING), + + /** + * Use all properties for {@code hashCode}, {@code equals} and {@code toString} by default. + */ + ALL(PropertyRole.EQUALS, PropertyRole.HASH_CODE, PropertyRole.TO_STRING), + + /** + * Do not use any properties for any of {@code hashCode}, {@code equals} or {@code toString} + * by default. + */ + NONE(); + + /** + * @return the roles this specified by this policy. + */ + public Set<PropertyRole> getRoles() { + return roles; + } + + private DefaultPojomaticPolicy(PropertyRole... roles) { + Set<PropertyRole> roleSet = EnumSet.noneOf(PropertyRole.class); + for (PropertyRole role: roles) { + roleSet.add(role); + } + this.roles = Collections.unmodifiableSet(roleSet); + } + + private final Set<PropertyRole> roles; +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojoFormat.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojoFormat.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojoFormat.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,25 +1,25 @@ -package org.pojomatic.annotations; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import org.pojomatic.formatter.PojoFormatter; - -/** - * Specifies formatting information to be used for creating {@code String} representations of POJOs. - * @see PojoFormatter - */ -@Target(TYPE) -@Retention(RUNTIME) -@Documented -public @interface PojoFormat { - - /** - * The formatter to use for creating a {@code String} representation. - */ - public Class<? extends PojoFormatter> value(); -} +package org.pojomatic.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.pojomatic.formatter.PojoFormatter; + +/** + * Specifies formatting information to be used for creating {@code String} representations of POJOs. + * @see PojoFormatter + */ +@Target(TYPE) +@Retention(RUNTIME) +@Documented +public @interface PojoFormat { + + /** + * The formatter to use for creating a {@code String} representation. + */ + public Class<? extends PojoFormatter> value(); +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojomaticPolicy.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojomaticPolicy.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PojomaticPolicy.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,89 +1,89 @@ -package org.pojomatic.annotations; - -import java.util.Collections; -import java.util.EnumSet; -import java.util.Set; - -import org.pojomatic.Pojomatic; -import org.pojomatic.internal.PropertyRole; - -/** - * A policy for defining which sets of {@link Pojomatic} operations - * ({@code equals}, {@code hashCode} and {@code toString}) should use a property. - * This is set using {@link Property}. - * @see DefaultPojomaticPolicy - */ -public enum PojomaticPolicy { - - /** - * Use the property for both {@code hashCode} and {@code equals}. - * Anything included in {@code public int hashCode()} should also be included in - * {@code public boolean equals(Object)} to preserve the general - * contract of {@link Object#hashCode()}. - * - * @see Object#hashCode() - * @see Object#equals(Object) - */ - HASHCODE_EQUALS(PropertyRole.HASH_CODE, PropertyRole.EQUALS), - - /** - * Use the property for both {@code equals} and {@code toString}. - * - * @see Object#equals(Object) - * @see Object#toString() - */ - EQUALS_TO_STRING(PropertyRole.EQUALS, PropertyRole.TO_STRING), - - - /** - * Use the property for {@code equals} only. - * - * @see Object#equals(Object) - */ - EQUALS(PropertyRole.EQUALS), - - /** - * Use the property for both {@code toString} only. - * - * @see Object#toString() - */ - TO_STRING(PropertyRole.TO_STRING), - - /** - * Use the property for {@code hashCode}, {@code equals} and {@code toString}. - */ - ALL(PropertyRole.EQUALS, PropertyRole.HASH_CODE, PropertyRole.TO_STRING), - - /** - * Do not use the property for any of {@code hashCode}, {@code equals} or {@code toString}. - */ - NONE(), - - /** - * Use the default policy specified via the {@code @AutoProperty} annotation, or - * {@code ALL} if none was specified. - */ - DEFAULT { - @Override public Set<PropertyRole> getRoles() { - return null; - } - }; - - - /** - * @return the roles this specified by this policy. Will be {@code null} for {@code DEFAULT}. - */ - public Set<PropertyRole> getRoles() { - return roles; - } - - private PojomaticPolicy(PropertyRole... roles) { - Set<PropertyRole> roleSet = EnumSet.noneOf(PropertyRole.class); - for (PropertyRole role: roles) { - roleSet.add(role); - } - this.roles = Collections.unmodifiableSet(roleSet); - } - - private final Set<PropertyRole> roles; -} +package org.pojomatic.annotations; + +import java.util.Collections; +import java.util.EnumSet; +import java.util.Set; + +import org.pojomatic.Pojomatic; +import org.pojomatic.internal.PropertyRole; + +/** + * A policy for defining which sets of {@link Pojomatic} operations + * ({@code equals}, {@code hashCode} and {@code toString}) should use a property. + * This is set using {@link Property}. + * @see DefaultPojomaticPolicy + */ +public enum PojomaticPolicy { + + /** + * Use the property for both {@code hashCode} and {@code equals}. + * Anything included in {@code public int hashCode()} should also be included in + * {@code public boolean equals(Object)} to preserve the general + * contract of {@link Object#hashCode()}. + * + * @see Object#hashCode() + * @see Object#equals(Object) + */ + HASHCODE_EQUALS(PropertyRole.HASH_CODE, PropertyRole.EQUALS), + + /** + * Use the property for both {@code equals} and {@code toString}. + * + * @see Object#equals(Object) + * @see Object#toString() + */ + EQUALS_TO_STRING(PropertyRole.EQUALS, PropertyRole.TO_STRING), + + + /** + * Use the property for {@code equals} only. + * + * @see Object#equals(Object) + */ + EQUALS(PropertyRole.EQUALS), + + /** + * Use the property for both {@code toString} only. + * + * @see Object#toString() + */ + TO_STRING(PropertyRole.TO_STRING), + + /** + * Use the property for {@code hashCode}, {@code equals} and {@code toString}. + */ + ALL(PropertyRole.EQUALS, PropertyRole.HASH_CODE, PropertyRole.TO_STRING), + + /** + * Do not use the property for any of {@code hashCode}, {@code equals} or {@code toString}. + */ + NONE(), + + /** + * Use the default policy specified via the {@code @AutoProperty} annotation, or + * {@code ALL} if none was specified. + */ + DEFAULT { + @Override public Set<PropertyRole> getRoles() { + return null; + } + }; + + + /** + * @return the roles this specified by this policy. Will be {@code null} for {@code DEFAULT}. + */ + public Set<PropertyRole> getRoles() { + return roles; + } + + private PojomaticPolicy(PropertyRole... roles) { + Set<PropertyRole> roleSet = EnumSet.noneOf(PropertyRole.class); + for (PropertyRole role: roles) { + roleSet.add(role); + } + this.roles = Collections.unmodifiableSet(roleSet); + } + + private final Set<PropertyRole> roles; +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/Property.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/Property.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/Property.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,38 +1,38 @@ -package org.pojomatic.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import org.pojomatic.Pojomatic; - -/** - * Marks a property of a class to be used by {@link Pojomatic} - * @see PojomaticPolicy - */ -@Target({FIELD, METHOD}) -@Retention(RUNTIME) -@Documented -public @interface Property { - - /** - * Which sets of {@link Pojomatic} operations ({@code equals}, {@code hashCode} and - * {@code toString}) should use a property. - */ - public PojomaticPolicy policy() default PojomaticPolicy.DEFAULT; - - /** - * The name used to identify the property in the standard {@code toString} representation. If - * empty, the following algorithm is used to determine the name. For a propertiy referenced by - * field, the name of the field is used. For a property referenced by a method whose name is of - * the form {@code getSomeField}, the name {@code someField} will be used. For a boolean property - * referenced by a method whose name is of the form {@code isSomeField}, the name - * {@code someField} will be used. For any other property referenced by a method, the name of - * the method is used. - */ - public String name() default ""; -} +package org.pojomatic.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.pojomatic.Pojomatic; + +/** + * Marks a property of a class to be used by {@link Pojomatic} + * @see PojomaticPolicy + */ +@Target({FIELD, METHOD}) +@Retention(RUNTIME) +@Documented +public @interface Property { + + /** + * Which sets of {@link Pojomatic} operations ({@code equals}, {@code hashCode} and + * {@code toString}) should use a property. + */ + public PojomaticPolicy policy() default PojomaticPolicy.DEFAULT; + + /** + * The name used to identify the property in the standard {@code toString} representation. If + * empty, the following algorithm is used to determine the name. For a propertiy referenced by + * field, the name of the field is used. For a property referenced by a method whose name is of + * the form {@code getSomeField}, the name {@code someField} will be used. For a boolean property + * referenced by a method whose name is of the form {@code isSomeField}, the name + * {@code someField} will be used. For any other property referenced by a method, the name of + * the method is used. + */ + public String name() default ""; +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PropertyFormat.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PropertyFormat.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/annotations/PropertyFormat.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,29 +1,29 @@ -package org.pojomatic.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import org.pojomatic.formatter.PropertyFormatter; - -/** - * Specifies formatting information to be used for creating {@code String} representations of - * properties. Note that using a {@code PropertyFormat} annotation on a property does not influence - * whether that property will be included in the {@code toString} implementation. That is - * determined solely by any {@link Property} and {@link AutoProperty} annotations on the POJO. - * @see PropertyFormatter - */ -@Target({FIELD, METHOD}) -@Retention(RUNTIME) -@Documented -public @interface PropertyFormat { - - /** - * The formatter to use for creating a {@code String} representation. - */ - public Class<? extends PropertyFormatter> value(); -} +package org.pojomatic.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import org.pojomatic.formatter.PropertyFormatter; + +/** + * Specifies formatting information to be used for creating {@code String} representations of + * properties. Note that using a {@code PropertyFormat} annotation on a property does not influence + * whether that property will be included in the {@code toString} implementation. That is + * determined solely by any {@link Property} and {@link AutoProperty} annotations on the POJO. + * @see PropertyFormatter + */ +@Target({FIELD, METHOD}) +@Retention(RUNTIME) +@Documented +public @interface PropertyFormat { + + /** + * The formatter to use for creating a {@code String} representation. + */ + public Class<? extends PropertyFormatter> value(); +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/AbstractPropertyElement.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/AbstractPropertyElement.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/AbstractPropertyElement.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,79 +1,79 @@ -package org.pojomatic.internal; - -import java.lang.reflect.AccessibleObject; -import java.lang.reflect.AnnotatedElement; -import java.lang.reflect.Member; - -import org.pojomatic.PropertyElement; - -public abstract class AbstractPropertyElement<E extends AccessibleObject & Member> -implements PropertyElement { - protected final E element; - private final String name; - - protected AbstractPropertyElement(E element, String name) { - element.setAccessible(true); - this.element = element; - this.name = name; - } - - public String getName() { - return name; - } - - public Object getValue(Object instance) { - if (instance == null) { - throw new NullPointerException("Instance is null: cannot get property value"); - } - else { - try { - return accessValue(instance); - } - catch (IllegalArgumentException e) { - throw new RuntimeException(e); - } - catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - } - } - - protected abstract Object accessValue(Object instance) - throws IllegalAccessException, IllegalArgumentException; - - /* (non-Javadoc) - * @see org.pojomatic.PropertyElement#getElement() - */ - public AnnotatedElement getElement() { - return this.element; - } - - public Class<?> getDeclaringClass() { - return element.getDeclaringClass(); - } - - @Override - public int hashCode() { - return element.hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final AbstractPropertyElement<?> other = (AbstractPropertyElement<?>) obj; - return element.equals(other.element); - } - - @Override - public String toString() { - return element.toString(); - } -} +package org.pojomatic.internal; + +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Member; + +import org.pojomatic.PropertyElement; + +public abstract class AbstractPropertyElement<E extends AccessibleObject & Member> +implements PropertyElement { + protected final E element; + private final String name; + + protected AbstractPropertyElement(E element, String name) { + element.setAccessible(true); + this.element = element; + this.name = name; + } + + public String getName() { + return name; + } + + public Object getValue(Object instance) { + if (instance == null) { + throw new NullPointerException("Instance is null: cannot get property value"); + } + else { + try { + return accessValue(instance); + } + catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } + catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } + + protected abstract Object accessValue(Object instance) + throws IllegalAccessException, IllegalArgumentException; + + /* (non-Javadoc) + * @see org.pojomatic.PropertyElement#getElement() + */ + public AnnotatedElement getElement() { + return this.element; + } + + public Class<?> getDeclaringClass() { + return element.getDeclaringClass(); + } + + @Override + public int hashCode() { + return element.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + final AbstractPropertyElement<?> other = (AbstractPropertyElement<?>) obj; + return element.equals(other.element); + } + + @Override + public String toString() { + return element.toString(); + } +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyAccessor.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyAccessor.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyAccessor.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,47 +1,47 @@ -package org.pojomatic.internal; - -import java.beans.Introspector; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; - -public class PropertyAccessor extends AbstractPropertyElement<Method> { - private final static String GET = "get", IS = "is"; - - public PropertyAccessor(Method method, String name) { - super(method, name.length() == 0 ? getName(method) : name); - } - - private static String getName(Method method) { - String methodName = method.getName(); - if (isPrefixedWith(methodName, GET)) { - return Introspector.decapitalize(methodName.substring(GET.length())); - } - else if (isBoolean(method.getReturnType()) && isPrefixedWith(methodName, IS)) { - return Introspector.decapitalize(methodName.substring(IS.length())); - } - else { - return methodName; - } - } - - private static boolean isBoolean(Class<?> clazz) { - return Boolean.class.equals(clazz) || Boolean.TYPE.equals(clazz); - } - - private static boolean isPrefixedWith(String name, String prefix) { - return name.length() > prefix.length() - && name.startsWith(prefix) - && Character.isUpperCase(name.charAt(prefix.length())); - } - - @Override - protected Object accessValue(Object instance) - throws IllegalArgumentException, IllegalAccessException { - try { - return element.invoke(instance, (Object[])null); - } - catch (InvocationTargetException e) { - throw new RuntimeException(e.getCause()); - } - } -} +package org.pojomatic.internal; + +import java.beans.Introspector; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +public class PropertyAccessor extends AbstractPropertyElement<Method> { + private final static String GET = "get", IS = "is"; + + public PropertyAccessor(Method method, String name) { + super(method, name.length() == 0 ? getName(method) : name); + } + + private static String getName(Method method) { + String methodName = method.getName(); + if (isPrefixedWith(methodName, GET)) { + return Introspector.decapitalize(methodName.substring(GET.length())); + } + else if (isBoolean(method.getReturnType()) && isPrefixedWith(methodName, IS)) { + return Introspector.decapitalize(methodName.substring(IS.length())); + } + else { + return methodName; + } + } + + private static boolean isBoolean(Class<?> clazz) { + return Boolean.class.equals(clazz) || Boolean.TYPE.equals(clazz); + } + + private static boolean isPrefixedWith(String name, String prefix) { + return name.length() > prefix.length() + && name.startsWith(prefix) + && Character.isUpperCase(name.charAt(prefix.length())); + } + + @Override + protected Object accessValue(Object instance) + throws IllegalArgumentException, IllegalAccessException { + try { + return element.invoke(instance, (Object[])null); + } + catch (InvocationTargetException e) { + throw new RuntimeException(e.getCause()); + } + } +} Modified: trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyField.java =================================================================== --- trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyField.java 2009-09-25 22:13:12 UTC (rev 161) +++ trunk/PojomaticAll/Pojomatic/src/main/java/org/pojomatic/internal/PropertyField.java 2009-09-25 22:17:23 UTC (rev 162) @@ -1,15 +1,15 @@ -package org.pojomatic.internal; - -import java.lang.reflect.Field; - -public class PropertyField extends AbstractPropertyElement<Field> { - public PropertyField(Field propertyField, String name) { - super(propertyField, name.length() == 0 ? propertyField.getName() : name); - } - - @Override - protected Object accessValue(Object instance) - throws IllegalArgumentException, IllegalAccessException { - return element.get(instance); - } -} +package org.pojomatic.internal; + +import java.lang.reflect.Field; + +public class PropertyField extends AbstractPropertyElement<Field> { + public PropertyField(Field propertyField, String name) { + super(propertyField, name.length() == 0 ? propertyField.getName() : name); + } + + @Override + protected Object accessValue(Object instance) + throws IllegalArgumentException, IllegalAccessException { + return element.get(instance); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |