diff --git a/src/main/java/cn/devezhao/persist4j/Record.java b/src/main/java/cn/devezhao/persist4j/Record.java index 47d9f9e..5d0f2f4 100644 --- a/src/main/java/cn/devezhao/persist4j/Record.java +++ b/src/main/java/cn/devezhao/persist4j/Record.java @@ -8,6 +8,7 @@ import java.util.Iterator; import cn.devezhao.persist4j.engine.ID; +import cn.devezhao.persist4j.engine.NullValue; /** * 记录 @@ -18,73 +19,236 @@ */ public interface Record extends Cloneable, Serializable { + /** + * 实体 + * + * @return + */ Entity getEntity(); + /** + * 编辑用户 + * + * @return + */ ID getEditor(); + /** + * 主键 + * + * @return + */ ID getPrimary(); + /** + * @param key + * @param value + * @return + */ Record setID(String key, ID value); + /** + * @param key + * @return + */ ID getID(String key); + /** + * @param key + * @param values + * @return + */ Record setIDArray(String key, ID[] values); + /** + * @param key + * @return + */ ID[] getIDArray(String key); + /** + * @param key + * @param value + * @return + */ Record setChar(String key, Character value); + /** + * @param key + * @return + */ Character getChar(String key); + /** + * @param key + * @param value + * @return + */ Record setString(String key, String value); + /** + * @param key + * @return + */ String getString(String key); + /** + * @param key + * @param value + * @return + */ Record setInt(String key, Integer value); + /** + * @param key + * @return + */ Integer getInt(String key); + /** + * @param key + * @param value + * @return + */ Record setDouble(String key, Double value); + /** + * @param key + * @return + */ Double getDouble(String key); + /** + * @param key + * @param value + * @return + */ Record setDecimal(String key, BigDecimal value); + /** + * @param key + * @return + */ BigDecimal getDecimal(String key); + /** + * @param key + * @param value + * @return + */ Record setLong(String key, Long value); + /** + * @param key + * @return + */ Long getLong(String key); + /** + * @param key + * @param value + * @return + */ Record setDate(String key, Date value); + /** + * @param key + * @return + */ Date getDate(String key); + /** + * @param key + * @param value + * @return + */ Record setBoolean(String key, Boolean value); + /** + * @param key + * @return + */ Boolean getBoolean(String key); + /** + * @param key + * @param stream + * @return + */ Record setBinary(String key, InputStream stream); + /** + * @param key + * @return + */ InputStream getBinary(String key); + /** + * @param key + * @param value + * @return + */ Record setReader(String key, Reader value); + /** + * @param key + * @param value + * @return + */ Record setReader(String key, String value); + /** + * @param key + * @return + */ Reader getReader(String key); + /** + * @param key + * @param value + * @return + */ Record setObjectValue(String key, Object value); + /** + * @param key + * @return + */ Object getObjectValue(String key); + /** + * @param key + * @return + */ boolean hasValue(String key); + /** + * @param key + * @param includeNullValue {@link NullValue} + * @return + * @see #setNull(String) + */ + boolean hasValue(String key, boolean includeNullValue); + + /** + * @param key + * @return + */ Object removeValue(String key); + /** + * @param key + * @return + */ Object setNull(String key); + /** + * @return + */ Iterator getAvailableFieldIterator(); + /** + * @return + */ Record clone(); } diff --git a/src/main/java/cn/devezhao/persist4j/engine/StandardRecord.java b/src/main/java/cn/devezhao/persist4j/engine/StandardRecord.java index f2fc1e4..c2f0ac2 100644 --- a/src/main/java/cn/devezhao/persist4j/engine/StandardRecord.java +++ b/src/main/java/cn/devezhao/persist4j/engine/StandardRecord.java @@ -48,7 +48,7 @@ public class StandardRecord implements Record { protected Map recordMap = new CaseInsensitiveMap<>(); /** - * Serializable + * for Serializable */ protected StandardRecord() { this.entity = null; @@ -56,8 +56,6 @@ protected StandardRecord() { } /** - * Create a new StandardRecordImpl - * * @param entity */ public StandardRecord(Entity entity) { @@ -65,43 +63,47 @@ public StandardRecord(Entity entity) { } /** - * Create a new StandardRecordImpl - * * @param entity * @param editor */ public StandardRecord(Entity entity, ID editor) { - Validate.notNull(entity, "entity must not be null"); + Validate.notNull(entity, "'entity' must not be null"); this.entity = entity; this.editor = editor; } - + + @Override public Entity getEntity() { return entity; } - + + @Override public ID getEditor() { return editor; } - + + @Override public ID getPrimary() { return getID(entity.getPrimaryField().getName()); } - + + @Override public ID getID(String key) { return (ID) getObject(key, ID.class); } - + + @Override public Record setID(String key, ID value) { if (entity.getPrimaryField().getName().equalsIgnoreCase(key) && getPrimary() != null) { throw new IllegalStateException( - "primary field value already exists, can not be re-settings"); + "Primary field value already exists, can not be re-settings"); } setObject(key, value); return this; } - + + @Override public ID[] getIDArray(String key) { Object v = recordMap.get(key); if (v == null) { @@ -109,91 +111,111 @@ public ID[] getIDArray(String key) { } return (ID[]) v; } - + + @Override public Record setIDArray(String key, ID[] value) { setObject(key, value); return this; } - + + @Override public Character getChar(String key) { return (Character) getObject(key, Character.class); } - + + @Override public Record setChar(String key, Character value) { setObject(key, value); return this; } - + + @Override public String getString(String key) { return (String) getObject(key, String.class); } - + + @Override public Record setString(String key, String value) { setObject(key, value); return this; } - + + @Override public Integer getInt(String key) { return (Integer) getObject(key, Integer.class); } - + + @Override public Record setInt(String key, Integer value) { setObject(key, value); return this; } - + + @Override public Double getDouble(String key) { return (Double) getObject(key, Double.class); } - + + @Override public Record setDouble(String key, Double value) { setObject(key, value); return this; } - + + @Override public BigDecimal getDecimal(String key) { return (BigDecimal) getObject(key, BigDecimal.class); } - + + @Override public Record setDecimal(String key, BigDecimal value) { setObject(key, value); return this; } + @Override public Long getLong(String key) { return (Long) getObject(key, Long.class); } + @Override public Record setLong(String key, Long value) { setObject(key, value); return this; } + @Override public Date getDate(String key) { return (Date) getObject(key, Date.class); } + @Override public Record setDate(String key, Date value) { setObject(key, value); return this; } + @Override public Boolean getBoolean(String key) { return (Boolean) getObject(key, Boolean.class); } + @Override public Record setBoolean(String key, Boolean value) { setObject(key, value); return this; } + @Override public Reader getReader(String key) { return null; } + @Override public Record setReader(String key, Reader value) { - if (value == null) + if (value == null) { return this; + } String content = null; try { @@ -204,6 +226,7 @@ public Record setReader(String key, Reader value) { return setReader(key, content); } + @Override public Record setReader(String key, String value) { if (value == null) { return this; @@ -212,52 +235,63 @@ public Record setReader(String key, String value) { return this; } + @Override public InputStream getBinary(String key) { throw new UnsupportedOperationException(); } + @Override public Record setBinary(String key, InputStream value) { setObject(key, value); return this; } - public Record getRecord(String key) { - throw new UnsupportedOperationException(); - } - - public Record setRecord(String key, Record value) { - throw new UnsupportedOperationException(); - } - + @Override public Object getObjectValue(String key) { return recordMap.get(key); } + @Override public Record setObjectValue(String key, Object value) { - if (key == null || value == null) + if (key == null || value == null) { return this; + } recordMap.put(key, value); return this; } + @Override public boolean hasValue(String key) { - return recordMap.containsKey(key); + return hasValue(key, true); + } + + @Override + public boolean hasValue(String key, boolean includeNullValue) { + boolean has = recordMap.containsKey(key); + if (!has) { + return false; + } + return includeNullValue ? true : !NullValue.is(recordMap.get(key)); } + @Override public Object removeValue(String key) { return recordMap.remove(key); } + @Override public Object setNull(String key) { Object old = recordMap.get(key); recordMap.put(key, new NullValue()); return old; } + @Override public Iterator getAvailableFieldIterator() { return recordMap.keySet().iterator(); } + @Override public Record clone() { StandardRecord o = null; try { @@ -287,7 +321,7 @@ protected void setObject(String key, Object value) { if (LOG.isDebugEnabled()) { if (recordMap.containsKey(key)) { - LOG.warn("update field [" + key + "] value. OLD: " + recordMap.get(key) + ", NEW: " + value); + LOG.warn("Update field [" + key + "] value. OLD: " + recordMap.get(key) + ", NEW: " + value); } } recordMap.put(key, value); @@ -307,7 +341,7 @@ protected Object getObject(String key, Class clazz) { } throw new PersistException( - "can't cast field [ " + key + " ] value type " + v.getClass() + " to " + clazz); + "Can't cast field [ " + key + " ] value type " + v.getClass() + " to " + clazz); } protected void checkReferenceValue(Field field, Object value) {