diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/BigDecimalHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/BigDecimalHandler.java index 6a183cb..4b69eba 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/BigDecimalHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/BigDecimalHandler.java @@ -2,9 +2,8 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; import java.math.BigDecimal; -import java.math.BigInteger; -class BigDecimalHandler extends PrimitiveType { +class BigDecimalHandler extends PrimitiveTypeHandler { public BigDecimalHandler(MethodHandle getter, MethodHandle setter) { super(BigDecimal.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/BigIntegerHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/BigIntegerHandler.java index f0d0c59..28f2012 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/BigIntegerHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/BigIntegerHandler.java @@ -3,7 +3,7 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; import java.math.BigInteger; -class BigIntegerHandler extends PrimitiveType { +class BigIntegerHandler extends PrimitiveTypeHandler { public BigIntegerHandler(MethodHandle getter, MethodHandle setter) { super(BigInteger.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/ByteHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/ByteHandler.java index e5f0578..aa4769f 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/ByteHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/ByteHandler.java @@ -5,7 +5,7 @@ import java.lang.invoke.MethodHandle; /** * Stores a byte value. */ -class ByteHandler extends PrimitiveType { +class ByteHandler extends PrimitiveTypeHandler { public ByteHandler(MethodHandle getter, MethodHandle setter) { super(Byte.class, getter, setter); diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/CompoundType.java b/lib/src/main/java/nl/sanderhautvast/contiguous/CompoundType.java deleted file mode 100644 index baabfc5..0000000 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/CompoundType.java +++ /dev/null @@ -1,51 +0,0 @@ -package nl.sanderhautvast.contiguous; - -import java.lang.invoke.MethodHandle; -import java.lang.reflect.Field; -import java.util.*; - -class CompoundType extends Type { - private final Map properties = new LinkedHashMap<>(); - - private MethodHandle getter; - private MethodHandle setter; - - - CompoundType(Class type) { - super(type, null,null); - } - - void setGetter(MethodHandle getter) { - this.getter = getter; - } - - public MethodHandle getGetter() { - return getter; - } - - public MethodHandle getSetter() { - return setter; - } - - void setSetter(MethodHandle setter) { - this.setter = setter; - } - - public Class getType() { - return type; - } - - Collection getProperties() { - return properties.values(); - } - - void addHandler(String propertyName, PrimitiveType primitiveType) { - properties.put(propertyName, primitiveType); - } - - void addChild(Field property, CompoundType childCompoundType) { - this.properties.put(property.getName(), childCompoundType); - } - - -} diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/CompoundTypeHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/CompoundTypeHandler.java new file mode 100644 index 0000000..36447da --- /dev/null +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/CompoundTypeHandler.java @@ -0,0 +1,29 @@ +package nl.sanderhautvast.contiguous; + +import java.lang.reflect.Field; +import java.util.*; + +class CompoundTypeHandler extends TypeHandler { + private final Map properties = new LinkedHashMap<>(); + + + + CompoundTypeHandler(Class type) { + super(type, null,null); + } + + + Collection getProperties() { + return properties.values(); + } + + void addHandler(String propertyName, PrimitiveTypeHandler primitiveType) { + properties.put(propertyName, primitiveType); + } + + void addChild(Field property, CompoundTypeHandler childCompoundType) { + this.properties.put(property.getName(), childCompoundType); + } + + +} diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java b/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java index 82d2500..8a9581a 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/ContiguousList.java @@ -52,7 +52,7 @@ public class ContiguousList implements List { private int size; - private Type type; + private TypeHandler type; public ContiguousList(Class type) { inspectType(type); @@ -71,7 +71,7 @@ public class ContiguousList implements List { if (PropertyHandlerFactory.isKnownType(type)) { this.type = PropertyHandlerFactory.forType(type); } else { - CompoundType compoundType = new CompoundType(type); + CompoundTypeHandler compoundType = new CompoundTypeHandler(type); this.type = compoundType; try { addPropertyHandlersForCompoundType(type, compoundType); @@ -81,7 +81,7 @@ public class ContiguousList implements List { } } - private void addPropertyHandlersForCompoundType(Class type, CompoundType parentCompoundType) throws IllegalAccessException { + private void addPropertyHandlersForCompoundType(Class type, CompoundTypeHandler parentCompoundType) throws IllegalAccessException { final MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(type, MethodHandles.lookup()); Arrays.stream(type.getDeclaredFields()) .forEach(field -> { @@ -91,11 +91,11 @@ public class ContiguousList implements List { MethodHandle setter = lookup.findSetter(type, field.getName(), fieldType); if (PropertyHandlerFactory.isKnownType(fieldType)) { - PrimitiveType primitiveType = PropertyHandlerFactory.forType(fieldType, getter, setter); + PrimitiveTypeHandler primitiveType = PropertyHandlerFactory.forType(fieldType, getter, setter); parentCompoundType.addHandler(field.getName(), primitiveType); } else { - CompoundType newParent = new CompoundType(fieldType); + CompoundTypeHandler newParent = new CompoundTypeHandler(fieldType); newParent.setGetter(getter); newParent.setSetter(setter); parentCompoundType.addChild(field, newParent); @@ -126,18 +126,18 @@ public class ContiguousList implements List { return true; } - private void getProperties(Object element, Type type) { + private void getProperties(Object element, TypeHandler type) { // passed type is primitive - if (type instanceof PrimitiveType) { - ((PrimitiveType) type).storePropertyValue(element, this); + if (type instanceof PrimitiveTypeHandler) { + ((PrimitiveTypeHandler) type).storePropertyValue(element, this); } else { // passed type is compund ie. has child properties - ((CompoundType)type).getProperties().forEach(property -> { - if (property instanceof PrimitiveType) { + ((CompoundTypeHandler)type).getProperties().forEach(property -> { + if (property instanceof PrimitiveTypeHandler) { // recurse once more -> property is stored getProperties(element, property); } else { - CompoundType child = ((CompoundType) property); + CompoundTypeHandler child = ((CompoundTypeHandler) property); try { Object result = child.getGetter().invoke(element); getProperties(result, child); @@ -168,14 +168,14 @@ public class ContiguousList implements List { } data.position(elementIndices[index]); try { - if (type instanceof PrimitiveType) { - return (E)((PrimitiveType)type).transform(ValueReader.read(data)); + if (type instanceof PrimitiveTypeHandler) { + return (E)((PrimitiveTypeHandler)type).transform(ValueReader.read(data)); } // create a new instance of the list element type - E newInstance = (E) type.type.getDeclaredConstructor().newInstance(); + E newInstance = (E) type.getType().getDeclaredConstructor().newInstance(); // set the data - setProperties(newInstance, (CompoundType) type); + setProperties(newInstance, (CompoundTypeHandler) type); return newInstance; } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | @@ -184,14 +184,14 @@ public class ContiguousList implements List { } } - private void setProperties(Object element, CompoundType compoundType) { + private void setProperties(Object element, CompoundTypeHandler compoundType) { compoundType.getProperties().forEach(property -> { - if (property instanceof PrimitiveType) { - PrimitiveType type =((PrimitiveType) property); + if (property instanceof PrimitiveTypeHandler) { + PrimitiveTypeHandler type =((PrimitiveTypeHandler) property); type.setValue(element, ValueReader.read(data)); } else { try { - CompoundType p = (CompoundType) property; + CompoundTypeHandler p = (CompoundTypeHandler) property; // create a new instance of the property Object newInstance = p.getType().getDeclaredConstructor().newInstance(); diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/DoubleHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/DoubleHandler.java index 2ee47dc..74b169b 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/DoubleHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/DoubleHandler.java @@ -5,7 +5,7 @@ import java.lang.invoke.MethodHandle; /** * Stores a double value. */ -class DoubleHandler extends PrimitiveType { +class DoubleHandler extends PrimitiveTypeHandler { public DoubleHandler(MethodHandle getter, MethodHandle setter) { super(Double.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/FloatHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/FloatHandler.java index c38bcfb..ba2dedc 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/FloatHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/FloatHandler.java @@ -2,7 +2,7 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; -class FloatHandler extends PrimitiveType { +class FloatHandler extends PrimitiveTypeHandler { public FloatHandler(MethodHandle getter, MethodHandle setter) { super(Float.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/IntegerHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/IntegerHandler.java index 3eb31a2..2082cfd 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/IntegerHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/IntegerHandler.java @@ -2,7 +2,7 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; -class IntegerHandler extends PrimitiveType { +class IntegerHandler extends PrimitiveTypeHandler { public IntegerHandler(MethodHandle getter, MethodHandle setter) { super(Integer.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/LongHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/LongHandler.java index 9f9dc4d..c04de80 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/LongHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/LongHandler.java @@ -2,7 +2,7 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; -class LongHandler extends PrimitiveType { +class LongHandler extends PrimitiveTypeHandler { public LongHandler(MethodHandle getter, MethodHandle setter) { super(Long.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/PrimitiveType.java b/lib/src/main/java/nl/sanderhautvast/contiguous/PrimitiveTypeHandler.java similarity index 87% rename from lib/src/main/java/nl/sanderhautvast/contiguous/PrimitiveType.java rename to lib/src/main/java/nl/sanderhautvast/contiguous/PrimitiveTypeHandler.java index 89a527f..bf7d13d 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/PrimitiveType.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/PrimitiveTypeHandler.java @@ -4,15 +4,15 @@ import java.lang.invoke.MethodHandle; /** * Base class for handlers. Its responsibility is to read and write a property from the incoming object to the internal storage. - * + *

* Can be extended for types that you need to handle. - * + *

* A property handler is instantiated once per bean property and contains handles to the getter and setter methods * of the bean that it needs to call 'runtime' (after instantiation of the list), * ie. when a bean is added or retrieved from the list */ -public abstract class PrimitiveType extends Type { - public PrimitiveType(Class type, MethodHandle getter, MethodHandle setter) { +public abstract class PrimitiveTypeHandler extends TypeHandler { + public PrimitiveTypeHandler(Class type, MethodHandle getter, MethodHandle setter) { super(type, getter, setter); } @@ -63,14 +63,14 @@ public abstract class PrimitiveType extends Type { /** * Certain types can easily be stored as another known type, for instance * a BigDecimal can be stored as a String. - * - * The {@link PrimitiveType} for BigDecimal would in that case be responsible for turning the String + *

+ * The {@link PrimitiveTypeHandler} for BigDecimal would in that case be responsible for turning the String * into a BigDecimal. It can do that by overriding this method * * @param value raw value to transform to the desired output type * @return the transformed object */ - public Object transform(Object value){ + public Object transform(Object value) { return value; } } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/PropertyHandlerFactory.java b/lib/src/main/java/nl/sanderhautvast/contiguous/PropertyHandlerFactory.java index 2e928a5..0100923 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/PropertyHandlerFactory.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/PropertyHandlerFactory.java @@ -11,7 +11,7 @@ import java.util.Map; * Maps the propertyvalue type to a PropertyHandler */ final class PropertyHandlerFactory { - private static final Map, Class>> STANDARD_HANDLERS = new HashMap<>(); + private static final Map, Class>> TYPE_HANDLERS = new HashMap<>(); private PropertyHandlerFactory() { } @@ -38,16 +38,16 @@ final class PropertyHandlerFactory { } public static boolean isKnownType(Class type) { - return STANDARD_HANDLERS.containsKey(type); + return TYPE_HANDLERS.containsKey(type); } - public static PrimitiveType forType(Class type, MethodHandle getter, MethodHandle setter) { + public static PrimitiveTypeHandler forType(Class type, MethodHandle getter, MethodHandle setter) { try { - Class> appenderClass = STANDARD_HANDLERS.get(type); + Class> appenderClass = TYPE_HANDLERS.get(type); if (appenderClass == null) { throw new IllegalStateException("No Handler for " + type.getName()); } - return (PrimitiveType) appenderClass.getDeclaredConstructor(MethodHandle.class, MethodHandle.class) + return (PrimitiveTypeHandler) appenderClass.getDeclaredConstructor(MethodHandle.class, MethodHandle.class) .newInstance(getter, setter); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { @@ -55,14 +55,14 @@ final class PropertyHandlerFactory { } } - public static PrimitiveType forType(Class type) { + public static PrimitiveTypeHandler forType(Class type) { return forType(type, null, null); } /** * register a new TypeHandler that cannot be derived from bean properties */ - public static void register(Class type, Class> typehandler) { - STANDARD_HANDLERS.put(type, typehandler); + public static void register(Class type, Class> typehandler) { + TYPE_HANDLERS.put(type, typehandler); } } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/ShortHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/ShortHandler.java index 1d80c4a..95b5393 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/ShortHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/ShortHandler.java @@ -2,7 +2,7 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; -class ShortHandler extends PrimitiveType { +class ShortHandler extends PrimitiveTypeHandler { public ShortHandler(MethodHandle getter, MethodHandle setter) { super(Short.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/StringHandler.java b/lib/src/main/java/nl/sanderhautvast/contiguous/StringHandler.java index 0714c98..6bd837d 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/StringHandler.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/StringHandler.java @@ -2,7 +2,7 @@ package nl.sanderhautvast.contiguous; import java.lang.invoke.MethodHandle; -class StringHandler extends PrimitiveType { +class StringHandler extends PrimitiveTypeHandler { public StringHandler(MethodHandle getter, MethodHandle setter) { super(String.class, getter, setter); } diff --git a/lib/src/main/java/nl/sanderhautvast/contiguous/Type.java b/lib/src/main/java/nl/sanderhautvast/contiguous/TypeHandler.java similarity index 52% rename from lib/src/main/java/nl/sanderhautvast/contiguous/Type.java rename to lib/src/main/java/nl/sanderhautvast/contiguous/TypeHandler.java index d4cb2b0..54f5f36 100644 --- a/lib/src/main/java/nl/sanderhautvast/contiguous/Type.java +++ b/lib/src/main/java/nl/sanderhautvast/contiguous/TypeHandler.java @@ -8,15 +8,36 @@ import java.lang.invoke.MethodHandle; * I needed to abstract over handlers for 'primitives' (ie. long, but also Long, String..=> built-in types) and compound types (your own) * So this is the common ancestor. The respective functions are completely different. */ -public abstract class Type { +public abstract class TypeHandler { protected MethodHandle getter; // both can be null, if it's for a known ('primitive') type protected MethodHandle setter; - protected Class type; + private final Class type; - public Type(Class type, MethodHandle getter, MethodHandle setter) { + public TypeHandler(Class type, MethodHandle getter, MethodHandle setter) { this.type = type; this.getter = getter; this.setter = setter; } + + void setGetter(MethodHandle getter) { + this.getter = getter; + } + + public MethodHandle getGetter() { + return getter; + } + + public MethodHandle getSetter() { + return setter; + } + + void setSetter(MethodHandle setter) { + this.setter = setter; + } + + public Class getType() { + return type; + } + } diff --git a/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java b/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java index 8c18dce..dc09728 100644 --- a/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java +++ b/lib/src/test/java/nl/sanderhautvast/contiguous/ContiguousListTest.java @@ -131,7 +131,7 @@ public class ContiguousListTest { } assertEquals(100, beanList.size()); for (int i = 0; i < 100; i++) { - assertNull(beanList.get(i)); + assertNull(beanList.get(i).getName()); } }