From 688634ae5a5aaf663159032e67d2132ea61c5d5f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 4 May 2014 17:37:41 +0200 Subject: Implement "save" and "load" --- src/net/pacien/util/CleanJSONObject.java | 48 ++++++++++++++++++++++++++++++++ src/net/pacien/util/IntrinsicMap.java | 23 +++++++++------ 2 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 src/net/pacien/util/CleanJSONObject.java (limited to 'src/net') diff --git a/src/net/pacien/util/CleanJSONObject.java b/src/net/pacien/util/CleanJSONObject.java new file mode 100644 index 0000000..0982ca8 --- /dev/null +++ b/src/net/pacien/util/CleanJSONObject.java @@ -0,0 +1,48 @@ +package net.pacien.util; + +import java.util.Collection; +import java.util.Map; + +import org.json.simple.JSONObject; + +public class CleanJSONObject extends JSONObject { + + /** + * + */ + private static final long serialVersionUID = 6700060746446264070L; + + public CleanJSONObject() { + super(); + } + + @Override + public Object put(final String key, final Object value) { + if (value == null) { + return null; + } + return super.put(key, value); + } + + public Object put(final String key, final Map map) { + if (map.isEmpty()) { + return null; + } + return super.put(key, map); + } + + public Object put(final String key, final Collection collection) { + if (collection.isEmpty()) { + return null; + } + return super.put(key, collection); + } + + public Object put(final String key, final Object[] array) { + if (array.length == 0) { + return null; + } + return super.put(key, array); + } + +} diff --git a/src/net/pacien/util/IntrinsicMap.java b/src/net/pacien/util/IntrinsicMap.java index a68dcbe..fe59ac2 100644 --- a/src/net/pacien/util/IntrinsicMap.java +++ b/src/net/pacien/util/IntrinsicMap.java @@ -3,12 +3,13 @@ */ package net.pacien.util; +import java.util.Collection; import java.util.HashMap; import java.util.Map; /** * @author pacien - * + * */ public class IntrinsicMap extends HashMap { @@ -27,14 +28,14 @@ public class IntrinsicMap extends HashMap { /** * @param initialCapacity */ - public IntrinsicMap(int initialCapacity) { + public IntrinsicMap(final int initialCapacity) { super(initialCapacity); } /** * @param m */ - public IntrinsicMap(Map m) { + public IntrinsicMap(final Map m) { super(m); } @@ -42,18 +43,24 @@ public class IntrinsicMap extends HashMap { * @param initialCapacity * @param loadFactor */ - public IntrinsicMap(int initialCapacity, float loadFactor) { + public IntrinsicMap(final int initialCapacity, final float loadFactor) { super(initialCapacity, loadFactor); } - + @SuppressWarnings("unchecked") - public V put(V value) { + public V put(final V value) { return super.put((K) value.getKey(), value); } - + @Override - public V put(K key, V value) { + public V put(final K key, final V value) { return this.put(value); } + public void putAll(final Collection collection) { + for (final V value : collection) { + this.put(value); + } + } + } -- cgit v1.2.3