|
| 1 | +package de.danielbechler.diff; |
| 2 | + |
| 3 | +import de.danielbechler.diff.accessor.*; |
| 4 | +import de.danielbechler.diff.introspect.*; |
| 5 | +import de.danielbechler.diff.node.*; |
| 6 | +import de.danielbechler.diff.path.*; |
| 7 | +import de.danielbechler.util.*; |
| 8 | + |
| 9 | +import java.util.*; |
| 10 | + |
| 11 | +/** @author Daniel Bechler */ |
| 12 | +public final class BeanDiffer extends AbstractObjectDiffer implements ObjectDiffer |
| 13 | +{ |
| 14 | + private final Introspector introspector = new StandardIntrospector(); |
| 15 | + |
| 16 | + @SuppressWarnings({"unchecked"}) |
| 17 | + public <T> DiffNode<T> compare(final T modifiedInstance, final T baseInstance) |
| 18 | + { |
| 19 | + Assert.notNull(modifiedInstance, "Missing argument [modifiedInstance]"); |
| 20 | +// Assert.notNull(baseInstance, "Missing argument [baseInstance]"); |
| 21 | + if (baseInstance != null && !modifiedInstance.getClass().equals(baseInstance.getClass())) |
| 22 | + { |
| 23 | + throw new IllegalArgumentException("Comparison of different types is not (yet) supported. " + |
| 24 | + "Modified was:" + modifiedInstance.getClass() + "; " + |
| 25 | + "Base was: " + baseInstance.getClass()); |
| 26 | + } |
| 27 | + final Accessor<T> accessor = new RootAccessor<T>(); |
| 28 | + final T defaultInstance = (T) Classes.freshInstanceOf(modifiedInstance.getClass()); |
| 29 | + return compare(modifiedInstance, baseInstance, defaultInstance, accessor); |
| 30 | + } |
| 31 | + |
| 32 | + public DiffNode compare(final Instances instances, final Accessor accessor) |
| 33 | + { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + public DiffNode compare(final Object modifiedInstance, |
| 38 | + final Object baseInstance, |
| 39 | + final Object defaultInstance, |
| 40 | + final Accessor accessor) |
| 41 | + { |
| 42 | + final Accessor childAccessor; |
| 43 | + if (accessor instanceof ChainingAccessor) |
| 44 | + { |
| 45 | + childAccessor = ((ChainingAccessor) accessor).getDetachedChildAccessor(); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + childAccessor = accessor; |
| 50 | + } |
| 51 | + final Object baseValue = childAccessor.get(baseInstance); |
| 52 | + final Object modifiedValue = childAccessor.get(modifiedInstance); |
| 53 | + final Object defaultValue = childAccessor.get(defaultInstance); |
| 54 | + final PropertyPath selectorPath = accessor.getPath(); |
| 55 | + if (isIgnoreProperty(accessor)) |
| 56 | + { |
| 57 | + final DiffNode difference = new DefaultNode(childAccessor); |
| 58 | + difference.setType(DifferenceType.UNTOUCHED); |
| 59 | + return difference; |
| 60 | + } |
| 61 | + final Class<?> propertyType = safeTypeOf(childAccessor, baseValue, modifiedValue, defaultValue); |
| 62 | + if (Collection.class.isAssignableFrom(propertyType)) |
| 63 | + { |
| 64 | + final Collection modifiedCollection = (Collection) modifiedValue; |
| 65 | + final Collection baseCollection = (Collection) baseValue; |
| 66 | + final Collection defaultCollection = (Collection) defaultValue; |
| 67 | + return new CollectionDiffer(this).compare(modifiedCollection, baseCollection, defaultCollection, accessor); |
| 68 | + } |
| 69 | + else if (Map.class.isAssignableFrom(propertyType)) |
| 70 | + { |
| 71 | + return new MapDiffer(this).compare( |
| 72 | + modifiedValue != null ? Map.class.cast(modifiedValue) : null, |
| 73 | + baseValue != null ? Map.class.cast(baseValue) : null, |
| 74 | + defaultValue != null ? Map.class.cast(defaultValue) : null, |
| 75 | + accessor |
| 76 | + ); |
| 77 | + } |
| 78 | + else if (propertyType.isArray()) |
| 79 | + { |
| 80 | + // TODO |
| 81 | + } |
| 82 | + final DiffNode difference = new DefaultNode(childAccessor); |
| 83 | + difference.setType(DifferenceType.UNTOUCHED); |
| 84 | + if (hasBeenAdded(defaultValue, baseValue, modifiedValue)) |
| 85 | + { |
| 86 | + difference.setType(DifferenceType.ADDED); |
| 87 | + } |
| 88 | + else if (hasBeenRemoved(defaultValue, baseValue, modifiedValue)) |
| 89 | + { |
| 90 | + difference.setType(DifferenceType.REMOVED); |
| 91 | + } |
| 92 | + else if (modifiedValue == baseValue) // both null or both same instance |
| 93 | + { |
| 94 | + return difference; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + if (Classes.isSimpleType(propertyType) || isEqualsOnlyPath(selectorPath) || isEqualsOnlyType(propertyType)) |
| 99 | + { |
| 100 | + if (!Objects.isEqual(baseValue, modifiedValue)) |
| 101 | + { |
| 102 | + difference.setType(DifferenceType.CHANGED); |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + for (final Accessor<?> typeAccessor : introspect(propertyType)) |
| 108 | + { |
| 109 | + final Accessor<Object> chain = new ChainedAccessor(accessor, typeAccessor); |
| 110 | + final DiffNode<Object> node = compare(modifiedValue, baseValue, defaultValue, chain); |
| 111 | + if (node.isDifferent()) |
| 112 | + { |
| 113 | + difference.addChild(node); |
| 114 | + } |
| 115 | + } |
| 116 | + if (difference.hasChildren()) |
| 117 | + { |
| 118 | + difference.setType(DifferenceType.CHANGED); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + return difference; |
| 123 | + } |
| 124 | + |
| 125 | + private static Class<?> safeTypeOf(final Accessor<?> accessor, |
| 126 | + final Object baseValue, |
| 127 | + final Object modifiedValue, |
| 128 | + final Object defaultValue) |
| 129 | + { |
| 130 | + if (accessor instanceof PropertyAccessor) |
| 131 | + { |
| 132 | + return ((PropertyAccessor) accessor).getType(); |
| 133 | + } |
| 134 | + return typeOf(baseValue, modifiedValue, defaultValue); |
| 135 | + } |
| 136 | + |
| 137 | + @SuppressWarnings({"unchecked"}) |
| 138 | + private static <T> Class<T> typeOf(final T... values) |
| 139 | + { |
| 140 | + for (final T value : values) |
| 141 | + { |
| 142 | + if (value != null) |
| 143 | + { |
| 144 | + return (Class<T>) value.getClass(); |
| 145 | + } |
| 146 | + } |
| 147 | + return (Class<T>) Object.class; |
| 148 | +// throw new RuntimeException("Couldn't extract object type"); |
| 149 | + } |
| 150 | + |
| 151 | + private static boolean hasBeenRemoved(final Object defaultValue, final Object baseValue, final Object updatedValue) |
| 152 | + { |
| 153 | + if (baseValue != null && updatedValue == null && !Classes.isSimpleType(baseValue.getClass())) |
| 154 | + { |
| 155 | + return true; |
| 156 | + } |
| 157 | + return Objects.isEqual(defaultValue, updatedValue) && !Objects.isEqual(baseValue, updatedValue); |
| 158 | + } |
| 159 | + |
| 160 | + private static boolean hasBeenAdded(final Object defaultValue, final Object baseValue, final Object updatedValue) |
| 161 | + { |
| 162 | + // Not sure if the commented part is needed. Tests succeed without it... but not everything is tested yet. |
| 163 | + if (baseValue == null && updatedValue != null) // && !Classes.isSimpleType(updatedValue.getClass())) |
| 164 | + { |
| 165 | + return true; |
| 166 | + } |
| 167 | + return Objects.isEqual(defaultValue, baseValue) && !Objects.isEqual(baseValue, updatedValue); |
| 168 | + } |
| 169 | + |
| 170 | + private Iterable<Accessor<?>> introspect(final Class<?> type) |
| 171 | + { |
| 172 | + return introspector.introspect(type); |
| 173 | + } |
| 174 | +} |
0 commit comments