sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1420561 - in /sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis: internal/jaxb/gco/LocalNameAdapter.java internal/jaxb/gmd/TextGroup.java internal/util/DefaultFactories.java util/type/DefaultNameFactory.java util/type/Types.java
Date Wed, 12 Dec 2012 08:18:18 GMT
Author: desruisseaux
Date: Wed Dec 12 08:18:17 2012
New Revision: 1420561

URL: http://svn.apache.org/viewvc?rev=1420561&view=rev
Log:
Moved the DefaultNameFactory.toArray() method to a more implementation-neutral Types.asGenericNames(...)
method.
Provides a common place where to get the unique factory instance (maybe temporarily, to be
revisited when we will
have explored dependency injection).

Added:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java
  (with props)
Modified:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/TextGroup.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/DefaultNameFactory.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/Types.java

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java?rev=1420561&r1=1420560&r2=1420561&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gco/LocalNameAdapter.java
Wed Dec 12 08:18:17 2012
@@ -51,14 +51,14 @@ public final class LocalNameAdapter exte
 
     /**
      * Fetches the name factory. The returned factory shall be an instance
-     * of {@link DefaultNameFactory}, not a subclass, because we are going
+     * of {@code DefaultNameFactory}, not a subclass, because we are going
      * to cast the created {@code GenericName} to {@code AbstractName} for
      * XML marshalling and we know that {@code DefaultNameFactory} creates
      * the expected type. A subclass could create other types, so we are
      * better to avoid them.
      */
     static NameFactory getNameFactory() {
-        throw new UnsupportedOperationException(); // TODO
+        return org.apache.sis.internal.util.DefaultFactories.NAMES;
     }
 
     /**

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/TextGroup.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/TextGroup.java?rev=1420561&r1=1420560&r2=1420561&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/TextGroup.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/TextGroup.java
Wed Dec 12 08:18:17 2012
@@ -110,18 +110,30 @@ final class TextGroup {
 
     /**
      * Returns a string representation of this text group for debugging purpose.
+     * Example:
+     *
+     * {@preformat
+     *   TextGroup
+     *   ├─ LocalisedCharacterString[#locale-eng, “A text”]
+     *   └─ LocalisedCharacterString[#locale-fra, “Un texte”]
+     * }
      *
      * @see LocalisedCharacterString#toString()
      */
     @Override
     public String toString() {
-        final StringBuilder buffer = new StringBuilder(160).append(getClass().getSimpleName());
+        final String lineSeparator = System.lineSeparator();
+        final StringBuilder buffer = new StringBuilder(160).append(getClass().getSimpleName()).append(lineSeparator);
         if (localized != null) {
-            final String lineSeparator = System.lineSeparator();
+            int corner = 0;
             for (LocalisedCharacterString string : localized) {
-                buffer.append(lineSeparator).append("  ").append(string);
+                corner = buffer.length();
+                buffer.append("├─ ").append(string).append(lineSeparator);
+            }
+            if (corner != 0) {
+                buffer.setCharAt(corner, '└');
             }
         }
-        return buffer.append(']').toString();
+        return buffer.toString();
     }
 }

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java?rev=1420561&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java
(added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java
Wed Dec 12 08:18:17 2012
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.util;
+
+import org.opengis.util.NameFactory;
+import org.apache.sis.util.Static;
+import org.apache.sis.util.type.DefaultNameFactory;
+
+
+/**
+ * Default factories defined in the {@code sis-utility} module.
+ * This is a temporary placeholder until we leverage the "dependency injection" pattern.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+public final class DefaultFactories extends Static {
+    /**
+     * The factory to use for creating names.
+     */
+    public static final NameFactory NAMES = new DefaultNameFactory();
+
+    /**
+     * Do not allow instantiation of this class.
+     */
+    private DefaultFactories() {
+    }
+}

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/DefaultFactories.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/DefaultNameFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/DefaultNameFactory.java?rev=1420561&r1=1420560&r2=1420561&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/DefaultNameFactory.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/DefaultNameFactory.java
Wed Dec 12 08:18:17 2012
@@ -21,7 +21,6 @@ import java.util.List;
 import java.util.Arrays;
 import java.util.Locale;
 import java.util.ArrayList;
-import java.util.Collection;
 import net.jcip.annotations.ThreadSafe;
 
 import org.opengis.util.TypeName;
