Author: desruisseaux
Date: Thu Jul 30 11:07:55 2015
New Revision: 1693410

URL: http://svn.apache.org/r1693410
Log:
Added a Builder(IdentifiedObject) constructor for allowing usage of an existing 
object as a template.

Modified:
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
    
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/Builder.java
    
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderMock.java
    
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderTest.java

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java?rev=1693410&r1=1693409&r2=1693410&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
 [UTF-8] Thu Jul 30 11:07:55 2015
@@ -126,6 +126,20 @@ public class ParameterBuilder extends Bu
     }
 
     /**
+     * Creates a new builder initialized to properties of the given object.
+     *
+     * @param descriptor The descriptor from which to inherit properties, or 
{@code null}.
+     *
+     * @since 0.6
+     */
+    public ParameterBuilder(final GeneralParameterDescriptor descriptor) {
+        super(descriptor);
+        if (descriptor != null) {
+            required = descriptor.getMinimumOccurs() != 0;
+        }
+    }
+
+    /**
      * Sets whether the parameter is mandatory or optional.
      * This property determines the {@linkplain 
DefaultParameterDescriptor#getMinimumOccurs() minimum number
      * of times} that values are required, which will be 0 for an optional 
parameter and 1 for a mandatory one.
@@ -148,7 +162,7 @@ public class ParameterBuilder extends Bu
      * Boxes the given value if non-NaN, or returns {@code null} if the value 
is {@code NaN}.
      */
     private static Double valueOf(final double value) {
-        return Double.isNaN(value) ? null : Double.valueOf(value);
+        return Double.isNaN(value) ? null : value;
     }
 
     /**

Modified: 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/Builder.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/Builder.java?rev=1693410&r1=1693409&r2=1693410&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/Builder.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/Builder.java
 [UTF-8] Thu Jul 30 11:07:55 2015
@@ -20,6 +20,7 @@ import java.util.Map;
 import java.util.List;
 import java.util.HashMap;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.lang.reflect.Type;
 import java.lang.reflect.ParameterizedType;
 import org.opengis.util.NameSpace;
@@ -200,10 +201,9 @@ public abstract class Builder<B extends
 
     /**
      * The codespace as a {@code NameSpace} object, or {@code null} if not yet 
created.
-     *
-     * @see #namespace()
+     * This object is built from the {@value 
org.opengis.metadata.Identifier#CODESPACE_KEY} value when first needed.
      */
