Author: desruisseaux
Date: Fri May 31 21:32:57 2013
New Revision: 1488404

URL: http://svn.apache.org/r1488404
Log:
Move helper methods as member of OptionKey rather than static methods in 
Options.
The initial intend was to hide those helper methods from public API. However 
they
are harmless, so we may have more to win by keeping the code a little bit 
simpler
instead.

Removed:
    
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/util/Options.java
    
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/internal/util/OptionsTest.java
Modified:
    
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
    
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
    
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
    
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java

Modified: 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java?rev=1488404&r1=1488403&r2=1488404&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/setup/OptionKey.java
 [UTF-8] Fri May 31 21:32:57 2013
@@ -16,6 +16,8 @@
  */
 package org.apache.sis.setup;
 
+import java.util.Map;
+import java.util.HashMap;
 import java.nio.ByteBuffer;
 import java.io.Serializable;
 import java.io.ObjectStreamException;
@@ -129,6 +131,52 @@ public class OptionKey<T> implements Ser
     }
 
     /**
+     * Returns the option value in the given map for this key, or {@code null} 
if none.
+     * This is a convenience method for implementors, which can be used as 
below:
+     *
+     * {@preformat java
+     *     public <T> T getOption(final OptionKey<T> key) {
+     *         ArgumentChecks.ensureNonNull("key", key);
+     *         return key.getValueFrom(options);
+     *     }
+     * }
+     *
+     * @param  options The map where to search for the value, or {@code null} 
if not yet created.
+     * @return The current value in the map for the this option, or {@code 
null} if none.
+     */
+    public T getValueFrom(final Map<OptionKey<?>,?> options) {
+        return (options != null) ? type.cast(options.get(this)) : null;
+    }
+
+    /**
+     * Sets a value for this option key in the given map, or in a new map if 
the given map is {@code null}.
+     * This is a convenience method for implementors, which can be used as 
below:
+     *
+     * {@preformat java
+     *     public <T> void setOption(final OptionKey<T> key, final T value) {
+     *         ArgumentChecks.ensureNonNull("key", key);
+     *         options = key.setValueInto(options, value);
+     *     }
+     * }
+     *
+     * @param  options The map where to set the value, or {@code null} if not 
yet created.
+     * @param  value   The new value for the given option, or {@code null} for 
removing the value.
+     * @return The given map of options, or a new map if the given map was 
null. The returned value
+     *         may be null if the given map and the given value are both null.
+     */
+    public Map<OptionKey<?>,Object> setValueInto(Map<OptionKey<?>,Object> 
options, final T value) {
+        if (value != null) {
+            if (options == null) {
+                options = new HashMap<>();
+            }
+            options.put(this, value);
+        } else if (options != null) {
+            options.remove(this);
+        }
+        return options;
+    }
+
+    /**
      * Returns {@code true} if the given object is an instance of the same 
class having the same name and type.
      *
      * @param object The object to compare with this {@code OptionKey} for 
equality.

Modified: 
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java?rev=1488404&r1=1488403&r2=1488404&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/setup/OptionKeyTest.java
 [UTF-8] Fri May 31 21:32:57 2013
@@ -16,11 +16,13 @@
  */
 package org.apache.sis.setup;
 
+import java.util.Map;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.apache.sis.test.Assert.*;
 import static org.apache.sis.setup.OptionKey.*;
+import static org.apache.sis.test.TestUtilities.getSingleton;
 
 
 /**
@@ -33,6 +35,31 @@ import static org.apache.sis.setup.Optio
  */
 public final strictfp class OptionKeyTest extends TestCase {
     /**
+     * Tests the {@link OptionKey#getValueFrom(Map)} and {@link 
OptionKey#setValueInto(Map, Object)}
+     * methods with null arguments.
+     */
+    @Test
+    public void testNullArguments() {
+        assertNull(URL_ENCODING.getValueFrom(null));
+        assertNull(URL_ENCODING.setValueInto(null, null));
+    }
+
+    /**
+     * Tests the {@link OptionKey#setValueInto(Map, Object)} method
+     * followed by {@link OptionKey#getValueFrom(Map)}.
+     */
+    @Test
+    public void testSetAndGet() {
+        final Map<OptionKey<?>,Object> options = 
URL_ENCODING.setValueInto(null, "UTF-8");
+        assertEquals("UTF-8", getSingleton(options.values()));
+        assertEquals("UTF-8", URL_ENCODING.getValueFrom(options));
+
+        assertSame(options, URL_ENCODING.setValueInto(options, "ISO-8859-1"));
+        assertEquals("ISO-8859-1", getSingleton(options.values()));
+        assertEquals("ISO-8859-1", URL_ENCODING.getValueFrom(options));
+    }
+
+    /**
      * Tests the serialization of constants.
      * Those constants shall be resolved to their singleton instance on 
deserialization.
      */

Modified: 
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1488404&r1=1488403&r2=1488404&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
 [UTF-8] Fri May 31 21:32:57 2013
@@ -52,7 +52,6 @@ import org.junit.BeforeClass;
     org.apache.sis.math.StatisticsTest.class,
     org.apache.sis.math.StatisticsFormatTest.class,
     org.apache.sis.internal.util.UtilitiesTest.class,
-    org.apache.sis.internal.util.OptionsTest.class,
     org.apache.sis.internal.jdk8.JDK8Test.class,
 
     // Collections.

Modified: 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java?rev=1488404&r1=1488403&r2=1488404&view=diff
==============================================================================
--- 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
 [UTF-8] Fri May 31 21:32:57 2013
@@ -34,7 +34,6 @@ import org.apache.sis.util.ArgumentCheck
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.storage.IOUtilities;
 import org.apache.sis.internal.storage.ChannelImageInputStream;
-import org.apache.sis.internal.util.Options;
 import org.apache.sis.setup.OptionKey;
 
 
@@ -152,7 +151,8 @@ public class DataStoreConnection impleme
      * @return The current value for the given option, or {@code null} if none.
      */
     public <T> T getOption(final OptionKey<T> key) {
-        return Options.get(options, key);
+        ArgumentChecks.ensureNonNull("key", key);
+        return key.getValueFrom(options);
     }
 
     /**
@@ -168,7 +168,8 @@ public class DataStoreConnection impleme
      * @param value The new value for the given option, or {@code null} for 
removing the value.
      */
     public <T> void setOption(final OptionKey<T> key, final T value) {
-        options = Options.set(options, key, value);
+        ArgumentChecks.ensureNonNull("key", key);
+        options = key.setValueInto(options, value);
     }
 
     /**
@@ -481,7 +482,9 @@ public class DataStoreConnection impleme
     public String toString() {
         final StringBuilder buffer = new StringBuilder(40);
         
buffer.append(Classes.getShortClassName(this)).append("[“").append(getStorageName()).append('”');
-        Options.list(options, ", ", buffer);
+        if (options != null) {
+            buffer.append(", options=").append(options);
+        }
         return buffer.append(']').toString();
     }
 }


Reply via email to