@@ -31,7 +30,6 @@ import org.opengis.util.MemberName;
 import org.opengis.util.GenericName;
 import org.opengis.util.NameFactory;
 import org.opengis.util.InternationalString;
-import org.opengis.metadata.Identifier;
 
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.NullArgumentException;
@@ -87,6 +85,8 @@ public class DefaultNameFactory extends 
 
     /**
      * Creates an international string from a set of strings in different locales.
+     *
+     * @see Types#toInternationalString(CharSequence)
      */
     @Override
     public InternationalString createInternationalString(final Map<Locale,String> strings)
{
@@ -280,82 +280,4 @@ public class DefaultNameFactory extends 
         }
         return createGenericName(scope, names.toArray(new String[names.size()]));
     }
-
-    /**
-     * Creates a generic name from the given value. The value may be an instance of
-     * {@link GenericName}, {@link Identifier} or {@link CharSequence}. If the given
-     * object is not recognized, then this method returns {@code null}.
-     *
-     * @param  value The object to convert.
-     * @return The converted object, or {@code null} if {@code value} is not convertible.
-     */
-    private GenericName createFromObject(final Object value) {
-        ensureNonNull("value", value);
-        if (value instanceof GenericName) {
-            return (GenericName) value;
-        }
-        if (value instanceof Identifier) {
-            return parseGenericName(null, ((Identifier) value).getCode());
-        }
-        if (value instanceof CharSequence) {
-            return parseGenericName(null, (CharSequence) value);
-        }
-        return null;
-    }
-
-    /**
-     * Converts the given value to an array of generic names. If the given value is an instance
of
-     * {@link GenericName}, {@link String} or any other type enumerated below, then it is
converted
-     * and returned in an array of length 1. If the given value is an array or a collection,
then an
-     * array of same length is returned where each element has been converted.
-     *
-     * <p>Allowed types or element types are:</p>
-     * <ul>
-     *   <li>{@link GenericName}, to be casted and returned as-is.</li>
-     *   <li>{@link CharSequence} (usually a {@link String} or an {@link InternationalString}),
to
-     *       be parsed as a generic name using the
-     *       {@value org.apache.sis.util.type.DefaultNameSpace#DEFAULT_SEPARATOR} separator.</li>
-     *   <li>{@link Identifier}, its {@linkplain Identifier#getCode() code} to be parsed
as a generic name
-     *       using the {@value org.apache.sis.util.type.DefaultNameSpace#DEFAULT_SEPARATOR}
separator.</li>
-     * </ul>
-     *
-     * @param  value The object to cast into an array of generic names.
-     * @return The generic names. May be a direct reference to {@code value}.
-     * @throws NullArgumentException if {@code value} is null.
-     * @throws ClassCastException if {@code value} can't be casted.
-     */
-    public GenericName[] toArray(Object value) throws ClassCastException {
-        GenericName name = createFromObject(value);
-        if (name != null) {
-            return new GenericName[] {
-                name
-            };
-        }
-        /*
-         * Above code checked for a singleton. Now check for a collection or an array.
-         */
-        final Object[] values;
-        if (value instanceof Collection<?>) {
-            values = ((Collection<?>) value).toArray();
-        } else if (value instanceof Object[]) {
-            values = (Object[]) value;
-        } else {
-            throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_2,
-                    "value", value.getClass()));
-        }
-        if (values instanceof GenericName[]) {
-            return (GenericName[]) values;
-        }
-        final GenericName[] names = new GenericName[values.length];
-        for (int i=0; i<values.length; i++) {
-            value = values[i];
-            name = createFromObject(value);
-            if (name == null) {
-                throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_2,
-                        "value[" + i + ']', value.getClass()));
-            }
-            names[i] = name;
-        }
-        return names;
-    }
 }

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/Types.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/Types.java?rev=1420561&r1=1420560&r2=1420561&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/Types.java (original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/util/type/Types.java Wed Dec
12 08:18:17 2012
@@ -18,6 +18,7 @@ package org.apache.sis.util.type;
 
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Collection;
 import java.util.Locale;
 import java.util.Properties;
 import java.util.ResourceBundle;
@@ -27,10 +28,15 @@ import java.io.InputStream;
 
 import org.opengis.annotation.UML;
 import org.opengis.util.CodeList;
+import org.opengis.util.NameFactory;
+import org.opengis.util.GenericName;
 import org.opengis.util.InternationalString;
+import org.opengis.metadata.Identifier;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.logging.Logging;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.collection.BackingStoreException;
+import org.apache.sis.internal.util.DefaultFactories;
 
 
 /**
@@ -186,6 +192,8 @@ public final class Types extends Static 
      * @param  string The characters sequence to convert, or {@code null}.
      * @return The given sequence as an international string,
      *         or {@code null} if the given sequence was null.
+     *
+     * @see DefaultNameFactory#createInternationalString(Map)
      */
     public static InternationalString toInternationalString(final CharSequence string) {
         if (string == null || string instanceof InternationalString) {
@@ -218,4 +226,94 @@ public final class Types extends Static 
         }
         return copy;
     }
+
+    /**
+     * Converts the given value to an array of generic names. If the given value is an instance
of
+     * {@link GenericName}, {@link String} or any other type enumerated below, then it is
converted
+     * and returned in an array of length 1. If the given value is an array or a collection,
then an
+     * array of same length is returned where each element has been converted.
+     *
+     * <p>Allowed types or element types are:</p>
+     * <ul>
+     *   <li>{@link GenericName}, to be casted and returned as-is.</li>
+     *   <li>{@link CharSequence} (usually a {@link String} or an {@link InternationalString}),
+     *       to be parsed as a generic name using the
+     *       {@value org.apache.sis.util.type.DefaultNameSpace#DEFAULT_SEPARATOR} separator.</li>
+     *   <li>{@link Identifier}, its {@linkplain Identifier#getCode() code} to be parsed
as a generic name
+     *       using the {@value org.apache.sis.util.type.DefaultNameSpace#DEFAULT_SEPARATOR}
separator.</li>
+     * </ul>
+     *
+     * If {@code value} is an array or a collection containing {@code null} elements,
+     * then the corresponding element in the returned array will also be {@code null}.
+     *
+     * @param  value The object to cast into an array of generic names, or {@code null}.
+     * @param  factory The factory to use for creating names, or {@code null} for the default.
+     * @return The generic names, or {@code null} if the given {@code value} was null.
+     *         Note that it may be the {@code value} reference itself casted to {@code GenericName[]}.
+     * @throws ClassCastException if {@code value} can't be casted.
+     */
+    public static GenericName[] toGenericNames(Object value, NameFactory factory) throws
ClassCastException {
+        if (value == null) {
+            return null;
+        }
+        if (factory == null) {
+            factory = DefaultFactories.NAMES;
+        }
+        GenericName name = toGenericName(value, factory);
+        if (name != null) {
+            return new GenericName[] {
+                name
+            };
+        }
+        /*
+         * Above code checked for a singleton. Now check for a collection or an array.
+         */
+        final Object[] values;
+        if (value instanceof Object[]) {
+            values = (Object[]) value;
+            if (values instanceof GenericName[]) {
+                return (GenericName[]) values;
+            }
+        } else if (value instanceof Collection<?>) {
+            values = ((Collection<?>) value).toArray();
+        } else {
+            throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_2,
+                    "value", value.getClass()));
+        }
+        final GenericName[] names = new GenericName[values.length];
+        for (int i=0; i<values.length; i++) {
+            value = values[i];
+            if (value != null) {
+                name = toGenericName(value, factory);
+                if (name == null) {
+                    throw new ClassCastException(Errors.format(Errors.Keys.IllegalArgumentClass_2,
+                            "value[" + i + ']', value.getClass()));
+                }
+                names[i] = name;
+            }
+        }
+        return names;
+    }
+
+    /**
+     * Creates a generic name from the given value. The value may be an instance of
+     * {@link GenericName}, {@link Identifier} or {@link CharSequence}. If the given
+     * object is not recognized, then this method returns {@code null}.
+     *
+     * @param  value The object to convert.
+     * @param  factory The factory to use for creating names.
+     * @return The converted object, or {@code null} if {@code value} is not convertible.
+     */
+    private static GenericName toGenericName(final Object value, final NameFactory factory)
{
+        if (value instanceof GenericName) {
+            return (GenericName) value;
+        }
+        if (value instanceof Identifier) {
+            return factory.parseGenericName(null, ((Identifier) value).getCode());
+        }
+        if (value instanceof CharSequence) {
+            return factory.parseGenericName(null, (CharSequence) value);
+        }
+        return null;
+    }
 }



Mime
View raw message