Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implemented proper equals hashcode and clone

  • Loading branch information...
commit 312b2370f4ff5461a29a5ba32edc666672935cef 1 parent 336f300
@michelegonella authored
View
43 zen-dataobject/src/main/java/com/nominanuda/dataobject/AbstractDataStruct.java
@@ -17,6 +17,8 @@
import static com.nominanuda.lang.Check.illegalargument;
+import static com.nominanuda.dataobject.DataStructHelper.STRUCT;
+
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -29,7 +31,6 @@
public abstract class AbstractDataStruct<K> implements DataStruct, PropertyBag<K> {
- protected static final DataStructHelper structHelper = new DataStructHelper();
protected final DataStruct parent;
public AbstractDataStruct(DataStruct parent) {
@@ -89,7 +90,7 @@ public K checkKey(K key) throws IllegalArgumentException {
}
public boolean isPrimitiveOrNull(Object o) {
- return structHelper.isPrimitiveOrNull(o);
+ return STRUCT.isPrimitiveOrNull(o);
}
public DataArray asArray() throws ClassCastException {
return (DataArray)this;
@@ -131,7 +132,7 @@ public DataObject getObject(K k) throws IllegalArgumentException {
public void setPathProperty(String path, @Nullable Object value) {
String[] pathBits = path.split("\\.");
int len = pathBits.length;
- explodePath(path).setProperty(pathBits[len-1], value);
+ explodePath(path).put(pathBits[len-1], value);
}
public void setOrPushPathProperty(String path, @Nullable Object value) {
String[] pathBits = path.split("\\.");
@@ -159,14 +160,14 @@ public void setOrPushPathProperty(String path, @Nullable Object value) {
return _target;
}
- public void setProperty(Object key, @Nullable Object value) {
+ private void setProperty(Object key, @Nullable Object value) {
asObjectKeyedDataStruct().put(key, value);
}
public void setOrPushProperty(Object key, @Nullable Object value) {
PropertyBag<Object> _this = asObjectKeyedDataStruct();
if(_this.exists(key)) {
Object cur = _this.get(key);
- if(structHelper.isDataArray(cur)) {
+ if(STRUCT.isDataArray(cur)) {
((DataArray)cur).add(value);
} else {
DataArray darr = new DataArrayImpl();
@@ -191,10 +192,7 @@ private Object toStringOrIntKey(String s) {
private PropertyBag<Object> asObjectKeyedDataStruct(DataStruct ds) {
return (PropertyBag<Object>)ds;
}
- @Override
- public String toString() {
- return structHelper.toJsonString(this);
- }
+
@SuppressWarnings("unchecked")
public Object getPathSafe(String path) {
PropertyBag<Object> _target = asObjectKeyedDataStruct();
@@ -308,4 +306,31 @@ public DataObject putObject(K key, DataObject o) {
public DataArray putArray(K key, DataArray o) {
return (DataArray)put(key, o);
}
+
+ protected void onMutate() {
+ json = null;
+ }
+ @Override
+ protected Object clone() throws CloneNotSupportedException {
+ return STRUCT.clone(this);
+ }
+
+ @Override
+ public String toString() {
+ return json != null ? json : STRUCT.toJsonString(this);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return STRUCT.equals(this, obj);
+ }
+
+ private String json = null;
+ @Override
+ public int hashCode() {
+ if(json == null) {
+ json = toString();
+ }
+ return json.hashCode();
+ }
}
View
8 zen-dataobject/src/main/java/com/nominanuda/dataobject/DataArrayImpl.java
@@ -49,6 +49,7 @@ public Object put(Integer i, Object v) {
Object obj = isPrimitiveOrNull(v)
? v : ((AbstractDataStruct)v).cloneStruct(this);
l.set(ensureRoom(i),obj);
+ onMutate();
return obj;
}
@@ -68,10 +69,15 @@ private int ensureRoom(int i) {
}
public Object remove(Integer i) {
- return l.remove(i);
+ Object o = l.remove(i);
+ if(o != null) {
+ onMutate();
+ }
+ return o;
}
public Object add(Object v) {
+ onMutate();
return put(getLength(), v);
}
View
7 zen-dataobject/src/main/java/com/nominanuda/dataobject/DataObjectImpl.java
@@ -40,6 +40,7 @@ private DataObjectImpl(Map<String, ? super Object> o, DataStruct parent) {
public Object put(String k, Object v) {
Object o = cloneInternal(v,this);
m.put(checkKey(k), o);
+ onMutate();
return o;
}
@@ -52,7 +53,11 @@ public boolean exists(String k) {
}
public Object remove(String k) {
- return m.remove(k);
+ Object o = m.remove(k);
+ if(o != null) {
+ onMutate();
+ }
+ return o;
}
public List<String> getKeys() {
View
2  zen-dataobject/src/main/java/com/nominanuda/dataobject/DataStruct.java
@@ -19,7 +19,7 @@
import com.nominanuda.code.Nullable;
-public interface DataStruct {
+public interface DataStruct extends Cloneable {
Pattern VALID_OBJ_KEY = Pattern.compile("[\\$_A-Za-z][\\$_A-Za-z0-9]*");
@Deprecated
@Nullable DataStruct getParent();
View
5 zen-dataobject/src/main/java/com/nominanuda/dataobject/LazyDataArray.java
@@ -251,11 +251,6 @@ public void setOrPushPathProperty(String path, Object value) {
delegee.setOrPushPathProperty(path, value);
}
- public void setProperty(Object key, Object value) {
- explode();
- delegee.setProperty(key, value);
- }
-
public void setOrPushProperty(Object key, Object value) {
explode();
delegee.setOrPushProperty(key, value);
View
5 zen-dataobject/src/main/java/com/nominanuda/dataobject/LazyDataObject.java
@@ -130,11 +130,6 @@ public void setOrPushPathProperty(String path, Object value) {
delegee.setOrPushPathProperty(path, value);
}
- public void setProperty(Object key, Object value) {
- explode();
- delegee.setProperty(key, value);
- }
-
public void setOrPushProperty(Object key, Object value) {
explode();
delegee.setOrPushProperty(key, value);
View
2  zen-dataobject/src/main/java/com/nominanuda/dataobject/PropertyBag.java
@@ -34,7 +34,7 @@
boolean isPrimitiveOrNull(@Nullable Object o);
void setPathProperty(String path, @Nullable Object value);
void setOrPushPathProperty(String path, @Nullable Object value);
- void setProperty(Object key, @Nullable Object value);
+ //void setProperty(Object key, @Nullable Object value);
void setOrPushProperty(Object key, @Nullable Object value);
Object getPathSafe(String path);
@Nullable String getString(K key) throws ClassCastException;
View
63 zen-dataobject/src/test/java/com/nominanuda/dataobject/HashCodeEqualsTest.java
@@ -0,0 +1,63 @@
+package com.nominanuda.dataobject;
+
+import static org.junit.Assert.*;
+
+import java.util.HashSet;
+
+import org.junit.Test;
+
+import static com.nominanuda.dataobject.DataStructHelper.STRUCT;
+
+public class HashCodeEqualsTest {
+
+ @Test
+ public void test1() {
+ HashSet<DataStruct> s = new HashSet<DataStruct>();
+ DataObject o1 = STRUCT.newObject();
+ s.add(o1);
+ assertEquals(1, s.size());
+ DataObject o2 = STRUCT.newObject();
+ s.add(o2);
+ assertEquals(1, s.size());
+
+ }
+
+ @Test
+ public void test2() {
+ HashSet<DataStruct> s = new HashSet<DataStruct>();
+ DataObject o1 = STRUCT.newObject().with("a", 1);
+ s.add(o1);
+ assertEquals(1, s.size());
+ DataObject o2 = STRUCT.newObject();
+ s.add(o2);
+ assertEquals(2, s.size());
+
+ }
+
+ @Test
+ public void test3() {
+ HashSet<DataStruct> s = new HashSet<DataStruct>();
+ DataObject o1 = STRUCT.newObject();
+ s.add(o1);
+ assertEquals(1, s.size());
+ o1.with("a", 1);
+ assertEquals(1, s.size());
+ assertSame(o1, s.iterator().next());
+ }
+
+ @Test
+ public void test4() {
+ HashSet<DataStruct> s = new HashSet<DataStruct>();
+ DataObject o1 = STRUCT.newObject().with("a", 1);
+ s.add(o1);
+ DataObject o2 = STRUCT.newObject();
+ s.add(o2);
+ assertTrue(s.contains(o2));
+ assertEquals(2, s.size());
+ assertFalse(o1.equals(o2));
+ o2.with("a", 1);
+ assertTrue(s.contains(o2));
+ assertEquals(o1, o2);
+ assertEquals(2, s.size());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.