Skip to content

Commit

Permalink
#4
Browse files Browse the repository at this point in the history
  • Loading branch information
ethlo committed Apr 24, 2017
1 parent 094950d commit 762d6e1
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private <T> void extractChangeData(final List<PropertyChange<?>> propChanges, fi
{
final String fieldName = field.getName();
final Object value = ReflectionUtils.getField(field, target);
propChanges.add(new PropertyChange(fieldName, field.getType(), deleted ? value : null, deleted ? null : value, field.getAnnotations()));
propChanges.add(new PropertyChange(fieldName, field.getType(), deleted ? value : null, deleted ? null : value));
}
}

Expand All @@ -72,7 +72,7 @@ public void extractSingle(Field field, String attrName, Class<?> attrType, Objec
if (! Objects.equals(oldValue, newValue))
{
@SuppressWarnings({ "rawtypes", "unchecked" })
final PropertyChange<?> propChange = new PropertyChange(attrName, attrType, oldValue, newValue, field.getAnnotations());
final PropertyChange<?> propChange = new PropertyChange(attrName, attrType, oldValue, newValue);
propChanges.add(propChange);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ private <T> PropertyChange<T> transform(PropertyChange<T> change)
{
final Object oldRefColl = transform(type, (Collection<T>)change.getNewValue());
final Object newRefColl = transform(type, (Collection<T>)change.getOldValue());
return new PropertyChange(change.getPropertyName(), change.getPropertyType(), oldRefColl, newRefColl, change.getAnnotations());
return new PropertyChange(change.getPropertyName(), change.getPropertyType(), oldRefColl, newRefColl);
}
else if (Map.class.isAssignableFrom(type))
{
final Object oldRefColl = transform(type, (Map)change.getNewValue());
final Object newRefColl = transform(type, (Map)change.getOldValue());
return new PropertyChange(change.getPropertyName(), change.getPropertyType(), oldRefColl, newRefColl, change.getAnnotations());
return new PropertyChange(change.getPropertyName(), change.getPropertyType(), oldRefColl, newRefColl);
}
else if (isEntity(type))
{
final Serializable newId = change.getNewValue() != null ? getId(change.getNewValue()) : null;
final Serializable oldId = change.getOldValue() != null ? getId(change.getOldValue()) : null;
return new PropertyChange(change.getPropertyName(), change.getPropertyType(), oldId, newId, change.getAnnotations());
return new PropertyChange(change.getPropertyName(), change.getPropertyType(), oldId, newId);
}
return change;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package com.ethlo.dachs;

import java.lang.reflect.Field;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Predicate;

import com.ethlo.dachs.util.ReflectionUtil;

/**
* Allows the specification of a class and field filter for the change events
*/
public class EntityChangeSetListenerFilter implements EntityChangeSetListener
public class DefaultFilteredEntityChangeSetListener implements FilteredEntityChangeSetListener
{
private final EntityChangeSetListener delegate;
private final Predicate<Object> classFilter;
private final Predicate<PropertyChange<?>> fieldFilter;
private final Predicate<Object> entityFilter;
private final Predicate<Map.Entry<Field, PropertyChange<?>>> fieldFilter;

public EntityChangeSetListenerFilter(EntityChangeSetListener delegate, Predicate<Object> classFilter, Predicate<PropertyChange<?>> fieldFilter)
public DefaultFilteredEntityChangeSetListener(EntityChangeSetListener delegate, Predicate<Object> entityFilter, Predicate<Entry<Field, PropertyChange<?>>> fieldFilter)
{
this.delegate = delegate;
this.classFilter = classFilter;
this.entityFilter = entityFilter;
this.fieldFilter = fieldFilter;
}

@Override
public void preDataChanged(EntityDataChangeSet changeset)
private void filter(EntityDataChangeSet changeset)
{
filter(changeset.getCreated());
filter(changeset.getUpdated());
filter(changeset.getDeleted());
delegate.preDataChanged(changeset);
}

private void filter(Collection<EntityDataChange> changes)
Expand All @@ -35,17 +39,20 @@ private void filter(Collection<EntityDataChange> changes)
while (changesIter.hasNext())
{
final EntityDataChange change = changesIter.next();
if (classFilter != null && !classFilter.test(change.getEntity()))
final Object entity = change.getEntity();
if (entityFilter != null && !entityFilter.test(entity))
{
changesIter.remove();
}
else
{
final Iterator<PropertyChange<?>> propChangeIter = change.getPropertyChanges().iterator();
while (propChangeIter.hasNext())
{
{
final PropertyChange<?> propChange = propChangeIter.next();
if (fieldFilter != null && !fieldFilter.test(propChange))
final String propName = propChange.getPropertyName();
final Field field = ReflectionUtil.getField(entity.getClass(), propName);
if (fieldFilter != null && !fieldFilter.test(new AbstractMap.SimpleEntry<>(field, propChange)))
{
propChangeIter.remove();
}
Expand All @@ -55,17 +62,31 @@ private void filter(Collection<EntityDataChange> changes)
}

@Override
public void postDataChanged(EntityDataChangeSet changeset)
public Predicate<Entry<Field, PropertyChange<?>>> fieldFilter()
{
filter(changeset.getCreated());
filter(changeset.getUpdated());
filter(changeset.getDeleted());
delegate.postDataChanged(changeset);
return this.fieldFilter;
}

@Override
public Predicate<Object> entityFilter()
{
return entityFilter;
}

public void preDataChanged(EntityDataChangeSet changeset)
{
filter(changeset);
delegate.preDataChanged(changeset);
}

public void postDataChanged(EntityDataChangeSet changeset)
{
filter(changeset);
delegate.postDataChanged(changeset);
}

public void begin()
{
// Empty implementation
delegate.begin();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void prependIdPropertyChange(Field field, String idPropertyName, Object i
{
throw new IllegalArgumentException("id should not be null");
}
final PropertyChange propChange = new PropertyChange(idPropertyName, id.getClass(), deleted ? id : null, deleted ? null : id, field.getAnnotations());
final PropertyChange propChange = new PropertyChange(idPropertyName, id.getClass(), deleted ? id : null, deleted ? null : id);
this.properties.put(idPropertyName, propChange);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ethlo.dachs;

import java.lang.reflect.Field;
import java.util.Map.Entry;
import java.util.function.Predicate;

/**
*
*/
public interface FilteredEntityChangeSetListener extends EntityChangeSetListener
{
Predicate<Entry<Field, PropertyChange<?>>> fieldFilter();
Predicate<Object> entityFilter();
}
29 changes: 2 additions & 27 deletions dachs-common/src/main/java/com/ethlo/dachs/PropertyChange.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.ethlo.dachs;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class PropertyChange<T>
{
private Map<String, Annotation> annotations;
private String propertyName;
private Class<T> propertyType;
private T oldValue;
Expand All @@ -19,7 +15,7 @@ private PropertyChange()

}

public PropertyChange(String propertyName, Class<T> propertyType, T oldValue, T newValue, Annotation[] annotations)
public PropertyChange(String propertyName, Class<T> propertyType, T oldValue, T newValue)
{
if (propertyName == null)
{
Expand All @@ -32,16 +28,7 @@ public PropertyChange(String propertyName, Class<T> propertyType, T oldValue, T
}
this.propertyType = propertyType;
this.oldValue = oldValue;
this.newValue = newValue;

if (annotations != null)
{
this.annotations = new HashMap<>(annotations.length);
for (Annotation ann : annotations)
{
this.annotations.put(ann.annotationType().getCanonicalName(), ann);
}
}
this.newValue = newValue;
}

public String getPropertyName()
Expand Down Expand Up @@ -95,16 +82,4 @@ public String toString()
{
return "PropertyChange [propertyName=" + propertyName + ", entityType=" + propertyType + ", oldValue=" + oldValue + ", newValue=" + newValue + "]";
}

public boolean hasAnnotation(Class<? extends Annotation> annotationType)
{
final String annName = annotationType.getCanonicalName();
final boolean contains = annotations != null && annotations.get(annName) != null;
return contains;
}

public Annotation[] getAnnotations()
{
return annotations != null ? annotations.values().toArray(new Annotation[annotations.size()]) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,11 @@ public class EntityChangeSetFilterTest
public void testFiltering()
{
final CollectingEntityChangeSetListener listener = new CollectingEntityChangeSetListener();
final EntityChangeSetListenerFilter filter = new EntityChangeSetListenerFilter(listener,
final DefaultFilteredEntityChangeSetListener filter = new DefaultFilteredEntityChangeSetListener(listener,
c->{return c.getClass().getAnnotation(Deprecated.class) == null;},
ch->{return !ch.hasAnnotation(Deprecated.class);});
ch->{return ch.getKey().getAnnotation(Deprecated.class) == null;});
final MutableEntityDataChangeSet cs = new MutableEntityDataChangeSet();
final EntityListenerIgnore[] annotations = new EntityListenerIgnore[]
{
new EntityListenerIgnore()
{
@Override
public Class<? extends java.lang.annotation.Annotation> annotationType()
{
return Deprecated.class;
}
}
};

cs.getCreated().add(new EntityDataChangeImpl(123, new DeprecatedEntityClass(), Arrays.asList(new PropertyChange<Integer>("id", Integer.class, null, 123, annotations))));
cs.getCreated().add(new EntityDataChangeImpl(123, new DeprecatedEntityClass(), Arrays.asList(new PropertyChange<Integer>("id", Integer.class, null, 123))));
filter.preDataChanged(cs);
System.out.println(StringUtils.collectionToCommaDelimitedString(listener.getPreDataChangeSet().getCreated()));
}
Expand Down
12 changes: 6 additions & 6 deletions dachs-common/src/test/java/com/ethlo/dachs/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class ModelTest
@Test
public void testPropertyChangeEquals()
{
final PropertyChange<String> a = new PropertyChange<String>("foo", String.class, "bar", "baz", null);
final PropertyChange<String> b = new PropertyChange<String>("foo", String.class, "bar", "baz", null);
final PropertyChange<String> c = new PropertyChange<String>("foo", String.class, "bar", "bad", null);
final PropertyChange<String> a = new PropertyChange<String>("foo", String.class, "bar", "baz");
final PropertyChange<String> b = new PropertyChange<String>("foo", String.class, "bar", "baz");
final PropertyChange<String> c = new PropertyChange<String>("foo", String.class, "bar", "bad");
assertThat(a).isEqualTo(b);
assertThat(b).isEqualTo(a);
assertThat(a).isNotEqualTo(c);
Expand All @@ -21,9 +21,9 @@ public void testPropertyChangeEquals()
@Test
public void testPropertyChangeHashCode()
{
final PropertyChange<String> a = new PropertyChange<String>("foo", String.class, "bar", "baz", null);
final PropertyChange<String> b = new PropertyChange<String>("foo", String.class, "bar", "baz", null);
final PropertyChange<String> c = new PropertyChange<String>("foo", String.class, "bar", "bad", null);
final PropertyChange<String> a = new PropertyChange<String>("foo", String.class, "bar", "baz");
final PropertyChange<String> b = new PropertyChange<String>("foo", String.class, "bar", "baz");
final PropertyChange<String> c = new PropertyChange<String>("foo", String.class, "bar", "bad");
assertThat(a.hashCode()).isEqualTo(b.hashCode());
assertThat(a.hashCode()).isNotEqualTo(c.hashCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private Collection<PropertyChange<?>> getProperties(Serializable id, final Objec

if (field != null && this.fieldFilter.test(field) && !Objects.equals(previousState[i], currentState[i]))
{
final PropertyChange changed = new PropertyChange(propertyName, types[i].getReturnedClass(), previousState[i], currentState[i], field.getAnnotations());
final PropertyChange changed = new PropertyChange(propertyName, types[i].getReturnedClass(), previousState[i], currentState[i]);
retVal.add(changed);
}
}
Expand All @@ -94,7 +94,7 @@ private Collection<PropertyChange<?>> getProperties(Serializable id, final Objec
entityField.setAccessible(true);
final Object after = entityField.get(entity);
final Object before = after;
final PropertyChange changed = new PropertyChange(fieldName, fieldType, before, after, entityField.getAnnotations());
final PropertyChange changed = new PropertyChange(fieldName, fieldType, before, after);
retVal.add(changed);
},
(f)->fieldFilter.test(f));
Expand Down

0 comments on commit 762d6e1

Please sign in to comment.