扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在本页阅读全文(共2页)

public interface Storage extends Serializable ...{
IdEntity get( Class entityClass, Serializable id );
Serializable create ( IdEntity object ) throws CreateException;
IdEntity update ( IdEntity object ) throws UpdateException;
Serializable merge ( IdEntity object ) throws StorageException;
int delete( Class entityClass, Serializable id ) throws CreateException;
int delete( IdEntity object ) throws CreateException;
Collection findAll( Class entityClass );
}
public class MemoryStorage implements Storage ...{
private static final long serialVersionUID = 8611213748834904125L;

private Map memory = new HashMap();

private Map getEntityMap ( Class entityClass ) ...{
if (entityClass != null) ...{
Map tryMap = (Map) memory.get(entityClass);
if (tryMap == null) ...{
synchronized(memory) ...{
tryMap = new HashMap();
memory.put(entityClass, tryMap);
}
}
return tryMap;
} else ...{
return null;
}
}

private IdEntity intStore( Class entityClass, IdEntity object ) ...{
getEntityMap(entityClass).put(object.getId(), object);
return object;
}

public IdEntity get( Class entityClass, Serializable id ) ...{
if (entityClass != null && id != null) ...{
return (IdEntity) getEntityMap(entityClass).get(id);
} else ...{
return null;
}
}

public Serializable create ( IdEntity object ) throws CreateException ...{
if (object == null) ...{
throw new CreateException("Either given class or object was null");
}
if (object.getId() == null) ...{
throw new CreateException("Cannot store object with null id");
}
if (get(object.getClass(), object.getId()) != null) ...{
throw new DuplicateKeyException("Object with this id already exists.");
}
return intStore(object.getClass(), object).getId();
}

public IdEntity update ( IdEntity object ) throws UpdateException ...{
if (object == null) ...{
throw new UpdateException("Cannot update null object.");
}
if ( get(object.getClass(), object.getId())==null ) ...{
throw new UpdateException("Object to update not found.");
}
return intStore(object.getClass(), object);
}

public Serializable merge ( IdEntity object ) throws StorageException ...{
if (object == null) ...{
throw new StorageException("Cannot merge null object");
}
if (object.getId() == null || get(object.getClass(), object.getId())==null) ...{
return create(object);
} else ...{
return update(object).getId();
}
}

public int delete( Class entityClass, Serializable id ) throws CreateException ...{
try ...{
if (get(entityClass, id) != null) ...{
getEntityMap(entityClass).remove(id);
return 1;
} else ...{
return 0;
}
} catch (Exception e) ...{
throw new CreateException(e);
}
}

public int delete( IdEntity object ) throws CreateException ...{
if (object == null) ...{
throw new CreateException("Cannot delete null object");
}
return delete(object.getClass(), object.getId());
}

public Collection findAll( Class entityClass ) ...{
if (entityClass != null) ...{
return getEntityMap(entityClass).values();
} else ...{
return new ArrayList();
}
}

public void reset() ...{
this.memory = new HashMap();
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。