From: <chr...@us...> - 2009-08-14 23:02:52
|
Revision: 123 http://pojomatic.svn.sourceforge.net/pojomatic/?rev=123&view=rev Author: chriswhansen Date: 2009-08-14 23:02:37 +0000 (Fri, 14 Aug 2009) Log Message: ----------- Pojomatic.diff() no longer requires a non-null first argument. Likewise, Pojomator.diff() no longer requires a non-null first argument. Created NoDifferences singleton class to represent no differences and removed similar logic from PropertyDifferences. Modified Paths: -------------- trunk/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java trunk/Pojomatic/src/main/java/org/pojomatic/Pojomator.java trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java trunk/Pojomatic/src/main/java/org/pojomatic/diff/PropertyDifferences.java trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java trunk/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java trunk/Pojomatic/src/test/java/org/pojomatic/diff/PropertyDifferencesTest.java trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java Added Paths: ----------- trunk/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java trunk/Pojomatic/src/main/java/org/pojomatic/diff/NoDifferences.java trunk/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java trunk/Pojomatic/src/test/java/org/pojomatic/diff/NoDifferencesTest.java Property Changed: ---------------- trunk/Pojomatic/ Property changes on: trunk/Pojomatic ___________________________________________________________________ Added: svn:ignore + target bin Modified: trunk/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/main/java/org/pojomatic/Pojomatic.java 2009-08-14 23:02:37 UTC (rev 123) @@ -1,6 +1,8 @@ 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; @@ -114,14 +116,24 @@ * * @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 - must not be {@code null} + * @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 IllegalArgumentException { + 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); } Modified: trunk/Pojomatic/src/main/java/org/pojomatic/Pojomator.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/Pojomator.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/main/java/org/pojomatic/Pojomator.java 2009-08-14 23:02:37 UTC (rev 123) @@ -46,7 +46,7 @@ * {@link Differences#areEqual()} on the returned object will return true iff * {@code instance.equals(other)}. * - * @param instance the instance to diff against - must not be {@code null} + * @param instance the instance to diff against * @param other the instance to diff * @return the differences between {@code instance} and {@code other} * among the properties examined by {@link #doEquals(Object, Object)}. Added: trunk/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java (rev 0) +++ trunk/Pojomatic/src/main/java/org/pojomatic/diff/AbstractNullDifference.java 2009-08-14 23:02:37 UTC (rev 123) @@ -0,0 +1,46 @@ +package org.pojomatic.diff; + +import org.pojomatic.Pojomatic; +import org.pojomatic.annotations.AutoProperty; +import org.pojomatic.annotations.DefaultPojomaticPolicy; +import org.pojomatic.annotations.PojomaticPolicy; +import org.pojomatic.annotations.Property; + +/** + * A {@link Differences} whose {@link Differences#toString()} is generated exactly once. + */ +@AutoProperty(policy=DefaultPojomaticPolicy.NONE) +abstract class AbstractNullDifference implements Differences { + @Property(policy=PojomaticPolicy.ALL) + protected final Object instance; + + /** + * @param instance the non-null object being compared to {@code null}. + * @throws NullPointerException if {@code instance} is {@code null}. + */ + public AbstractNullDifference(Object instance) { + if (instance == null) { + throw new NullPointerException("Instance cannot be null"); + } + + this.instance = instance; + } + + public final boolean areEqual() { + return false; + } + + @Override + public abstract String toString(); + + @Override + public int hashCode() { + return Pojomatic.hashCode(this); + } + + @Override + public boolean equals(Object o) { + return Pojomatic.equals(this, o); + } + +} Added: trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java (rev 0) +++ trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceFromNull.java 2009-08-14 23:02:37 UTC (rev 123) @@ -0,0 +1,18 @@ +package org.pojomatic.diff; + +/** + * The result of comparing {@code null} to a non-null object. + * @see DifferenceToNull + */ +public final class DifferenceFromNull extends AbstractNullDifference { + + public DifferenceFromNull(Object other) { + super(other); + } + + @Override + public String toString() { + return "null is different than the object {" + instance + "}"; + } + +} Modified: trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/main/java/org/pojomatic/diff/DifferenceToNull.java 2009-08-14 23:02:37 UTC (rev 123) @@ -1,21 +1,17 @@ package org.pojomatic.diff; -public class DifferenceToNull implements Differences { - private final Object instance; +/** + * The result of comparing a non-null object to {@code null}. + * @see DifferenceFromNull + */ +public final class DifferenceToNull extends AbstractNullDifference { public DifferenceToNull(Object instance) { - if (instance == null) { - throw new NullPointerException(); - } - this.instance = instance; + super(instance); } @Override public String toString() { - return "the object {" + instance + "} is different than null" ; + return "the object {" + instance + "} is different than null"; } - - public boolean areEqual() { - return false; - } } Added: trunk/Pojomatic/src/main/java/org/pojomatic/diff/NoDifferences.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/diff/NoDifferences.java (rev 0) +++ trunk/Pojomatic/src/main/java/org/pojomatic/diff/NoDifferences.java 2009-08-14 23:02:37 UTC (rev 123) @@ -0,0 +1,22 @@ +package org.pojomatic.diff; + +public final class NoDifferences implements Differences { + private static final NoDifferences INSTANCE = new NoDifferences(); + + private NoDifferences() { + } + + public boolean areEqual() { + return true; + } + + @Override + public String toString() { + return "no differences"; + } + + public static NoDifferences getInstance() { + return INSTANCE; + } + +} Modified: trunk/Pojomatic/src/main/java/org/pojomatic/diff/PropertyDifferences.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/diff/PropertyDifferences.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/main/java/org/pojomatic/diff/PropertyDifferences.java 2009-08-14 23:02:37 UTC (rev 123) @@ -10,10 +10,18 @@ @Property private final List<Difference> differences; + /** + * @param differences cannot be {@code null} or empty + * @throws NullPointerException if {@code differences} is {@code null} + * @throws IllegalArgumentException if {@code differences.isEmpty()} is {@code true} + */ public PropertyDifferences(List<Difference> differences) { if (differences == null) { throw new NullPointerException("list of differences is null"); } + if (differences.isEmpty()) { + throw new IllegalArgumentException("list of differences is empty"); + } this.differences = Collections.unmodifiableList(differences); } @@ -21,15 +29,22 @@ return differences; } + public boolean areEqual() { + return false; + } + @Override public String toString() { - return differences.isEmpty() ? "no differences" : differences.toString(); + return differences.toString(); } - @Override public boolean equals(Object other) { return Pojomatic.equals(this, other); } - @Override public int hashCode() { return Pojomatic.hashCode(this); } + @Override + public boolean equals(Object other) { + return Pojomatic.equals(this, other); + } - public boolean areEqual() { - return differences.isEmpty(); + @Override + public int hashCode() { + return Pojomatic.hashCode(this); } } Modified: trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java =================================================================== --- trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/main/java/org/pojomatic/internal/PojomatorImpl.java 2009-08-14 23:02:37 UTC (rev 123) @@ -3,7 +3,6 @@ import java.lang.reflect.AnnotatedElement; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.pojomatic.Pojomator; @@ -13,8 +12,10 @@ import org.pojomatic.annotations.Property; import org.pojomatic.annotations.PropertyFormat; import org.pojomatic.diff.Difference; +import org.pojomatic.diff.DifferenceFromNull; import org.pojomatic.diff.DifferenceToNull; import org.pojomatic.diff.Differences; +import org.pojomatic.diff.NoDifferences; import org.pojomatic.diff.PropertyDifferences; import org.pojomatic.formatter.DefaultPojoFormatter; import org.pojomatic.formatter.DefaultPropertyFormatter; @@ -257,15 +258,16 @@ } public Differences doDiff(T instance, T other) { - if (instance == null) { - throw new NullPointerException("instance is null"); + if (instance == other) { + return NoDifferences.getInstance(); } - if (other == null) { + else if (instance == null) { + return new DifferenceFromNull(other); + } + else if (other == null) { return new DifferenceToNull(instance); } - if (instance == other) { - return new PropertyDifferences(Collections.<Difference>emptyList()); - } + checkClass(instance, "instance"); checkClass(other, "other"); List<Difference> differences = new ArrayList<Difference>(); @@ -276,6 +278,10 @@ differences.add(new Difference(prop.getName(), instanceValue, otherValue)); } } + + if (differences.isEmpty()) { + return NoDifferences.getInstance(); + } return new PropertyDifferences(differences); } Modified: trunk/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/test/java/org/pojomatic/PojomaticTest.java 2009-08-14 23:02:37 UTC (rev 123) @@ -2,12 +2,10 @@ import static org.junit.Assert.*; -import java.util.Collections; - import org.junit.Test; import org.pojomatic.annotations.Property; -import org.pojomatic.diff.Difference; -import org.pojomatic.diff.PropertyDifferences; +import org.pojomatic.diff.DifferenceFromNull; +import org.pojomatic.diff.NoDifferences; import org.pojomatic.internal.PojomatorImpl; public class PojomaticTest { @@ -32,11 +30,25 @@ @Test public void testDiff() { - assertEquals( - new PropertyDifferences(Collections.<Difference>emptyList()), Pojomatic.diff(BEAN, BEAN)); + assertEquals(NoDifferences.getInstance(), Pojomatic.diff(BEAN, BEAN)); } @Test + public void testDiffBothNull() { + assertEquals(NoDifferences.getInstance(), Pojomatic.diff(null, null)); + } + + @Test + public void testDiffNullFirst() { + assertEquals(new DifferenceFromNull(BEAN), Pojomatic.diff(null, BEAN)); + } + + @Test + public void testDiffNullSecond() { + assertEquals(BEAN_POJOMATOR.doDiff(BEAN, null), Pojomatic.diff(BEAN, null)); + } + + @Test public void testHashCode() { assertEquals(BEAN_POJOMATOR.doHashCode(BEAN), Pojomatic.hashCode(BEAN)); } Added: trunk/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java (rev 0) +++ trunk/Pojomatic/src/test/java/org/pojomatic/diff/DifferenceFromNullTest.java 2009-08-14 23:02:37 UTC (rev 123) @@ -0,0 +1,23 @@ +package org.pojomatic.diff; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class DifferenceFromNullTest { + + @Test(expected=NullPointerException.class) + public void testConstructorNullPointerException() { + new DifferenceFromNull(null); + } + + @Test + public void testToString() { + assertEquals("null is different than the object {3}", new DifferenceFromNull(3).toString()); + } + + @Test + public void testAreEqual() { + assertFalse(new DifferenceFromNull(3).areEqual()); + } +} Added: trunk/Pojomatic/src/test/java/org/pojomatic/diff/NoDifferencesTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/diff/NoDifferencesTest.java (rev 0) +++ trunk/Pojomatic/src/test/java/org/pojomatic/diff/NoDifferencesTest.java 2009-08-14 23:02:37 UTC (rev 123) @@ -0,0 +1,19 @@ +package org.pojomatic.diff; + +import static org.junit.Assert.*; + +import org.junit.Test; + +public class NoDifferencesTest { + + @Test + public void testAreEqual() { + assertTrue(NoDifferences.getInstance().areEqual()); + } + + @Test + public void testToString() { + assertEquals("no differences", NoDifferences.getInstance().toString()); + } + +} Modified: trunk/Pojomatic/src/test/java/org/pojomatic/diff/PropertyDifferencesTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/diff/PropertyDifferencesTest.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/test/java/org/pojomatic/diff/PropertyDifferencesTest.java 2009-08-14 23:02:37 UTC (rev 123) @@ -13,10 +13,10 @@ public void testConstructorNullPointerException() { new PropertyDifferences(null); } - @Test - public void testEmptyToString() { - assertEquals( - "no differences", new PropertyDifferences(Collections.<Difference>emptyList()).toString()); + + @Test(expected=IllegalArgumentException.class) + public void testEmptyDifferences() { + new PropertyDifferences(Collections.<Difference>emptyList()); } @Test @@ -39,7 +39,6 @@ @Test public void testAreEqual() { - assertTrue(new PropertyDifferences(Collections.<Difference>emptyList()).areEqual()); assertFalse(new PropertyDifferences(Arrays.asList(new Difference("foo", 3, 4))).areEqual()); } } Modified: trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java =================================================================== --- trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java 2009-08-14 22:58:29 UTC (rev 122) +++ trunk/Pojomatic/src/test/java/org/pojomatic/internal/PojomatorImplTest.java 2009-08-14 23:02:37 UTC (rev 123) @@ -15,6 +15,7 @@ import org.pojomatic.annotations.Property; import org.pojomatic.annotations.PropertyFormat; import org.pojomatic.diff.Difference; +import org.pojomatic.diff.DifferenceFromNull; import org.pojomatic.diff.DifferenceToNull; import org.pojomatic.diff.Differences; import org.pojomatic.diff.PropertyDifferences; @@ -220,10 +221,22 @@ makePojomatorImpl(FormattedObject.class).doToString(new FormattedObject("x"))); } - @Test(expected=NullPointerException.class) public void testDiffNullInstance() { - OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, new ObjectPairProperty("this", "that")); + @Test public void testDiffNullInstance() { + ObjectPairProperty other = new ObjectPairProperty("this", "that"); + Differences actual = OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, other); + assertTrue(actual instanceof DifferenceFromNull); } + @Test public void testDiffNullOther() { + ObjectPairProperty instance = new ObjectPairProperty("this", "that"); + Differences actual = OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(instance, null); + assertTrue(actual instanceof DifferenceToNull); + } + + @Test public void testDiffNulls() { + assertTrue(OBJECT_PAIR_PROPERTY_POJOMATOR.doDiff(null, null).areEqual()); + } + @Test public void testDiffSameObject() { ObjectPairProperty objectPairProperty = new ObjectPairProperty("this", "that"); assertTrue( This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |