Restricting types not related through inheritance without using instanceOf
by creating own class hierarchy. It needs to interact with a key value data store, will convert to object before putting, but needs to convert back to fields after getting from DB with Function<Object,Field>
. Something about this with regards to inheritance doesn't seem right, although it works.
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
/**
* Field, placeholder for a type that can be stored in db
*/
@EqualsAndHashCode
public class Field<T> {
@Getter
private T featureValue;
Field(@NonNull T value) {
this.featureValue = value;
}
public static class Factory {
}
}
//convert object to feature value (since ddb returns Map<String, Object> on Item.asMap)
//convert particular type of object to feature value (say Integer, String)
//abstract Field from(T value);
//convert feature value to particular type, call from drvd (from base => obj)
//enum or getEnum for some type expressed as string factory
/**
* Field for a boolean type
*/
public class BooleanField extends Field<Boolean> {
public BooleanField(Boolean value) {
super(value);
}
public static BooleanField fromObject(Object value) {
return new BooleanField((Boolean) value);
}
}
import java.util.List;
/**
* Field for a list data type, holds a list of Field
*/
public class ListField extends Field<List<Field>> {
public ListField(List<Field> value) {
super(value);
}
public static ListField fromObject(Object value) {
return new ListField((List<Field>) value);
}
}
import java.util.Map;
/**
* Field for a map type, holds a map of String to Field
*/
public class MapField extends Field<Map<String, Field>> {
public MapField(Map<String, Field> value) {
super(value);
}
public static MapField fromObject(Object value) {
return new MapField((Map<String, Field>) value);
}
}
/**
* Field for a Numeric type
*/
public class NumericField extends Field<Number> {
public NumericField(Number value) {
super(value);
}
public static NumericField fromObject(Object value) {
return new NumericField((Number) value);
}
}
import java.util.Set;
/**
* Field for a set data type
*/
public class SetField extends Field<Set<Field>> {
public SetField(Set<Field> value) {
super(value);
}
public static SetField fromObject(Object value) {
return new SetField((Set<Field>) value);
}
}
/**
* Field for a string type
*/
public class StringField extends Field<String> {
public StringField(String value) {
super(value);
}
public static StringField fromObject(Object value) {
return new StringField((String) value);
}
}
instanceOf
you get stuck. see this question : stackoverflow.com/questions/34893297/… or is there any other way to know what type of object it is? – chillworld Jan 20 at 7:13