Skip to content

Instantly share code, notes, and snippets.

@contextfw
Created March 23, 2011 19:09
Show Gist options
  • Select an option

  • Save contextfw/883737 to your computer and use it in GitHub Desktop.

Select an option

Save contextfw/883737 to your computer and use it in GitHub Desktop.
Idea for dynamic domain model in Java
public interface Struct {
<T> T get(FieldType<T> type);
<T> void set(FieldType<T> type, T value);
}
///////////////////////////////////////
public interface FieldType<T> {
String getFieldName();
Class<T> getType();
T get(Object value);
T validate(T value);
}
///////////////////////////////////////
public abstract class BasicType {
private final String name;
protected BasicType(String name) {
this.name = name;
}
public String getFieldName() {
return name;
}
}
///////////////////////////////////////
public class StringType extends BasicType implements FieldType<String> {
public StringType(String name) {
super(name);
}
public Class<String> getType() {
return String.class;
}
public String get(Object value) {
return value == null ? null : value.toString();
}
public String validate(String value) {
return value;
}
}
///////////////////////////////////////
public class IntType extends BasicType implements FieldType<Integer> {
public IntType(String name) {
super(name);
}
public Class<Integer> getType() {
return Integer.class;
}
public Integer get(Object value) {
if (value == null) return null;
if (value instanceof Integer) return (Integer) value;
return Integer.parseInt(value.toString());
}
public Integer validate(Integer value) {
return value;
}
}
///////////////////////////////////////
public class PositiveType extends IntType {
public PositiveType(String name) {
super(name);
}
public Integer validate(Integer value) {
if (value == null || value < 1) {
throw new IllegalArgumentException("Value must be positive");
}
return value;
}
}
///////////////////////////////////////
public class StructType extends BasicType implements FieldType<Struct> {
protected StructType(String name) {
super(name);
}
public Class<Struct> getType() {
return Struct.class;
}
public Struct get(Object value) {
if (value == null) return null;
if (value instanceof Struct) return (Struct) value;
throw new IllegalArgumentException("Could not convert");
}
public Struct validate(Struct value) {
return value;
}
}
///////////////////////////////////////
public class MapStruct implements Struct {
private Map<String, Object> values = new HashMap<String, Object>();
public <T> T get(FieldType<T> type) {
return type.get(values.get(type.getFieldName()));
}
public <T> void set(FieldType<T> type, T value) {
values.put(type.getFieldName(), type.validate(value));
}
}
///////////////////////////////////////
public class Address {
public static final FieldType<String> email = new StringType("email");
}
///////////////////////////////////////
public class User {
public static final FieldType<String> firstName = new StringType("firstName");
public static final FieldType<String> lastName = new StringType("lastName");
public static final FieldType<Integer> age = new PositiveType("age");
public static final FieldType<Struct> address = new StructType("address");
}
///////////////////////////////////////
import static User.*;
import static Address.*;
public class UserTest {
@Test
public void test() {
Struct user = new MapStruct();
// User user = new User();
user.set(firstName, "John");
// user.setFirstName("John");
user.set(lastName, "Bloggs");
// user.setLastName("Bloggs");
user.set(age, 40);
// user.setAge(40);
user.set(address, new MapStruct());
// user.setAddress(new Address());
user.get(address).set(email, "john.bloggs@test.com");
// user.getAddress().setEmail("john.bloggs@test.com");
System.out.println(user.get(firstName)
+ " " + user.get(lastName)
+ " " + user.get(address).get(email));
// System.out.println(user.getFirstName()
// + " " + user.getLastName();
// + " " + user.getAddress().getEmail();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment