sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1754952 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/builder/ test/java/org/apache/sis/feature/builder/ test/java/org/apache/sis/test/suite/
Date Tue, 02 Aug 2016 15:30:17 GMT
Author: desruisseaux
Date: Tue Aug  2 15:30:17 2016
New Revision: 1754952

URL: http://svn.apache.org/viewvc?rev=1754952&view=rev
Log:
Add getter methods, perform more checks and add tests.

Added:
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java
      - copied, changed from r1754841, sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/CharacteristicTypeBuilderTest.java
      - copied, changed from r1754841, sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/CharacteristicTypeBuilder.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/PropertyTypeBuilder.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/RemoveOnlyList.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/TypeBuilder.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/AttributeTypeBuilder.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -41,9 +41,22 @@ import org.opengis.feature.PropertyType;
 
 
 /**
- * Describes one attribute of the {@code FeatureType} to be built by the enclosing {@code FeatureTypeBuilder}.
- * A different instance of {@code AttributeTypeBuilder} exists for each feature attribute to describe.
- * Those instances are created by {@link FeatureTypeBuilder#addAttribute(Class)}.
+ * Describes one {@code AttributeType} which will be part of the feature type to be built by
+ * a {@code FeatureTypeBuilder}. An attribute can be for example a city name, a temperature
+ * (together with its units of measurement and uncertainty if desired) or a geometric shape.
+ * Attribute types contain the following information:
+ *
+ * <ul>
+ *   <li>the name        — a unique name which can be defined within a scope (or namespace).</li>
+ *   <li>the definition  — a concise definition of the element.</li>
+ *   <li>the designation — a natural language designator for the element for user interfaces.</li>
+ *   <li>the description — information beyond that required for concise definition of the element.</li>
+ *   <li>the value class — often {@link String}, {@link Float} or {@link com.esri.core.geometry.Geometry}.
+ *       Must be specified at {@linkplain FeatureTypeBuilder#addAttribute(Class) construction time}.</li>
+ *   <li>a default value — to be used when an attribute instance does not provide an explicit value.</li>
+ *   <li>characteristics — for example the units of measurement for all attributes of the same type.</li>
+ *   <li>cardinality     — the minimum and maximum occurrences of attribute values.</li>
+ * </ul>
  *
  * @param <V> the class of attribute values.
  *
@@ -53,8 +66,8 @@ import org.opengis.feature.PropertyType;
  * @version 0.8
  * @module
  *
- * @see org.apache.sis.feature.DefaultAttributeType
  * @see FeatureTypeBuilder#addAttribute(Class)
+ * @see org.apache.sis.feature.DefaultAttributeType
  */
 public final class AttributeTypeBuilder<V> extends PropertyTypeBuilder {
     /**
@@ -81,7 +94,7 @@ public final class AttributeTypeBuilder<
     /**
      * Builders for the characteristics associated to the attribute.
      */
-    private final List<CharacteristicTypeBuilder<?>> characteristics;
+    final List<CharacteristicTypeBuilder<?>> characteristics;
 
     /**
      * Creates a new builder initialized to the values of the given builder.
@@ -210,13 +223,14 @@ public final class AttributeTypeBuilder<
         if (type == valueClass) {
             return (AttributeTypeBuilder<N>) this;
         }
-        final AttributeTypeBuilder<N> n = new AttributeTypeBuilder<>(this, type);
-        for (final CharacteristicTypeBuilder<?> c : n.characteristics) {
-            c.owner = n;
+        final AttributeTypeBuilder<N> newb = new AttributeTypeBuilder<>(this, type);
+        for (final CharacteristicTypeBuilder<?> c : characteristics) {
+            c.owner(newb);
         }
-        owner.replace(this, n);
+        owner.properties.set(owner.properties.lastIndexOf(this), newb);
+        // Note: a negative lastIndexOf(old) would be a bug in our algorithm.
         dispose();
-        return n;
+        return newb;
     }
 
     /**
@@ -248,6 +262,7 @@ public final class AttributeTypeBuilder<
 
     /**
      * Returns an enumeration of valid values for the attribute, or an empty array if none.
+     * This convenience method returns the value of the characteristic set by {@link #setValidValues(Object...)}.
      *
      * @return valid values for the attribute, or an empty array if none.
      */
@@ -282,6 +297,16 @@ public final class AttributeTypeBuilder<
     }
 
     /**
+     * Returns the maximal length that characterizes the {@link CharSequence} values of this attribute.
+     * This convenience method returns the value of the characteristic set by {@link #setMaximalLength(Integer)}.
+     *
+     * @return the maximal length of {@link CharSequence} attribute values, or {@code null}.
+     */
+    public Integer getMaximalLength() {
+        return (Integer) getCharacteristic(AttributeConvention.MAXIMAL_LENGTH_CHARACTERISTIC);
+    }
+
+    /**
      * Sets the maximal length that characterizes the {@link CharSequence} values of this attribute.
      * While this characteristic can be applied to any kind of attribute, it is meaningful only with
      * character sequences.
@@ -300,6 +325,16 @@ public final class AttributeTypeBuilder<
     }
 
     /**
+     * Returns the coordinate reference system associated to attribute values.
+     * This convenience method returns the value of the characteristic set by {@link #setCRS(CoordinateReferenceSystem)}.
+     *
+     * @return the coordinate reference system associated to attribute values, or {@code null}.
+     */
+    public CoordinateReferenceSystem getCRS() {
+        return (CoordinateReferenceSystem) getCharacteristic(AttributeConvention.CRS_CHARACTERISTIC);
+    }
+
+    /**
      * Sets the coordinate reference system that characterizes the values of this attribute.
      * While this characteristic can be applied to any kind of attribute, it is meaningful
      * only with georeferenced values like geometries or coverages.
@@ -336,6 +371,7 @@ public final class AttributeTypeBuilder<
         for (final CharacteristicTypeBuilder<?> characteristic : characteristics) {
             if (name.equals(characteristic.getName())) {
                 characteristic.set(value);
+                clearCache();
                 return this;
             }
         }
@@ -344,6 +380,22 @@ public final class AttributeTypeBuilder<
     }
 
     /**
+     * Returns the builder for the characteristic of the given name. The given name does not need to contains
+     * all elements of a {@link org.opengis.util.ScopedName}; it is okay to specify only the tip (for example
+     * {@code "myName"} instead of {@code "myScope:myName"}) provided that ignoring the name head does not
+     * create ambiguity.
+     *
+     * @param  name   name of the characteristic to search.
+     * @return characteristic of the given name, or {@code null} if none.
+     * @throws IllegalArgumentException if the given name is ambiguous.
+     *
+     * @see #characteristics()
+     */
+    public CharacteristicTypeBuilder<?> getCharacteristic(final String name) {
+        return forName(characteristics, name);
+    }
+
+    /**
      * Adds another attribute type that describes this attribute type.
      * See <cite>"Attribute characterization"</cite> in {@link DefaultAttributeType} Javadoc for more information.
      *
@@ -437,6 +489,7 @@ public final class AttributeTypeBuilder<
      *
      * @return a live list over the characteristics declared to this builder.
      *
+     * @see #getCharacteristic(String)
      * @see #addCharacteristic(Class)
      * @see #addCharacteristic(AttributeType)
      * @see #setValidValues(Object...)
@@ -447,20 +500,6 @@ public final class AttributeTypeBuilder<
     }
 
     /**
-     * Replaces the given characteristic by a new one. Exactly one instance of the old characteristic
-     * shall exist (this is not verified).
-     *
-     * @see CharacteristicTypeBuilder#setValueClass(Class)
-     */
-    final void replace(final CharacteristicTypeBuilder<?> old, final CharacteristicTypeBuilder<?> n) {
-        /*
-         * We do not verify if lastIndexOf(old) >= 0 because
-         * an element not found would be a bug in our algorithm.
-         */
-        characteristics.set(characteristics.lastIndexOf(old), n);
-    }
-
-    /**
      * {@inheritDoc}
      */
     @Override

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/CharacteristicTypeBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/CharacteristicTypeBuilder.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/CharacteristicTypeBuilder.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/CharacteristicTypeBuilder.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -28,13 +28,14 @@ import org.opengis.feature.AttributeType
 
 
 /**
- * Describes one characteristic of an {@code AttributeType} to be built by the enclosing {@code FeatureTypeBuilder}.
- * A different instance of {@code CharacteristicTypeBuilder} exists for each characteristic to describe.
- * Those instances are created by:
+ * Describes one characteristic of the {@code AttributeType} will will be built by a {@code FeatureTypeBuilder}.
+ * Characteristics can describe additional information useful for interpreting an attribute value, like
+ * the units of measurement and uncertainties of a numerical value, or the coordinate reference system
+ * (CRS) of a geometry.
  *
- * <ul>
- *   <li>{@link AttributeTypeBuilder#addCharacteristic(Class)}</li>
- * </ul>
+ * <p>In many cases, all instances of the same {@code AttributeType} have the same characteristics.
+ * For example all values of the "temperature" attribute typically have the same units of measurement.
+ * Such common value can be specified as the characteristic {@linkplain #setDefaultValue(Object) default value}.</p>
  *
  * @param <V> the class of characteristic values.
  *
@@ -43,13 +44,15 @@ import org.opengis.feature.AttributeType
  * @since   0.8
  * @version 0.8
  * @module
+ *
+ * @see AttributeTypeBuilder#addCharacteristic(Class)
  */
 public final class CharacteristicTypeBuilder<V> extends TypeBuilder {
     /**
      * The attribute type builder instance that created this {@code CharacteristicTypeBuilder} builder.
      * This is set at construction time and considered as immutable until it is set to {@code null}.
      */
-    AttributeTypeBuilder<?> owner;
+    private AttributeTypeBuilder<?> owner;
 
     /**
      * The class of characteristic values. Can not be changed after construction
@@ -210,10 +213,11 @@ public final class CharacteristicTypeBui
         if (type == valueClass) {
             return (CharacteristicTypeBuilder<N>) this;
         }
-        final CharacteristicTypeBuilder<N> n = new CharacteristicTypeBuilder<>(this, type);
-        owner.replace(this, n);
-        owner = null;
-        return n;
+        final CharacteristicTypeBuilder<N> newb = new CharacteristicTypeBuilder<>(this, type);
+        owner.characteristics.set(owner.characteristics.lastIndexOf(this), newb);
+        // Note: a negative lastIndexOf(old) would be a bug in our algorithm.
+        dispose();
+        return newb;
     }
 
     /**
@@ -286,4 +290,36 @@ public final class CharacteristicTypeBui
         }
         return characteristic;
     }
+
+    /**
+     * Sets a new owner.
+     */
+    final void owner(final AttributeTypeBuilder<?> newb) {
+        owner = newb;
+    }
+
+    /**
+     * Flags this builder as a disposed one. The builder should not be used anymore after this method call.
+     *
+     * @see #remove()
+     */
+    @Override
+    final void dispose() {
+        owner = null;
+    }
+
+    /**
+     * Removes this characteristics from the {@code AttributeTypeBuilder}.
+     * After this method has been invoked, this {@code CharacteristicTypeBuilder} instance
+     * is no longer in the list returned by {@link AttributeTypeBuilder#characteristics()}
+     * and attempts to invoke any setter method on {@code this} will cause an
+     * {@link IllegalStateException} to be thrown.
+     */
+    public void remove() {
+        if (owner != null) {
+            owner.characteristics.remove(owner.characteristics.lastIndexOf(this));
+            // Note: a negative lastIndexOf(old) would be a bug in our algorithm.
+            dispose();
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -70,7 +70,7 @@ public class FeatureTypeBuilder extends
     /**
      * Builders for the properties (attributes, associations or operations) of this feature.
      */
-    private final List<PropertyTypeBuilder> properties;
+    final List<PropertyTypeBuilder> properties;
 
     /**
      * The parent of the feature to create. By default, new features have no parent.
@@ -414,6 +414,7 @@ public class FeatureTypeBuilder extends
      *
      * @return a live list over the properties declared to this builder.
      *
+     * @see #getProperty(String)
      * @see #addAttribute(Class)
      * @see #addAttribute(AttributeType)
      * @see #addAssociation(FeatureType)
@@ -425,17 +426,18 @@ public class FeatureTypeBuilder extends
     }
 
     /**
-     * Replaces the given attribute by a new one. Exactly one instance of the old attribute
-     * shall exist (this is not verified).
+     * Returns the builder for the property of the given name. The given name does not need to contains all elements
+     * of a {@link org.opengis.util.ScopedName}; it is okay to specify only the tip (for example {@code "myName"}
+     * instead of {@code "myScope:myName"}) provided that ignoring the name head does not create ambiguity.
+     *
+     * @param  name   name of the property to search.
+     * @return property of the given name, or {@code null} if none.
+     * @throws IllegalArgumentException if the given name is ambiguous.
      *
-     * @see AttributeTypeBuilder#setValueClass(Class)
+     * @see #addProperty(PropertyType)
      */
-    final void replace(final AttributeTypeBuilder<?> old, final AttributeTypeBuilder<?> n) {
-        /*
-         * We do not verify if lastIndexOf(old) >= 0 because
-         * an element not found would be a bug in our algorithm.
-         */
-        properties.set(properties.lastIndexOf(old), n);
+    public PropertyTypeBuilder getProperty(final String name) {
+        return forName(properties, name);
     }
 
     /**
@@ -554,6 +556,7 @@ public class FeatureTypeBuilder extends
      *         In the {@code Operation} case, the builder is a read-only accessor on the operation properties.
      *
      * @see #properties()
+     * @see #getProperty(String)
      */
     public PropertyTypeBuilder addProperty(final PropertyType template) {
         ensureNonNull("template", template);

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/PropertyTypeBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/PropertyTypeBuilder.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/PropertyTypeBuilder.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/PropertyTypeBuilder.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -113,13 +113,6 @@ public abstract class PropertyTypeBuilde
     }
 
     /**
-     * Flags this builder as a disposed one. The builder should not be used anymore after this method call.
-     */
-    final void dispose() {
-        owner = null;
-    }
-
-    /**
      * Sets the minimum and maximum number of property values. Those numbers must be equal or greater than zero.
      *
      * <p>If this method is not invoked, then the default values are the cardinality specified by the last call
@@ -185,4 +178,27 @@ public abstract class PropertyTypeBuilde
      * Creates a new property type from the current setting.
      */
     abstract PropertyType create();
+
+    /**
+     * Flags this builder as a disposed one. The builder should not be used anymore after this method call.
+     */
+    @Override
+    final void dispose() {
+        owner = null;
+    }
+
+    /**
+     * Removes this property from the {@code FeatureTypeBuilder}.
+     * After this method has been invoked, this {@code PropertyTypeBuilder} instance
+     * is no longer in the list returned by {@link FeatureTypeBuilder#properties()}
+     * and attempts to invoke any setter method on {@code this} will cause an
+     * {@link IllegalStateException} to be thrown.
+     */
+    public void remove() {
+        if (owner != null) {
+            owner.properties.remove(owner.properties.lastIndexOf(this));
+            // Note: a negative lastIndexOf(old) would be a bug in our algorithm.
+            dispose();
+        }
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/RemoveOnlyList.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/RemoveOnlyList.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/RemoveOnlyList.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/RemoveOnlyList.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -17,7 +17,6 @@
 package org.apache.sis.feature.builder;
 
 import java.util.AbstractList;
-import java.util.Iterator;
 import java.util.List;
 
 
@@ -30,7 +29,7 @@ import java.util.List;
  * @version 0.8
  * @module
  */
-final class RemoveOnlyList<E> extends AbstractList<E> {
+final class RemoveOnlyList<E extends TypeBuilder> extends AbstractList<E> {
     /**
      * The original list to wrap.
      */
@@ -43,9 +42,40 @@ final class RemoveOnlyList<E> extends Ab
         this.elements = elements;
     }
 
-    @Override public void        clear()           {       elements.clear();}
-    @Override public int         size()            {return elements.size();}
-    @Override public E           get(int index)    {return elements.get(index);}
-    @Override public E           remove(int index) {return elements.get(index);}
-    @Override public Iterator<E> iterator()        {return elements.iterator();}
+    /**
+     * Returns the number of elements in this list.
+     */
+    @Override
+    public int size() {
+        return elements.size();
+    }
+
+    /**
+     * Returns the element at the given index.
+     */
+    @Override
+    public E get(int index) {
+        return elements.get(index);
+    }
+
+    /**
+     * Removes the element at the given index. The removed element is flagged as not usable anymore.
+     */
+    @Override
+    public E remove(int index) {
+        final E element = elements.get(index);
+        if (element != null) {
+            element.dispose();
+        }
+        return element;
+    }
+
+    /**
+     * Flags all elements as not usable anymore, then remove all of them.
+     */
+    @Override
+    public void clear() {
+        elements.forEach(TypeBuilder::dispose);
+        elements.clear();
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/TypeBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/TypeBuilder.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/TypeBuilder.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/TypeBuilder.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -18,7 +18,9 @@ package org.apache.sis.feature.builder;
 
 import java.util.Map;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Locale;
+import org.opengis.util.ScopedName;
 import org.opengis.util.GenericName;
 import org.apache.sis.feature.AbstractIdentifiedType;
 import org.apache.sis.util.resources.Vocabulary;
@@ -34,8 +36,8 @@ import org.opengis.feature.IdentifiedTyp
 
 
 /**
- * Properties common to all kind of types (feature, association, characteristics).
- * Those properties are:
+ * Information common to all kind of types (feature, association, characteristics).
+ * Those information are:
  *
  * <ul>
  *   <li>the name        — a unique name which can be defined within a scope (or namespace).</li>
@@ -44,6 +46,7 @@ import org.opengis.feature.IdentifiedTyp
  *   <li>the description — information beyond that required for concise definition of the element.</li>
  * </ul>
  *
+ * <div class="section">Default namespace</div>
  * In many cases, the names of all {@code AttributeType}s and {@code AssociationRole}s to create
  * within a {@code FeatureType} share the same namespace.
  * For making name creations more convenient, a default namespace can be
@@ -323,6 +326,46 @@ public abstract class TypeBuilder implem
     }
 
     /**
+     * Returns the element of the given name in the given list. The given name does not need to contains
+     * all elements of a {@link ScopedName}; it can be only the tip (for example {@code "myName"} instead
+     * of {@code "myScope:myName"}) provided that ignoring the name head does not create ambiguity.
+     *
+     * @param  types  the collection where to search for an element of the given name.
+     * @param  name   name of the element to search.
+     * @return element of the given name, or {@code null} if none were found.
+     * @throws IllegalArgumentException if the given name is ambiguous.
+     */
+    final <E extends TypeBuilder> E forName(final List<E> types, final String name) {
+        E best      = null;                     // Best type found so far.
+        E ambiguity = null;                     // If two types are found at the same depth, the other type.
+        int depth   = Integer.MAX_VALUE;        // Number of path elements that we had to ignore in the GenericName.
+        for (final E type : types) {
+            GenericName candidate = type.getName();
+            for (int d=0; candidate != null; d++) {
+                if (name.equals(candidate.toString())) {
+                    if (d < depth) {
+                        best      = type;
+                        ambiguity = null;
+                        depth     = d;
+                        break;
+                    }
+                    if (d == depth) {
+                        ambiguity = type;
+                        break;
+                    }
+                }
+                if (!(candidate instanceof ScopedName)) break;
+                candidate = ((ScopedName) candidate).tail();
+            }
+        }
+        if (ambiguity != null) {
+            throw new IllegalArgumentException(errors().getString(
+                    Errors.Keys.AmbiguousName_3, best.getName(), ambiguity.getName(), name));
+        }
+        return best;
+    }
+
+    /**
      * Returns the locale used for formatting error messages, or {@code null} if unspecified.
      * If unspecified, the system default locale will be used.
      *
@@ -397,7 +440,7 @@ public abstract class TypeBuilder implem
 
     /**
      * Partial implementation of {@link #toString()}. This method assumes that the class name
-     * has already been written in the buffer.
+     * has already be written in the buffer.
      */
     final StringBuilder toString(final StringBuilder buffer) {
         toStringInternal(buffer.append("[“").append(getDisplayName()).append('”'));
@@ -409,4 +452,11 @@ public abstract class TypeBuilder implem
      */
     void toStringInternal(StringBuilder buffer) {
     }
+
+    /**
+     * Invoked when a type builder has been removed from its parent.
+     * Subclasses should override this method in a way that flag the builder as not usable anymore.
+     */
+    void dispose() {
+    }
 }

Copied: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java (from r1754841, sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java?p2=sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java&p1=sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java&r1=1754841&r2=1754952&rev=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/AttributeTypeBuilderTest.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -16,27 +16,24 @@
  */
 package org.apache.sis.feature.builder;
 
-import java.util.Iterator;
-import com.esri.core.geometry.Geometry;
-import com.esri.core.geometry.Point;
+import java.util.Collections;
+import org.apache.sis.referencing.CommonCRS;
 import org.apache.sis.internal.feature.AttributeConvention;
-import org.apache.sis.feature.DefaultFeatureTypeTest;
-import org.apache.sis.referencing.crs.HardCodedCRS;
 import org.apache.sis.test.DependsOnMethod;
-import org.apache.sis.test.TestUtilities;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.apache.sis.test.Assert.*;
 
 // Branch-dependent imports
 import org.opengis.feature.AttributeType;
-import org.opengis.feature.FeatureType;
-import org.opengis.feature.PropertyType;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
 
 
 /**
- * Tests {@link FeatureTypeBuilder}.
+ * Tests {@link AttributeTypeBuilder}. The tests need to create a {@link FeatureTypeBuilder} in order to allow
+ * {@code AttributeTypeBuilder} instantiation, but nothing else is done with the {@code FeatureTypeBuilder}.
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
@@ -44,12 +41,13 @@ import org.opengis.feature.PropertyType;
  * @version 0.8
  * @module
  */
-public final strictfp class FeatureTypeBuilderTest extends TestCase {
+@DependsOn(CharacteristicTypeBuilderTest.class)
+public final strictfp class AttributeTypeBuilderTest extends TestCase {
     /**
-     * Tests a {@code AttributeTypeBuilder} with the minimum number of parameters.
+     * Tests with the minimum number of parameters.
      */
     @Test
-    public void testAttributeTypeBuilderInitialization() {
+    public void testInitialization() {
         final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
         assertEquals("default name", "string", builder.getName().toString());
 
@@ -67,42 +65,19 @@ public final strictfp class FeatureTypeB
     }
 
     /**
-     * Tests a {@code FeatureTypeBuilder} with the minimum number of parameters (no property and no super type).
+     * Test creation of a single attribute with more values than the minimal ones.
      */
     @Test
-    @DependsOnMethod("testAttributeTypeBuilderInitialization")
-    public void testFeatureTypeBuilderInitialization() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        try {
-            builder.build();
-            fail("Builder should have failed if there is not at least a name set.");
-        } catch (IllegalArgumentException ex) {
-            final String message = ex.getMessage();
-            assertTrue(message, message.contains("name"));
-        }
-        builder.setName("scope", "test");
-        final FeatureType type = builder.build();
-
-        assertEquals("name", "scope:test",   type.getName().toString());
-        assertFalse ("isAbstract",           type.isAbstract());
-        assertEquals("properties count",  0, type.getProperties(true).size());
-        assertEquals("super-types count", 0, type.getSuperTypes().size());
-    }
-
-    /**
-     * Test creation of a single attribute.
-     */
-    @Test
-    @DependsOnMethod("testAttributeTypeBuilderInitialization")
-    public void testAttributeTypeBuilder() {
+    @DependsOnMethod("testInitialization")
+    public void testBuilder() {
         final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
-        builder.setName        ("myScope", "myName");
-        builder.setDefinition  ("test definition");
-        builder.setDesignation ("test designation");
-        builder.setDescription ("test description");
-        builder.setDefaultValue("test default value.");
-        builder.setCardinality(10, 60);
-        builder.setMaximalLength(80);
+        assertSame(builder, builder.setName        ("myScope", "myName"));
+        assertSame(builder, builder.setDefinition  ("test definition"));
+        assertSame(builder, builder.setDesignation ("test designation"));
+        assertSame(builder, builder.setDescription ("test description"));
+        assertSame(builder, builder.setDefaultValue("test default value."));
+        assertSame(builder, builder.setCardinality(10, 60));
+        assertSame(builder, builder.setMaximalLength(80));
         final AttributeType<?> att = (AttributeType<?>) builder.build();
 
         assertEquals("name",          "myScope:myName",      att.getName().toString());
@@ -119,159 +94,120 @@ public final strictfp class FeatureTypeB
     }
 
     /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)}.
+     * Tests {@link AttributeTypeBuilder#setValueClass(Class)}.
+     * This implies the replacement of the builder by a new instance.
      */
     @Test
-    @DependsOnMethod("testAttributeTypeBuilder")
-    public void testAddAttribute() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("myScope", "myName");
-        builder.setDefinition ("test definition");
-        builder.setDesignation("test designation");
-        builder.setDescription("test description");
-        builder.setAbstract(true);
-        builder.addAttribute(String .class).setName("name");
-        builder.addAttribute(Integer.class).setName("age");
-        builder.addAttribute(Point  .class).setName("location").setCRS(HardCodedCRS.WGS84);
-        builder.addAttribute(Double .class).setName("score").setDefaultValue(10.0).setCardinality(5, 50);
-
-        final FeatureType type = builder.build();
-        assertEquals("name",        "myScope:myName",   type.getName().toString());
-        assertEquals("definition",  "test definition",  type.getDefinition().toString());
-        assertEquals("description", "test description", type.getDescription().toString());
-        assertEquals("designation", "test designation", type.getDesignation().toString());
-        assertTrue  ("isAbstract",                      type.isAbstract());
-
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final AttributeType<?> a0 = (AttributeType<?>) it.next();
-        final AttributeType<?> a1 = (AttributeType<?>) it.next();
-        final AttributeType<?> a2 = (AttributeType<?>) it.next();
-        final AttributeType<?> a3 = (AttributeType<?>) it.next();
-        assertFalse("properties count", it.hasNext());
-
-        assertEquals("name", "name",     a0.getName().toString());
-        assertEquals("name", "age",      a1.getName().toString());
-        assertEquals("name", "location", a2.getName().toString());
-        assertEquals("name", "score",    a3.getName().toString());
-
-        assertEquals("valueClass", String.class,  a0.getValueClass());
-        assertEquals("valueClass", Integer.class, a1.getValueClass());
-        assertEquals("valueClass", Point.class,   a2.getValueClass());
-        assertEquals("valueClass", Double.class,  a3.getValueClass());
-
-        assertEquals("minimumOccurs",   1, a0.getMinimumOccurs());
-        assertEquals("minimumOccurs",   1, a1.getMinimumOccurs());
-        assertEquals("minimumOccurs",   1, a2.getMinimumOccurs());
-        assertEquals("minimumOccurs",   5, a3.getMinimumOccurs());
-
-        assertEquals("maximumOccurs",   1, a0.getMaximumOccurs());
-        assertEquals("maximumOccurs",   1, a1.getMaximumOccurs());
-        assertEquals("maximumOccurs",   1, a2.getMaximumOccurs());
-        assertEquals("maximumOccurs",  50, a3.getMaximumOccurs());
-
-        assertEquals("defaultValue", null, a0.getDefaultValue());
-        assertEquals("defaultValue", null, a1.getDefaultValue());
-        assertEquals("defaultValue", null, a2.getDefaultValue());
-        assertEquals("defaultValue", 10.0, a3.getDefaultValue());
-
-        assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a0));
-        assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a1));
-        assertTrue ("characterizedByCRS", AttributeConvention.characterizedByCRS(a2));
-        assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a3));
+    @DependsOnMethod("testBuilder")
+    public void testSetValueClass() {
+        final AttributeTypeBuilder<Float> builder = new FeatureTypeBuilder().addAttribute(Float.class);
+        assertSame(builder, builder.setName        ("temperature"));
+        assertSame(builder, builder.setDefinition  ("test definition"));
+        assertSame(builder, builder.setDesignation ("test designation"));
+        assertSame(builder, builder.setDescription ("test description"));
+        assertSame(builder, builder.setDefaultValue(25f));
+        assertSame(builder, builder.setValueClass(Float.class));
+        assertEquals("valueClass", Float.class, builder.getValueClass());
+        assertSetEquals(Collections.singleton(builder), builder.owner().properties());
+        final CharacteristicTypeBuilder<Float> stddev = builder.addCharacteristic(Float.class);
+        assertSame(stddev, stddev.setName("stddev"));
+        assertSame(stddev, stddev.setDefaultValue(2f));
+        /*
+         * Pretend that we changed our mind and now want a Double type instead than Float.
+         * In current implementation this requires the creation of a new builder instance,
+         * but there is no guarantees that it will always be the case in future versions.
+         */
+        final AttributeTypeBuilder<Double> newb = builder.setValueClass(Double.class);
+        assertEquals("name",          "temperature",      newb.getName().toString());
+        assertEquals("definition",    "test definition",  newb.getDefinition());
+        assertEquals("description",   "test description", newb.getDescription());
+        assertEquals("designation",   "test designation", newb.getDesignation());
+        assertEquals("valueClass",    Double.class,       newb.getValueClass());
+        assertEquals("defaultValue",  Double.valueOf(25), newb.getDefaultValue());
+        assertSetEquals(Collections.singleton(newb), newb.owner().properties());
+        /*
+         * In order to avoid accidental misuse, the old builder should not be usable anymore.
+         */
+        try {
+            builder.setName("new name");
+            fail("Should not allow modification of disposed instance.");
+        } catch (IllegalStateException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("AttributeTypeBuilder"));
+        }
+        /*
+         * Verify the attribute created by the builder.
+         */
+        final AttributeType<?> att = (AttributeType<?>) newb.build();
+        assertEquals("name",          "temperature",      att.getName().toString());
+        assertEquals("definition",    "test definition",  att.getDefinition().toString());
+        assertEquals("description",   "test description", att.getDescription().toString());
+        assertEquals("designation",   "test designation", att.getDesignation().toString());
+        assertEquals("valueClass",    Double.class,       att.getValueClass());
+        assertEquals("defaultValue",  Double.valueOf(25), att.getDefaultValue());
     }
 
     /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one property is an identifier
-     * and another property is the geometry.
+     * Tests {@link AttributeTypeBuilder#setValidValues(Object...)} and the corresponding getter method.
      */
     @Test
-    @DependsOnMethod("testAddAttribute")
-    public void testAddIdentifierAndGeometry() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("scope", "test");
-        builder.setIdentifierDelimiters("-", "pref.", null);
-        builder.addAttribute(String.class).setName("name")
-                .addRole(AttributeRole.IDENTIFIER_COMPONENT);
-        builder.addAttribute(Geometry.class).setName("shape")
-                .setCRS(HardCodedCRS.WGS84)
-                .addRole(AttributeRole.DEFAULT_GEOMETRY);
-
-        final FeatureType type = builder.build();
-        assertEquals("name", "scope:test", type.getName().toString());
-        assertFalse ("isAbstract", type.isAbstract());
-
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final PropertyType a0 = it.next();
-        final PropertyType a1 = it.next();
-        final PropertyType a2 = it.next();
-        final PropertyType a3 = it.next();
-        final PropertyType a4 = it.next();
-        assertFalse("properties count", it.hasNext());
-
-        assertEquals("name", AttributeConvention.IDENTIFIER_PROPERTY, a0.getName());
-        assertEquals("name", AttributeConvention.ENVELOPE_PROPERTY,   a1.getName());
-        assertEquals("name", AttributeConvention.GEOMETRY_PROPERTY,   a2.getName());
-        assertEquals("name", "name",                                  a3.getName().toString());
-        assertEquals("name", "shape",                                 a4.getName().toString());
+    @DependsOnMethod("testBuilder")
+    public void testSetValidValues() {
+        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
+        assertEquals("length", 0, builder.getValidValues().length);
+        assertSame(builder, builder.setValidValues("Blue", "Green", "Red"));
+        assertArrayEquals(new String[] {"Blue", "Green", "Red"}, builder.getValidValues());
+        assertSame(builder, builder.setValidValues("Yellow", "Cyan", "Magenta"));
+        assertArrayEquals(new String[] {"Yellow", "Cyan", "Magenta"}, builder.getValidValues());
     }
 
     /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one attribute is an identifier that already has
-     * the {@code "@identifier"} name. This is called "anonymous" because identifiers with an explicit name in the
-     * data file should use that name instead in the feature type.
+     * Tests {@link AttributeTypeBuilder#setMaximalLength(Integer)} and
+     * {@link AttributeTypeBuilder#setCRS(CoordinateReferenceSystem)}
+     * together with the corresponding getter methods.
      */
     @Test
-    @DependsOnMethod("testAddIdentifierAndGeometry")
-    public void testAddAnonymousIdentifier() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("City");
-        builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY).addRole(AttributeRole.IDENTIFIER_COMPONENT);
-        builder.addAttribute(Integer.class).setName("population");
-        final FeatureType type = builder.build();
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final PropertyType a0 = it.next();
-        final PropertyType a1 = it.next();
-        assertFalse("properties count", it.hasNext());
-        assertEquals("name", AttributeConvention.IDENTIFIER_PROPERTY, a0.getName());
-        assertEquals("type", String.class,  ((AttributeType<?>) a0).getValueClass());
-        assertEquals("name", "population", a1.getName().toString());
-        assertEquals("type", Integer.class, ((AttributeType<?>) a1).getValueClass());
-    }
+    @DependsOnMethod("testBuilder")
+    public void testOtherCharacteristics() {
+        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
+        assertNull("maximalLength", builder.getMaximalLength());
+        assertNull("crs", builder.getCRS());
 
-    /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one attribute is a geometry that already has
-     * the {@code "@geometry"} name. This is called "anonymous" because geometries with an explicit name in the
-     * data file should use that name instead in the feature type.
-     */
-    @Test
-    @DependsOnMethod("testAddIdentifierAndGeometry")
-    public void testAddAnonymousGeometry() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("City");
-        builder.addAttribute(Point.class).setName(AttributeConvention.GEOMETRY_PROPERTY).addRole(AttributeRole.DEFAULT_GEOMETRY);
-        builder.addAttribute(Integer.class).setName("population");
-        final FeatureType type = builder.build();
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final PropertyType a0 = it.next();
-        final PropertyType a1 = it.next();
-        final PropertyType a2 = it.next();
-        assertFalse("properties count", it.hasNext());
-        assertEquals("name", AttributeConvention.ENVELOPE_PROPERTY, a0.getName());
-        assertEquals("name", AttributeConvention.GEOMETRY_PROPERTY, a1.getName());
-        assertEquals("type", Point.class,   ((AttributeType<?>) a1).getValueClass());
-        assertEquals("name", "population", a2.getName().toString());
-        assertEquals("type", Integer.class, ((AttributeType<?>) a2).getValueClass());
+        assertSame(builder, builder.setMaximalLength(20));
+        assertEquals("maximalLength", Integer.valueOf(20), builder.getMaximalLength());
+        assertNull("crs", builder.getCRS());
+
+        final CoordinateReferenceSystem crs = CommonCRS.defaultGeographic();
+        assertSame(builder, builder.setCRS(crs));
+        assertEquals("maximalLength", Integer.valueOf(20), builder.getMaximalLength());
+        assertSame("crs", crs, builder.getCRS());
+
+        assertSame(builder, builder.setMaximalLength(30));
+        assertEquals("maximalLength", Integer.valueOf(30), builder.getMaximalLength());
+        assertSame("crs", crs, builder.getCRS());
     }
 
     /**
-     * Tests creation of a builder from an existing feature type.
-     * This method also acts as a test of {@code FeatureTypeBuilder} getter methods.
+     * Tests {@link AttributeTypeBuilder#getCharacteristic(String)}.
      */
     @Test
-    public void testCreateFromTemplate() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder(DefaultFeatureTypeTest.capital());
-        assertEquals("name",       "Capital", builder.getName().toString());
-        assertEquals("superTypes", "City",    TestUtilities.getSingleton(builder.getSuperTypes()).getName().toString());
-        assertFalse ("isAbstract",            builder.isAbstract());
+    @DependsOnMethod("testOtherCharacteristics")
+    public void testGetCharacteristics() {
+        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
+        final CharacteristicTypeBuilder<Float> a = builder.addCharacteristic(Float.class).setName("a", "temp");
+        final CharacteristicTypeBuilder<Float> b = builder.addCharacteristic(Float.class).setName("b", "temp");
+        final CharacteristicTypeBuilder<Float> c = builder.addCharacteristic(Float.class).setName("c");
+        assertNull("dummy", builder.getCharacteristic("dummy"));
+        assertSame("c", c, builder.getCharacteristic("c"));
+        assertSame("b", b, builder.getCharacteristic("b:temp"));
+        assertSame("a", a, builder.getCharacteristic("a:temp"));
+        try {
+            builder.getCharacteristic("temp");
+            fail("Given name should be considered ambiguous.");
+        } catch (IllegalArgumentException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("a:temp"));
+            assertTrue(message, message.contains("b:temp"));
+        }
     }
 }

Copied: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/CharacteristicTypeBuilderTest.java (from r1754841, sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/CharacteristicTypeBuilderTest.java?p2=sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/CharacteristicTypeBuilderTest.java&p1=sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java&r1=1754841&r2=1754952&rev=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/CharacteristicTypeBuilderTest.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -16,27 +16,19 @@
  */
 package org.apache.sis.feature.builder;
 
-import java.util.Iterator;
-import com.esri.core.geometry.Geometry;
-import com.esri.core.geometry.Point;
-import org.apache.sis.internal.feature.AttributeConvention;
-import org.apache.sis.feature.DefaultFeatureTypeTest;
-import org.apache.sis.referencing.crs.HardCodedCRS;
-import org.apache.sis.test.DependsOnMethod;
-import org.apache.sis.test.TestUtilities;
+import java.util.Collections;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.apache.sis.test.Assert.*;
 
 // Branch-dependent imports
 import org.opengis.feature.AttributeType;
-import org.opengis.feature.FeatureType;
-import org.opengis.feature.PropertyType;
 
 
 /**
- * Tests {@link FeatureTypeBuilder}.
+ * Tests {@link CharacteristicTypeBuilder}. The tests need to create a {@link AttributeTypeBuilder} in order to allow
+ * {@code CharacteristicTypeBuilder} instantiation, but nothing else is done with the {@code AttributeTypeBuilder}.
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
@@ -44,234 +36,55 @@ import org.opengis.feature.PropertyType;
  * @version 0.8
  * @module
  */
-public final strictfp class FeatureTypeBuilderTest extends TestCase {
+public final strictfp class CharacteristicTypeBuilderTest extends TestCase {
     /**
-     * Tests a {@code AttributeTypeBuilder} with the minimum number of parameters.
+     * Tests {@link CharacteristicTypeBuilder#setValueClass(Class)}.
+     * This implies the replacement of the builder by a new instance.
      */
     @Test
-    public void testAttributeTypeBuilderInitialization() {
-        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
-        assertEquals("default name", "string", builder.getName().toString());
-
-        builder.setName("myScope", "myName");
-        final AttributeType<?> att = (AttributeType<?>) builder.build();
-
-        assertEquals("name", "myScope:myName",   att.getName().toString());
-        assertEquals("valueClass", String.class, att.getValueClass());
-        assertNull  ("defaultValue",             att.getDefaultValue());
-        assertNull  ("definition",               att.getDefinition());
-        assertNull  ("description",              att.getDescription());
-        assertNull  ("designation",              att.getDesignation());
-        assertEquals("minimumOccurs", 1,         att.getMinimumOccurs());
-        assertEquals("maximumOccurs", 1,         att.getMaximumOccurs());
-    }
-
-    /**
-     * Tests a {@code FeatureTypeBuilder} with the minimum number of parameters (no property and no super type).
-     */
-    @Test
-    @DependsOnMethod("testAttributeTypeBuilderInitialization")
-    public void testFeatureTypeBuilderInitialization() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
+    public void testSetValueClass() {
+        final AttributeTypeBuilder<Float> owner = new FeatureTypeBuilder().addAttribute(Float.class);
+        final CharacteristicTypeBuilder<Integer> builder = owner.addCharacteristic(Integer.class);
+        assertSame(builder, builder.setName        ("stddev"));
+        assertSame(builder, builder.setDefinition  ("test definition"));
+        assertSame(builder, builder.setDesignation ("test designation"));
+        assertSame(builder, builder.setDescription ("test description"));
+        assertSame(builder, builder.setDefaultValue(2));
+        assertSame(builder, builder.setValueClass(Integer.class));
+        assertEquals("valueClass", Integer.class, builder.getValueClass());
+        assertSetEquals(Collections.singleton(builder), owner.characteristics());
+        /*
+         * Pretend that we changed our mind and now want a Float type instead than Integer.
+         * In current implementation this requires the creation of a new builder instance,
+         * but there is no guarantees that it will always be the case in future versions.
+         */
+        final CharacteristicTypeBuilder<Float> newb = builder.setValueClass(Float.class);
+        assertEquals("name",          "stddev",           newb.getName().toString());
+        assertEquals("definition",    "test definition",  newb.getDefinition());
+        assertEquals("description",   "test description", newb.getDescription());
+        assertEquals("designation",   "test designation", newb.getDesignation());
+        assertEquals("valueClass",    Float.class,        newb.getValueClass());
+        assertEquals("defaultValue",  Float.valueOf(2),   newb.getDefaultValue());
+        assertSetEquals(Collections.singleton(newb), owner.characteristics());
+        /*
+         * In order to avoid accidental misuse, the old builder should not be usable anymore.
+         */
         try {
-            builder.build();
-            fail("Builder should have failed if there is not at least a name set.");
-        } catch (IllegalArgumentException ex) {
-            final String message = ex.getMessage();
-            assertTrue(message, message.contains("name"));
+            builder.setName("new name");
+            fail("Should not allow modification of disposed instance.");
+        } catch (IllegalStateException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("CharacteristicTypeBuilder"));
         }
-        builder.setName("scope", "test");
-        final FeatureType type = builder.build();
-
-        assertEquals("name", "scope:test",   type.getName().toString());
-        assertFalse ("isAbstract",           type.isAbstract());
-        assertEquals("properties count",  0, type.getProperties(true).size());
-        assertEquals("super-types count", 0, type.getSuperTypes().size());
-    }
-
-    /**
-     * Test creation of a single attribute.
-     */
-    @Test
-    @DependsOnMethod("testAttributeTypeBuilderInitialization")
-    public void testAttributeTypeBuilder() {
-        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
-        builder.setName        ("myScope", "myName");
-        builder.setDefinition  ("test definition");
-        builder.setDesignation ("test designation");
-        builder.setDescription ("test description");
-        builder.setDefaultValue("test default value.");
-        builder.setCardinality(10, 60);
-        builder.setMaximalLength(80);
-        final AttributeType<?> att = (AttributeType<?>) builder.build();
-
-        assertEquals("name",          "myScope:myName",      att.getName().toString());
-        assertEquals("definition",    "test definition",     att.getDefinition().toString());
-        assertEquals("description",   "test description",    att.getDescription().toString());
-        assertEquals("designation",   "test designation",    att.getDesignation().toString());
-        assertEquals("valueClass",    String.class,          att.getValueClass());
-        assertEquals("defaultValue",  "test default value.", att.getDefaultValue());
-        assertEquals("minimumOccurs", 10,                    att.getMinimumOccurs());
-        assertEquals("maximumOccurs", 60,                    att.getMaximumOccurs());
-        assertTrue  ("characterizedByMaximalLength", AttributeConvention.characterizedByMaximalLength(att));
-        assertEquals("maximalLengthCharacteristic", Integer.valueOf(80),
-                AttributeConvention.getMaximalLengthCharacteristic(att.newInstance()));
-    }
-
-    /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)}.
-     */
-    @Test
-    @DependsOnMethod("testAttributeTypeBuilder")
-    public void testAddAttribute() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("myScope", "myName");
-        builder.setDefinition ("test definition");
-        builder.setDesignation("test designation");
-        builder.setDescription("test description");
-        builder.setAbstract(true);
-        builder.addAttribute(String .class).setName("name");
-        builder.addAttribute(Integer.class).setName("age");
-        builder.addAttribute(Point  .class).setName("location").setCRS(HardCodedCRS.WGS84);
-        builder.addAttribute(Double .class).setName("score").setDefaultValue(10.0).setCardinality(5, 50);
-
-        final FeatureType type = builder.build();
-        assertEquals("name",        "myScope:myName",   type.getName().toString());
-        assertEquals("definition",  "test definition",  type.getDefinition().toString());
-        assertEquals("description", "test description", type.getDescription().toString());
-        assertEquals("designation", "test designation", type.getDesignation().toString());
-        assertTrue  ("isAbstract",                      type.isAbstract());
-
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final AttributeType<?> a0 = (AttributeType<?>) it.next();
-        final AttributeType<?> a1 = (AttributeType<?>) it.next();
-        final AttributeType<?> a2 = (AttributeType<?>) it.next();
-        final AttributeType<?> a3 = (AttributeType<?>) it.next();
-        assertFalse("properties count", it.hasNext());
-
-        assertEquals("name", "name",     a0.getName().toString());
-        assertEquals("name", "age",      a1.getName().toString());
-        assertEquals("name", "location", a2.getName().toString());
-        assertEquals("name", "score",    a3.getName().toString());
-
-        assertEquals("valueClass", String.class,  a0.getValueClass());
-        assertEquals("valueClass", Integer.class, a1.getValueClass());
-        assertEquals("valueClass", Point.class,   a2.getValueClass());
-        assertEquals("valueClass", Double.class,  a3.getValueClass());
-
-        assertEquals("minimumOccurs",   1, a0.getMinimumOccurs());
-        assertEquals("minimumOccurs",   1, a1.getMinimumOccurs());
-        assertEquals("minimumOccurs",   1, a2.getMinimumOccurs());
-        assertEquals("minimumOccurs",   5, a3.getMinimumOccurs());
-
-        assertEquals("maximumOccurs",   1, a0.getMaximumOccurs());
-        assertEquals("maximumOccurs",   1, a1.getMaximumOccurs());
-        assertEquals("maximumOccurs",   1, a2.getMaximumOccurs());
-        assertEquals("maximumOccurs",  50, a3.getMaximumOccurs());
-
-        assertEquals("defaultValue", null, a0.getDefaultValue());
-        assertEquals("defaultValue", null, a1.getDefaultValue());
-        assertEquals("defaultValue", null, a2.getDefaultValue());
-        assertEquals("defaultValue", 10.0, a3.getDefaultValue());
-
-        assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a0));
-        assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a1));
-        assertTrue ("characterizedByCRS", AttributeConvention.characterizedByCRS(a2));
-        assertFalse("characterizedByCRS", AttributeConvention.characterizedByCRS(a3));
-    }
-
-    /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one property is an identifier
-     * and another property is the geometry.
-     */
-    @Test
-    @DependsOnMethod("testAddAttribute")
-    public void testAddIdentifierAndGeometry() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("scope", "test");
-        builder.setIdentifierDelimiters("-", "pref.", null);
-        builder.addAttribute(String.class).setName("name")
-                .addRole(AttributeRole.IDENTIFIER_COMPONENT);
-        builder.addAttribute(Geometry.class).setName("shape")
-                .setCRS(HardCodedCRS.WGS84)
-                .addRole(AttributeRole.DEFAULT_GEOMETRY);
-
-        final FeatureType type = builder.build();
-        assertEquals("name", "scope:test", type.getName().toString());
-        assertFalse ("isAbstract", type.isAbstract());
-
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final PropertyType a0 = it.next();
-        final PropertyType a1 = it.next();
-        final PropertyType a2 = it.next();
-        final PropertyType a3 = it.next();
-        final PropertyType a4 = it.next();
-        assertFalse("properties count", it.hasNext());
-
-        assertEquals("name", AttributeConvention.IDENTIFIER_PROPERTY, a0.getName());
-        assertEquals("name", AttributeConvention.ENVELOPE_PROPERTY,   a1.getName());
-        assertEquals("name", AttributeConvention.GEOMETRY_PROPERTY,   a2.getName());
-        assertEquals("name", "name",                                  a3.getName().toString());
-        assertEquals("name", "shape",                                 a4.getName().toString());
-    }
-
-    /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one attribute is an identifier that already has
-     * the {@code "@identifier"} name. This is called "anonymous" because identifiers with an explicit name in the
-     * data file should use that name instead in the feature type.
-     */
-    @Test
-    @DependsOnMethod("testAddIdentifierAndGeometry")
-    public void testAddAnonymousIdentifier() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("City");
-        builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY).addRole(AttributeRole.IDENTIFIER_COMPONENT);
-        builder.addAttribute(Integer.class).setName("population");
-        final FeatureType type = builder.build();
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final PropertyType a0 = it.next();
-        final PropertyType a1 = it.next();
-        assertFalse("properties count", it.hasNext());
-        assertEquals("name", AttributeConvention.IDENTIFIER_PROPERTY, a0.getName());
-        assertEquals("type", String.class,  ((AttributeType<?>) a0).getValueClass());
-        assertEquals("name", "population", a1.getName().toString());
-        assertEquals("type", Integer.class, ((AttributeType<?>) a1).getValueClass());
-    }
-
-    /**
-     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one attribute is a geometry that already has
-     * the {@code "@geometry"} name. This is called "anonymous" because geometries with an explicit name in the
-     * data file should use that name instead in the feature type.
-     */
-    @Test
-    @DependsOnMethod("testAddIdentifierAndGeometry")
-    public void testAddAnonymousGeometry() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("City");
-        builder.addAttribute(Point.class).setName(AttributeConvention.GEOMETRY_PROPERTY).addRole(AttributeRole.DEFAULT_GEOMETRY);
-        builder.addAttribute(Integer.class).setName("population");
-        final FeatureType type = builder.build();
-        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
-        final PropertyType a0 = it.next();
-        final PropertyType a1 = it.next();
-        final PropertyType a2 = it.next();
-        assertFalse("properties count", it.hasNext());
-        assertEquals("name", AttributeConvention.ENVELOPE_PROPERTY, a0.getName());
-        assertEquals("name", AttributeConvention.GEOMETRY_PROPERTY, a1.getName());
-        assertEquals("type", Point.class,   ((AttributeType<?>) a1).getValueClass());
-        assertEquals("name", "population", a2.getName().toString());
-        assertEquals("type", Integer.class, ((AttributeType<?>) a2).getValueClass());
-    }
-
-    /**
-     * Tests creation of a builder from an existing feature type.
-     * This method also acts as a test of {@code FeatureTypeBuilder} getter methods.
-     */
-    @Test
-    public void testCreateFromTemplate() {
-        final FeatureTypeBuilder builder = new FeatureTypeBuilder(DefaultFeatureTypeTest.capital());
-        assertEquals("name",       "Capital", builder.getName().toString());
-        assertEquals("superTypes", "City",    TestUtilities.getSingleton(builder.getSuperTypes()).getName().toString());
-        assertFalse ("isAbstract",            builder.isAbstract());
+        /*
+         * Verify the characteristic created by the builder.
+         */
+        final AttributeType<?> att = newb.build();
+        assertEquals("name",          "stddev",           att.getName().toString());
+        assertEquals("definition",    "test definition",  att.getDefinition().toString());
+        assertEquals("description",   "test description", att.getDescription().toString());
+        assertEquals("designation",   "test designation", att.getDesignation().toString());
+        assertEquals("valueClass",    Float.class,        att.getValueClass());
+        assertEquals("defaultValue",  Float.valueOf(2),   att.getDefaultValue());
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -23,6 +23,7 @@ import org.apache.sis.internal.feature.A
 import org.apache.sis.feature.DefaultFeatureTypeTest;
 import org.apache.sis.referencing.crs.HardCodedCRS;
 import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestUtilities;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -44,34 +45,13 @@ import org.opengis.feature.PropertyType;
  * @version 0.8
  * @module
  */
+@DependsOn(AttributeTypeBuilderTest.class)
 public final strictfp class FeatureTypeBuilderTest extends TestCase {
     /**
-     * Tests a {@code AttributeTypeBuilder} with the minimum number of parameters.
+     * Tests with the minimum number of parameters (no property and no super type).
      */
     @Test
-    public void testAttributeTypeBuilderInitialization() {
-        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
-        assertEquals("default name", "string", builder.getName().toString());
-
-        builder.setName("myScope", "myName");
-        final AttributeType<?> att = (AttributeType<?>) builder.build();
-
-        assertEquals("name", "myScope:myName",   att.getName().toString());
-        assertEquals("valueClass", String.class, att.getValueClass());
-        assertNull  ("defaultValue",             att.getDefaultValue());
-        assertNull  ("definition",               att.getDefinition());
-        assertNull  ("description",              att.getDescription());
-        assertNull  ("designation",              att.getDesignation());
-        assertEquals("minimumOccurs", 1,         att.getMinimumOccurs());
-        assertEquals("maximumOccurs", 1,         att.getMaximumOccurs());
-    }
-
-    /**
-     * Tests a {@code FeatureTypeBuilder} with the minimum number of parameters (no property and no super type).
-     */
-    @Test
-    @DependsOnMethod("testAttributeTypeBuilderInitialization")
-    public void testFeatureTypeBuilderInitialization() {
+    public void testInitialization() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
         try {
             builder.build();
@@ -80,7 +60,7 @@ public final strictfp class FeatureTypeB
             final String message = ex.getMessage();
             assertTrue(message, message.contains("name"));
         }
-        builder.setName("scope", "test");
+        assertSame(builder, builder.setName("scope", "test"));
         final FeatureType type = builder.build();
 
         assertEquals("name", "scope:test",   type.getName().toString());
@@ -90,46 +70,17 @@ public final strictfp class FeatureTypeB
     }
 
     /**
-     * Test creation of a single attribute.
-     */
-    @Test
-    @DependsOnMethod("testAttributeTypeBuilderInitialization")
-    public void testAttributeTypeBuilder() {
-        final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
-        builder.setName        ("myScope", "myName");
-        builder.setDefinition  ("test definition");
-        builder.setDesignation ("test designation");
-        builder.setDescription ("test description");
-        builder.setDefaultValue("test default value.");
-        builder.setCardinality(10, 60);
-        builder.setMaximalLength(80);
-        final AttributeType<?> att = (AttributeType<?>) builder.build();
-
-        assertEquals("name",          "myScope:myName",      att.getName().toString());
-        assertEquals("definition",    "test definition",     att.getDefinition().toString());
-        assertEquals("description",   "test description",    att.getDescription().toString());
-        assertEquals("designation",   "test designation",    att.getDesignation().toString());
-        assertEquals("valueClass",    String.class,          att.getValueClass());
-        assertEquals("defaultValue",  "test default value.", att.getDefaultValue());
-        assertEquals("minimumOccurs", 10,                    att.getMinimumOccurs());
-        assertEquals("maximumOccurs", 60,                    att.getMaximumOccurs());
-        assertTrue  ("characterizedByMaximalLength", AttributeConvention.characterizedByMaximalLength(att));
-        assertEquals("maximalLengthCharacteristic", Integer.valueOf(80),
-                AttributeConvention.getMaximalLengthCharacteristic(att.newInstance()));
-    }
-
-    /**
      * Tests {@link FeatureTypeBuilder#addAttribute(Class)}.
      */
     @Test
-    @DependsOnMethod("testAttributeTypeBuilder")
+    @DependsOnMethod("testInitialization")
     public void testAddAttribute() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("myScope", "myName");
-        builder.setDefinition ("test definition");
-        builder.setDesignation("test designation");
-        builder.setDescription("test description");
-        builder.setAbstract(true);
+        assertSame(builder, builder.setName("myScope", "myName"));
+        assertSame(builder, builder.setDefinition ("test definition"));
+        assertSame(builder, builder.setDesignation("test designation"));
+        assertSame(builder, builder.setDescription("test description"));
+        assertSame(builder, builder.setAbstract(true));
         builder.addAttribute(String .class).setName("name");
         builder.addAttribute(Integer.class).setName("age");
         builder.addAttribute(Point  .class).setName("location").setCRS(HardCodedCRS.WGS84);
@@ -188,8 +139,8 @@ public final strictfp class FeatureTypeB
     @DependsOnMethod("testAddAttribute")
     public void testAddIdentifierAndGeometry() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("scope", "test");
-        builder.setIdentifierDelimiters("-", "pref.", null);
+        assertSame(builder, builder.setName("scope", "test"));
+        assertSame(builder, builder.setIdentifierDelimiters("-", "pref.", null));
         builder.addAttribute(String.class).setName("name")
                 .addRole(AttributeRole.IDENTIFIER_COMPONENT);
         builder.addAttribute(Geometry.class).setName("shape")
@@ -224,7 +175,7 @@ public final strictfp class FeatureTypeB
     @DependsOnMethod("testAddIdentifierAndGeometry")
     public void testAddAnonymousIdentifier() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("City");
+        assertSame(builder, builder.setName("City"));
         builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY).addRole(AttributeRole.IDENTIFIER_COMPONENT);
         builder.addAttribute(Integer.class).setName("population");
         final FeatureType type = builder.build();
@@ -247,7 +198,7 @@ public final strictfp class FeatureTypeB
     @DependsOnMethod("testAddIdentifierAndGeometry")
     public void testAddAnonymousGeometry() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
-        builder.setName("City");
+        assertSame(builder, builder.setName("City"));
         builder.addAttribute(Point.class).setName(AttributeConvention.GEOMETRY_PROPERTY).addRole(AttributeRole.DEFAULT_GEOMETRY);
         builder.addAttribute(Integer.class).setName("population");
         final FeatureType type = builder.build();

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java?rev=1754952&r1=1754951&r2=1754952&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java [UTF-8] Tue Aug  2 15:30:17 2016
@@ -52,6 +52,8 @@ import org.junit.BeforeClass;
     org.apache.sis.filter.DefaultLiteralTest.class,
     org.apache.sis.filter.DefaultPropertyNameTest.class,
     org.apache.sis.internal.feature.AttributeConventionTest.class,
+    org.apache.sis.feature.builder.CharacteristicTypeBuilderTest.class,
+    org.apache.sis.feature.builder.AttributeTypeBuilderTest.class,
     org.apache.sis.feature.builder.FeatureTypeBuilderTest.class
 })
 public final strictfp class FeatureTestSuite extends TestSuite {



Mime
View raw message