-    private NameSpace namespace;
+    private transient NameSpace namespace;
 
     /**
      * The name factory, fetched when first needed.
@@ -253,6 +253,26 @@ public abstract class Builder<B extends
     }
 
     /**
+     * Creates a new builder initialized to properties of the given object.
+     * The properties recognized by this constructor are documented in the
+     * {@link IdentifiedObjects#getProperties(IdentifiedObject, String...)} 
method.
+     *
+     * @param object The identified object from which to inherit properties, 
or {@code null}.
+     *
+     * @since 0.6
+     */
+    protected Builder(final IdentifiedObject object) {
+        this();
+        if (object != null) {
+            properties.putAll(IdentifiedObjects.getProperties(object));
+            final GenericName[] valueAlias = (GenericName[]) 
properties.remove(IdentifiedObject.ALIAS_KEY);
+            final Identifier[]  valueIds   = (Identifier[])  
properties.remove(IdentifiedObject.IDENTIFIERS_KEY);
+            if (valueAlias != null) aliases.addAll(Arrays.asList(valueAlias));
+            if (valueIds != null) identifiers.addAll(Arrays.asList(valueIds));
+        }
+    }
+
+    /**
      * Returns the name factory to use for creating namespaces and local names.
      * The factory will be fetched when first needed, and while not change 
anymore
      * for the rest of this {@code Builder} lifetime.
@@ -430,31 +450,6 @@ public abstract class Builder<B extends
     }
 
     /**
-     * Sets whether the next {@code IdentifiedObject}s to create shall be 
considered deprecated. Deprecated objects
-     * exist in some {@linkplain org.opengis.referencing.AuthorityFactory 
authority factories} like the EPSG database.
-     *
-     * <p>Note that this method does not apply to name and identifiers, which 
have their own
-     * {@code addDeprecatedFoo(…)} methods.</p>
-     *
-     * <p><b>Lifetime:</b>
-     * this property is kept unchanged until this {@code setDeprecated(…)} 
method is invoked again.</p>
-     *
-     * @param  deprecated {@code true} if the next names, identifiers and 
identified objects should be
-     *         considered deprecated, or {@code false} otherwise.
-     * @return {@code this}, for method call chaining.
-     *
-     * @see #addDeprecatedName(CharSequence, CharSequence)
-     * @see #addDeprecatedIdentifier(String, String)
-     * @see AbstractIdentifiedObject#isDeprecated()
-     *
-     * @since 0.6
-     */
-    public B setDeprecated(final boolean deprecated) {
-        properties.put(AbstractIdentifiedObject.DEPRECATED_KEY, deprecated);
-        return self();
-    }
-
-    /**
      * Adds an {@code IdentifiedObject} name given by a {@code String} or 
{@code InternationalString}.
      * The given string will be combined with the authority, {@link 
#setCodeSpace(Citation, String) code space}
      * and {@link #setVersion(String) version} information for creating the 
{@link Identifier} or {@link GenericName}
@@ -837,6 +832,31 @@ public abstract class Builder<B extends
         return self();
     }
 
+    /**
+     * Sets whether the next {@code IdentifiedObject}s to create shall be 
considered deprecated. Deprecated objects
+     * exist in some {@linkplain org.opengis.referencing.AuthorityFactory 
authority factories} like the EPSG database.
+     *
+     * <p>Note that this method does not apply to name and identifiers, which 
have their own
+     * {@code addDeprecatedFoo(…)} methods.</p>
+     *
+     * <p><b>Lifetime:</b>
+     * this property is kept unchanged until this {@code setDeprecated(…)} 
method is invoked again.</p>
+     *
+     * @param  deprecated {@code true} if the next names, identifiers and 
identified objects should be
+     *         considered deprecated, or {@code false} otherwise.
+     * @return {@code this}, for method call chaining.
+     *
+     * @see #addDeprecatedName(CharSequence, CharSequence)
+     * @see #addDeprecatedIdentifier(String, String)
+     * @see AbstractIdentifiedObject#isDeprecated()
+     *
+     * @since 0.6
+     */
+    public B setDeprecated(final boolean deprecated) {
+        properties.put(AbstractIdentifiedObject.DEPRECATED_KEY, deprecated);
+        return self();
+    }
+
     /**
      * Initializes/cleanups the {@link #properties} map before/after a {@code 
createXXX(…)} execution.
      * Subclasses shall invoke this method in their {@code createXXX(…)} 
methods as below:

Modified: 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderMock.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderMock.java?rev=1693410&r1=1693409&r2=1693410&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderMock.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderMock.java
 [UTF-8] Thu Jul 30 11:07:55 2015
@@ -32,6 +32,19 @@ import org.opengis.util.GenericName;
  */
 final strictfp class BuilderMock extends Builder<BuilderMock> {
     /**
+     * Creates a new builder.
+     */
+    BuilderMock() {
+    }
+
+    /**
+     * Creates a new builder initialized to the given object.
+     */
+    BuilderMock(final IdentifiedObject object) {
+        super(object);
+    }
+
+    /**
      * Convenience accessor for the property value assigned to {@link 
IdentifiedObject#NAME_KEY}.
      */
     Object getName() {

Modified: 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderTest.java?rev=1693410&r1=1693409&r2=1693410&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/BuilderTest.java
 [UTF-8] Thu Jul 30 11:07:55 2015
@@ -17,6 +17,7 @@
 package org.apache.sis.referencing;
 
 import java.util.Map;
+import java.util.HashMap;
 import org.opengis.util.NameSpace;
 import org.opengis.util.LocalName;
 import org.opengis.util.GenericName;
@@ -24,6 +25,7 @@ import org.opengis.util.NameFactory;
 import org.opengis.metadata.citation.Citation;
 import org.opengis.metadata.Identifier;
 import org.apache.sis.internal.simple.SimpleCitation;
+import org.apache.sis.internal.simple.SimpleIdentifier;
 import org.apache.sis.internal.system.DefaultFactories;
 import org.apache.sis.metadata.iso.ImmutableIdentifier;
 import org.apache.sis.metadata.iso.citation.Citations;
@@ -49,6 +51,7 @@ public final strictfp class BuilderTest
      * Tests {@link Builder#verifyParameterizedType(Class)}.
      */
     @Test
+    @SuppressWarnings("ResultOfObjectAllocationIgnored")
     public void testVerifyParameterizedType() {
         final class Invalid extends Builder<BuilderMock> {
         }
@@ -281,4 +284,36 @@ public final strictfp class BuilderTest
             "GeoTIFF:CT_Mercator"
         }, builder.getAsStrings(1));
     }
+
+    /**
+     * Tests the {@link Builder#Builder(IdentifiedObject)} constructor.
+     *
+     * @since 0.6
+     */
+    @Test
+    public void testCreationFromObject() {
+        final Map<String,Object> properties = new HashMap<>();
+        final Identifier id = new SimpleIdentifier(null, "An identifier", 
false);
+        assertNull(properties.put(AbstractIdentifiedObject.IDENTIFIERS_KEY, 
id));
+        assertNull(properties.put(AbstractIdentifiedObject.ALIAS_KEY,       
"An alias"));
+        assertNull(properties.put(AbstractIdentifiedObject.NAME_KEY,        
"Dummy object"));
+        assertNull(properties.put(AbstractIdentifiedObject.REMARKS_KEY,     
"Some remarks"));
+        final BuilderMock builder = new BuilderMock(new 
AbstractIdentifiedObject(properties));
+
+        assertEquals("Expected only name and remarks.", 2, 
builder.properties.size());
+        builder.onCreate(false);
+        assertEquals("Expected name, aliases, identifiers and remarks.", 4, 
builder.properties.size());
+
+        assertEquals(AbstractIdentifiedObject.NAME_KEY, "Dummy object",
+                
builder.properties.get(AbstractIdentifiedObject.NAME_KEY).toString());
+
+        assertEquals(AbstractIdentifiedObject.REMARKS_KEY, "Some remarks",
+                
builder.properties.get(AbstractIdentifiedObject.REMARKS_KEY).toString());
+
+        assertEquals(AbstractIdentifiedObject.ALIAS_KEY, "An alias",
+                ((Object[]) 
builder.properties.get(AbstractIdentifiedObject.ALIAS_KEY))[0].toString());
+
+        assertSame(AbstractIdentifiedObject.IDENTIFIERS_KEY, id,
+                ((Object[]) 
builder.properties.get(AbstractIdentifiedObject.IDENTIFIERS_KEY))[0]);
+    }
 }


Reply via email to