You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2017/11/07 20:00:17 UTC

svn commit: r1814534 - /pivot/trunk/web/src/org/apache/pivot/web/QueryDictionary.java

Author: rwhitcomb
Date: Tue Nov  7 20:00:17 2017
New Revision: 1814534

URL: http://svn.apache.org/viewvc?rev=1814534&view=rev
Log:
Code cleanup:  Made some private methods in QueryDictionary to remove duplicated
code all through this class.
Fixed what looked like a bug in "remove(String,int)" where the return value was
correct, but nothing was actually removed from the ArrayList referenced by the key.

Modified:
    pivot/trunk/web/src/org/apache/pivot/web/QueryDictionary.java

Modified: pivot/trunk/web/src/org/apache/pivot/web/QueryDictionary.java
URL: http://svn.apache.org/viewvc/pivot/trunk/web/src/org/apache/pivot/web/QueryDictionary.java?rev=1814534&r1=1814533&r2=1814534&view=diff
==============================================================================
--- pivot/trunk/web/src/org/apache/pivot/web/QueryDictionary.java (original)
+++ pivot/trunk/web/src/org/apache/pivot/web/QueryDictionary.java Tue Nov  7 20:00:17 2017
@@ -21,6 +21,7 @@ import java.util.Iterator;
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.Dictionary;
 import org.apache.pivot.collections.HashMap;
+import org.apache.pivot.collections.Sequence;
 
 /**
  * Represents a collection of keyed data associated with a query. Allows
@@ -34,12 +35,25 @@ public final class QueryDictionary imple
         this.caseSensitiveKeys = caseSensitiveKeys;
     }
 
+    private String getSearchKey(String key) {
+        return caseSensitiveKeys ? key : key.toLowerCase();
+    }
+
+    private ArrayList<String> getListCheckIndex(String key, int index) {
+        String searchKey = getSearchKey(key);
+
+        ArrayList<String> list = map.get(searchKey);
+        // e.g if index = 0 and length = 0, throw an exception
+        if (list == null || list.getLength() <= index) {
+            throw new IndexOutOfBoundsException("No list for search key or list length <= index " + index);
+        }
+
+        return list;
+    }
+
     @Override
     public String get(final String key) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        String searchKey = getSearchKey(key);
 
         ArrayList<String> list = map.get(searchKey);
         if (list != null && list.getLength() > 0) {
@@ -50,25 +64,14 @@ public final class QueryDictionary imple
     }
 
     public String get(final String key, int index) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
-
-        ArrayList<String> list = map.get(searchKey);
-        if (list == null || list.getLength() <= index) {
-            throw new IndexOutOfBoundsException();
-        }
+        ArrayList<String> list = getListCheckIndex(key, index);
 
         return list.get(index);
     }
 
     @Override
     public String put(final String key, final String value) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        String searchKey = getSearchKey(key);
 
         ArrayList<String> list = new ArrayList<>();
         list.add(value);
@@ -82,10 +85,7 @@ public final class QueryDictionary imple
     }
 
     public int add(final String key, final String value) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        String searchKey = getSearchKey(key);
 
         ArrayList<String> list = map.get(searchKey);
         if (list == null) {
@@ -98,27 +98,14 @@ public final class QueryDictionary imple
     }
 
     public void insert(final String key, final String value, int index) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
-
-        ArrayList<String> list = map.get(searchKey);
-
-        // e.g if index = 0 and length = 0, throw an exception
-        if (list == null || list.getLength() <= index) {
-            throw new IndexOutOfBoundsException();
-        }
+        ArrayList<String> list = getListCheckIndex(key, index);
 
         list.insert(value, index);
     }
 
     @Override
     public String remove(final String key) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        String searchKey = getSearchKey(key);
 
         ArrayList<String> list = map.remove(searchKey);
         if (list != null && list.getLength() > 0) {
@@ -129,17 +116,14 @@ public final class QueryDictionary imple
     }
 
     public String remove(final String key, int index) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        ArrayList<String> list = getListCheckIndex(key, index);
 
-        ArrayList<String> list = map.get(searchKey);
-        if (list == null || list.getLength() <= index) {
-            throw new IndexOutOfBoundsException();
+        Sequence<String> removed = list.remove(index, 1);
+        if (removed != null && removed.getLength() > 0) {
+            return removed.get(0);
         }
 
-        return list.get(index);
+        return null;
     }
 
     public void clear() {
@@ -148,25 +132,16 @@ public final class QueryDictionary imple
 
     @Override
     public boolean containsKey(final String key) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        String searchKey = getSearchKey(key);
 
         return map.containsKey(searchKey);
     }
 
     public int getLength(final String key) {
-        String searchKey = key;
-        if (!caseSensitiveKeys) {
-            searchKey = searchKey.toLowerCase();
-        }
+        String searchKey = getSearchKey(key);
 
         ArrayList<String> list = map.get(searchKey);
-        if (list == null) {
-            return 0;
-        }
-        return list.getLength();
+        return (list == null) ? 0 : list.getLength();
     }
 
     @Override