0

I have modeled my class in 2 different ways: one using ? and the other using T extends SomeClass. I would like to know which approach is the right way to do it. Basically I have an object and I would like to persist it in my datastore. From the client I just get a Customer, I add some metaDataInfo to that object and then select the Id/key for the object and then persist it using key and object. I want to make sure the object that I persist has metaInfo and not just any object.

Option1 using T extends SomeClass:

public class Client {

public static void main(String[] args) {
    Customer customer = new Customer();
    Persister.save(customer);
}

static class Persister {
    static void save(Customer customer) {
        EntityWithMetaDataImpl<Customer> entityWithMetaData = new EntityWithMetaDataImpl<>("metadata", customer);
        String key = "key";
        PersistentEntity<EntityWithMetaDataImpl<Customer>> impl = new PersistentEntity<>(key, entityWithMetaData);
        ActualPersister.save(impl);
    }
}

static class ActualPersister {
    static <T extends EntityWithMetaData> void save(PersistentEntity<T> entity) {
        // save record with entity.getObjectKey() and
        // entity.getActualObject();
    }
}
}

public class Customer {
  String name;
  public String getName() { return name;}
  public void setName(String name) { this.name = name; }
}

public interface EntityWithMetaData {   
  public String getMetaData();
}

public class EntityWithMetaDataImpl<T> implements EntityWithMetaData {
  private T actualObject;
  private String metaData;

  EntityWithMetaDataImpl(String metaData, T actualObject) {
    this.metaData = metaData;
    this.actualObject = actualObject;
  }

public T getActualObject() {
    return actualObject;
}

@Override
public String getMetaData() {
    return metaData;
}
}

public class PersistentEntity<T extends EntityWithMetaData> {
String key;
T actualObject;

PersistentEntity(String key, T entity){
    this.key = key;
    this.actualObject = entity;
}

public String getObjectKey() { return key;}

public T getActualObject() { return actualObject; }

}

Option2 using ?:

public class Client {

public static void main(String[] args) {
    Customer customer = new Customer();
    Persister.save(customer);
}

static class Persister {
    static void save(Customer customer) {
        EntityWithMetaData<Customer> entityWithMetaData = new EntityWithMetaData<>("metadata", customer);
        String key = "key";
        PersistentEntity impl = new PersistentEntity(key, entityWithMetaData);
        ActualPersister.save(impl);
    }
}

static class ActualPersister {
    static void save(PersistentEntity persisterEntity) {
        // save record with persisterEntity.getObjectKey() and
        // persisterEntity.getActualObject();
    }
}
}
public class EntityWithMetaData<T>{

private T actualObject;
private String metaData;

EntityWithMetaData(String metaData, T actualObject) {
    this.metaData = metaData;
    this.actualObject = actualObject;
}

public T getActualObject() {
    return actualObject;
}

public String getMetaData() {
    return metaData;
}

}

public class PersistentEntity {

String key;
EntityWithMetaData<?> actualObject;

PersistentEntity(String key, EntityWithMetaData<?> entity){
    this.key = key;
    this.actualObject = entity;
}

public String getObjectKey() {
    return key;
}

public EntityWithMetaData<?> getActualObject() {
    return actualObject;
}
}
anuj
  • 201
  • 5
  • 16
  • 1
    Have a look at [the Generics trail](https://docs.oracle.com/javase/tutorial/java/generics/) – MadProgrammer Oct 16 '15 at 04:59
  • I'd say that any version permitting you to do what you don't want to have permitted by the compiler or, at the worst, by runtime checks is not what you want. Did you try? – laune Oct 16 '15 at 05:29

0 Answers0