Commits

John Marsden committed 97913aa

Remove List Tracking of Name Order from Object Imp (Speed improvement).

  • Participants
  • Parent commits 1775602

Comments (0)

Files changed (2)

src/main/java/cc/plural/jsonij/ObjectImp.java

 /**
- * Copyright (C) 2010-2011 J.W.Marsden
+ * Copyright (C) 2010-2014 J.W.Marsden
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  * use this file except in compliance with the License. You may obtain a copy of
  */
 package cc.plural.jsonij;
 
-import java.util.LinkedHashMap;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Set;
-
 import static cc.plural.jsonij.Constants.CLOSE_OBJECT;
 import static cc.plural.jsonij.Constants.NAME_SEPARATOR;
 import static cc.plural.jsonij.Constants.OPEN_OBJECT;
 import static cc.plural.jsonij.Constants.QUOTATION_MARK;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.Set;
 
 public class ObjectImp<CS extends CharSequence, V extends Value> extends Value implements java.util.Map<CS, V> {
 
      * Holds the Mapping Values for the Object
      */
     protected LinkedHashMap<CS, V> mapValue;
-    /**
-     * Holds the Key order so values can be extracted in the order they were
-     * added.
-     */
-    protected ArrayList<CS> valueOrder;
 
     /**
      * Default Constructor
      */
     public ObjectImp() {
         mapValue = new LinkedHashMap<CS, V>();
-        valueOrder = new ArrayList<CS>();
     }
 
     /* (non-Javadoc)
     /* (non-Javadoc)
      * @see java.util.List#get(java.lang.Integer)
      */
+    @Override
     public V get(int i) {
-        return mapValue.get((CS) valueOrder.get(i));
+        Object[] keyArray = mapValue.keySet().toArray();
+        if(keyArray != null && i >= 0 && i < keyArray.length) {
+            return mapValue.get((CS) keyArray[i]);
+        } else {
+            return null;
+        }
     }
 
     /* (non-Javadoc)
      */
     public V put(CS key, V value) {
         CS keySequence = (CS) key.toString();
-        valueOrder.add(keySequence);
         return mapValue.put(keySequence, value);
     }
 
      * @see java.util.Map#putAll(java.util.Map)
      */
     public void putAll(Map<? extends CS, ? extends V> m) {
-        for (CS k : m.keySet()) {
-            valueOrder.add(k);
-        }
         mapValue.putAll(m);
     }
 
      */
     public V remove(Object key) {
         CS keySequence = (CS) key.toString();
-        valueOrder.remove(keySequence);
         return mapValue.remove(keySequence);
     }
 
     /* (non-Javadoc)
      * @see java.util.Map#size()
      */
+    @Override
     public int size() {
         return mapValue.size();
     }
      */
     @Override
     public String toJSON() {
-        CS k = null;
-        Value v = null;
-        Iterator<CS> keyIterator = valueOrder.iterator();
+        CS k;
+        Value v;
+        Iterator<CS> keyIterator = mapValue.keySet().iterator();
         if (keyIterator.hasNext()) {
             StringBuilder json = new StringBuilder();
             json.append((char) OPEN_OBJECT);

src/test/java/cc/plural/jsonij/marshal/helpers/BasicTestObject.java

+
+package cc.plural.jsonij.marshal.helpers;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class BasicTestObject {
+    private int id;
+    public String value;   
+    private Map<String, Class<?>> classMapping;
+    
+    public BasicTestObject() {
+        id = -1;
+        value = null;
+        classMapping = new HashMap<String, Class<?>>();
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public Map<String, Class<?>> getClassMapping() {
+        return classMapping;
+    }
+
+    public void setClassMapping(Map<String, Class<?>> classMapping) {
+        this.classMapping = classMapping;
+    }
+}