sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1391467 - in /sis/trunk/sis-utility/src: main/java/org/apache/sis/util/ main/java/org/apache/sis/util/resources/ main/java/org/apache/sis/util/type/ test/java/org/apache/sis/util/ test/java/org/apache/sis/util/resources/ test/java/org/apac...
Date Fri, 28 Sep 2012 13:53:19 GMT
Author: desruisseaux
Date: Fri Sep 28 13:53:18 2012
New Revision: 1391467

URL: http://svn.apache.org/viewvc?rev=1391467&view=rev
Log:
Fixed Locales fields which needed to be static, fixed documentation, added tests, and added
Errors.formatInternational method.

Added:
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java   (with props)
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/ResourceInternationalString.java
      - copied, changed from r1391413, sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java
    sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java   (with props)
    sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/
    sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java   (with props)
Modified:
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Locales.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/AbstractInternationalString.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/DefaultInternationalString.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java
    sis/trunk/sis-utility/src/test/java/org/apache/sis/util/resources/IndexedResourceBundleTest.java

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Locales.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Locales.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Locales.java (original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Locales.java Fri Sep 28 13:53:18
2012
@@ -89,13 +89,13 @@ public final class Locales extends Stati
     /**
      * All locales available on the JavaVM.
      */
-    public final Locales ALL = new Locales();
+    public static final Locales ALL = new Locales();
 
     /**
      * Only locales available in the Apache SIS library. They are the locales for which localized
      * resources are provided in the {@link org.apache.sis.util.resources} package.
      */
-    public final Locales SIS = new Locales();
+    public static final Locales SIS = new Locales();
 
     /**
      * Do not allow instantiation of this class,

Added: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java?rev=1391467&view=auto
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java (added)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java Fri Sep 28 13:53:18
2012
@@ -0,0 +1,134 @@
+/*
+ * 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.util;
+
+import java.util.Arrays;
+
+
+/**
+ * Miscellaneous static methods.
+ *
+ * @author Martin Desruisseaux (IRD, Geomatys)
+ * @since   0.3 (derived from geotk-1.2)
+ * @version 0.3
+ * @module
+ */
+public final class Utilities extends Static {
+    /**
+     * Do not allow object creation.
+     */
+    private Utilities() {
+    }
+
+    /**
+     * Returns {@code true} if the given floats are equals. Positive and negative zero are
+     * considered different, while a {@link Float#NaN NaN} value is considered equal to all
+     * other NaN values.
+     *
+     * @param o1 The first value to compare.
+     * @param o2 The second value to compare.
+     * @return {@code true} if both values are equal.
+     *
+     * @see Float#equals(Object)
+     */
+    public static boolean equals(final float o1, final float o2) {
+        return Float.floatToIntBits(o1) == Float.floatToIntBits(o2);
+    }
+
+    /**
+     * Returns {@code true} if the given doubles are equals. Positive and negative zero are
+     * considered different, while a {@link Double#NaN NaN} value is considered equal to
all
+     * other NaN values.
+     *
+     * @param o1 The first value to compare.
+     * @param o2 The second value to compare.
+     * @return {@code true} if both values are equal.
+     *
+     * @see Double#equals(Object)
+     */
+    public static boolean equals(final double o1, final double o2) {
+        return Double.doubleToLongBits(o1) == Double.doubleToLongBits(o2);
+    }
+
+    /**
+     * Returns a hash code for the specified object, which may be an array.
+     * This method returns one of the following values:
+     * <p>
+     * <ul>
+     *   <li>If the supplied object is {@code null}, then this method returns 0.</li>
+     *   <li>Otherwise if the object is an array of objects, then
+     *       {@link Arrays#deepHashCode(Object[])} is invoked.</li>
+     *   <li>Otherwise if the object is an array of primitive type, then the corresponding
+     *       {@link Arrays#hashCode(double[]) Arrays.hashCode(...)} method is invoked.</li>
+     *   <li>Otherwise {@link Object#hashCode()} is invoked.<li>
+     * </ul>
+     * <p>
+     * This method should be invoked <strong>only</strong> if the object type
is declared
+     * exactly as {@code Object}, not as some subtype like {@code Object[]}, {@code String}
or
+     * {@code float[]}. In the later cases, use the appropriate {@link Arrays} method instead.
+     *
+     * @param object The object to compute hash code. May be {@code null}.
+     * @return The hash code of the given object.
+     */
+    public static int deepHashCode(final Object object) {
+        if (object == null) {
+            return 0;
+        }
+        if (object instanceof Object[])  return Arrays.deepHashCode((Object[])  object);
+        if (object instanceof double[])  return Arrays.hashCode    ((double[])  object);
+        if (object instanceof float[])   return Arrays.hashCode    ((float[])   object);
+        if (object instanceof long[])    return Arrays.hashCode    ((long[])    object);
+        if (object instanceof int[])     return Arrays.hashCode    ((int[])     object);
+        if (object instanceof short[])   return Arrays.hashCode    ((short[])   object);
+        if (object instanceof byte[])    return Arrays.hashCode    ((byte[])    object);
+        if (object instanceof char[])    return Arrays.hashCode    ((char[])    object);
+        if (object instanceof boolean[]) return Arrays.hashCode    ((boolean[]) object);
+        return object.hashCode();
+    }
+
+    /**
+     * Returns a string representation of the specified object, which may be an array.
+     * This method returns one of the following values:
+     * <p>
+     * <ul>
+     *   <li>If the object is an array of objects, then
+     *       {@link Arrays#deepToString(Object[])} is invoked.</li>
+     *   <li>Otherwise if the object is an array of primitive type, then the corresponding
+     *       {@link Arrays#toString(double[]) Arrays.toString(...)} method is invoked.</li>
+     *   <li>Otherwise {@link String#valueOf(Object)} is invoked.</li>
+     * </ul>
+     * <p>
+     * This method should be invoked <strong>only</strong> if the object type
is declared
+     * exactly as {@code Object}, not as some subtype like {@code Object[]}, {@code Number}
or
+     * {@code float[]}. In the later cases, use the appropriate {@link Arrays} method instead.
+     *
+     * @param object The object to format as a string. May be {@code null}.
+     * @return A string representation of the given object.
+     */
+    public static String deepToString(final Object object) {
+        if (object instanceof Object[])  return Arrays.deepToString((Object[]) object);
+        if (object instanceof double[])  return Arrays.toString    ((double[]) object);
+        if (object instanceof float[])   return Arrays.toString    ((float[]) object);
+        if (object instanceof long[])    return Arrays.toString    ((long[]) object);
+        if (object instanceof int[])     return Arrays.toString    ((int[]) object);
+        if (object instanceof short[])   return Arrays.toString    ((short[]) object);
+        if (object instanceof byte[])    return Arrays.toString    ((byte[]) object);
+        if (object instanceof char[])    return Arrays.toString    ((char[]) object);
+        if (object instanceof boolean[]) return Arrays.toString    ((boolean[]) object);
+        return String.valueOf(object);
+    }
+}

Propchange: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Utilities.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java (original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java Fri Sep
28 13:53:18 2012
@@ -18,6 +18,7 @@ package org.apache.sis.util.resources;
 
 import java.util.Locale;
 import java.util.MissingResourceException;
+import org.opengis.util.InternationalString;
 
 
 /**
@@ -218,4 +219,59 @@ public final class Errors extends Indexe
     {
         return getResources(null).getString(key, arg0, arg1, arg2, arg3);
     }
+
+    /**
+     * The international string to be returned by {@link formatInternational}.
+     */
+    private static final class International extends ResourceInternationalString {
+        private static final long serialVersionUID = -229348959712294902L;
+
+        International(int key)              {super(key);}
+        International(int key, Object args) {super(key, args);}
+        @Override IndexedResourceBundle getBundle(Locale locale) {
+            return getResources(locale);
+        }
+    }
+
+    /**
+     * Gets an international string for the given key. This method does not check for the
key
+     * validity. If the key is invalid, then a {@link MissingResourceException} may be thrown
+     * when a {@link InternationalString#toString(Locale)} method is invoked.
+     *
+     * @param  key The key for the desired string.
+     * @return An international string for the given key.
+     */
+    public static InternationalString formatInternational(final int key) {
+        return new International(key);
+    }
+
+    /**
+     * Gets an international string for the given key. This method does not check for the
key
+     * validity. If the key is invalid, then a {@link MissingResourceException} may be thrown
+     * when a {@link InternationalString#toString(Locale)} method is invoked.
+     *
+     * {@note This method is redundant with the one expecting <code>Object...</code>,
but avoid
+     *        the creation of a temporary array. There is no risk of confusion since the
two
+     *        methods delegate their work to the same <code>format</code> method
anyway.}
+     *
+     * @param  key The key for the desired string.
+     * @param  arg Values to substitute to "{0}".
+     * @return An international string for the given key.
+     */
+    public static InternationalString formatInternational(final int key, final Object arg)
{
+        return new International(key, arg);
+    }
+
+    /**
+     * Gets an international string for the given key. This method does not check for the
key
+     * validity. If the key is invalid, then a {@link MissingResourceException} may be thrown
+     * when a {@link InternationalString#toString(Locale)} method is invoked.
+     *
+     * @param  key  The key for the desired string.
+     * @param  args Values to substitute to "{0}", "{1}", <i>etc</i>.
+     * @return An international string for the given key.
+     */
+    public static InternationalString formatInternational(final int key, final Object...
args) {
+        return new International(key, args);
+    }
 }

Copied: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/ResourceInternationalString.java
(from r1391413, sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java)
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/ResourceInternationalString.java?p2=sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/ResourceInternationalString.java&p1=sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java&r1=1391413&r2=1391467&rev=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java
(original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/resources/ResourceInternationalString.java
Fri Sep 28 13:53:18 2012
@@ -14,106 +14,81 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sis.util.type;
+package org.apache.sis.util.resources;
 
 import java.io.Serializable;
 import java.util.Locale;
-import java.util.Properties;
-import java.util.ResourceBundle;
 import java.util.MissingResourceException;
 import net.jcip.annotations.Immutable;
-import org.opengis.util.InternationalString;
-
-import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
+import org.apache.sis.util.type.AbstractInternationalString;
 
 // Related to JDK7
+import org.apache.sis.util.Utilities;
 import org.apache.sis.internal.Objects;
 
 
 /**
- * An {@linkplain InternationalString international string} backed by a {@linkplain ResourceBundle
- * resource bundle}. A resource bundle can be a Java class or a {@linkplain Properties properties}
- * file, one for each language. The constructor expects the fully qualified class name of
the base
- * resource bundle (the one used when no resource was found in the client's language). The
appropriate
- * resource bundle is loaded at runtime for the client's language by looking for a class
or a
- * properties file with the right suffix, for example {@code "_en"} for English or {@code
"_fr"}
- * for French. This mechanism is explained in J2SE javadoc for the
- * {@link ResourceBundle#getBundle(String,Locale,ClassLoader) getBundle} static method.
- * <p>
- * <b>Example:</b> If a file named "{@code MyResources.properties}" exists in
the package
- * {@code org.apache.sis.mypackage} and contains a line like "{@code MyKey = some value}",
- * then an international string for "{@code some value}" can be created using the following
- * code:
- *
- * {@preformat java
- *     InternationalString value =
- *         new ResourceInternationalString("org.apache.sis.mypackage.MyResources", "MyKey");
- * }
- *
- * The "{@code some value}" string will be localized if the required properties files exist,
for
- * example "{@code MyResources_fr.properties}" for French, "{@code MyResources_it.properties}"
- * for Italian, <i>etc.</i>
+ * A copy of {@link org.apache.sis.util.type.ResourceInternationalString} specialized for
+ * {@link IndexedResourceBundle}. Compared to the public class, this specialization works
+ * with integer resource keys and accepts arguments.
  *
- * @author  Martin Desruisseaux (IRD, Geomatys)
- * @author  Johann Sorel (Geomatys)
- * @since   0.3 (derived from geotk-2.1)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
  * @version 0.3
  * @module
  */
 @Immutable
-public class ResourceInternationalString extends AbstractInternationalString implements Serializable
{
+abstract class ResourceInternationalString extends AbstractInternationalString implements
Serializable {
     /**
      * Serial number for inter-operability with different versions.
      */
-    private static final long serialVersionUID = 6339944890723487336L;
-
-    /**
-     * The name of the resource bundle from which to fetch the string.
-     */
-    private final String resources;
+    private static final long serialVersionUID = 4744571031462678126L;
 
     /**
      * The key for the resource to fetch.
      */
-    private final String key;
+    private final int key;
 
     /**
-     * The class loader to use for loading the resources file, or {@code null} for the default
-     * class loader.
+     * The argument(s), or {@code Loader.class} if none. The choice of {@code Loader.class}
for
+     * meaning "no argument" is arbitrary - we just need a reference that the user is unlikely
+     * to known, and we need the referenced object to be serializable. We can not use the
+     * {@code null} value for "no argument" because the user may really wants to specify
+     * {@code null} as an argument value.
      */
-    private final ClassLoader loader;
+    private final Object arguments;
 
     /**
-     * Creates a new international string from the specified resource bundle and key.
+     * Creates a new international string for the given key.
      *
-     * @param resources The name of the resource bundle, as a fully qualified class name.
      * @param key The key for the resource to fetch.
      */
-    public ResourceInternationalString(final String resources, final String key) {
-        this(resources, key, null);
+    ResourceInternationalString(final int key) {
+        this.key  = key;
+        arguments = Loader.class;
     }
 
     /**
-     * Creates a new international string from the specified resource bundle, key and class
loader.
+     * Creates a new international string for the given key and arguments.
      *
-     * @param resources The name of the resource bundle, as a fully qualified class name.
      * @param key The key for the resource to fetch.
-     * @param loader The class loader to use for loading the resources file,
-     *        or {@code null} for the default class loader.
+     * @param The argument(s).
      */
-    public ResourceInternationalString(final String resources, final String key, final ClassLoader
loader) {
-        this.resources = resources;
-        this.key       = key;
-        this.loader    = loader;
-        ensureNonNull("resources", resources);
-        ensureNonNull("key",       key);
-
+    ResourceInternationalString(final int key, final Object arguments) {
+        this.key = key;
+        this.arguments = arguments;
     }
 
     /**
-     * Returns a string in the specified locale. If there is no string for the specified
-     * {@code locale}, then this method search for a string in an other locale as
-     * specified in the {@link ResourceBundle} class description.
+     * Returns the resource bundle for the given locale.
+     *
+     * @param  locale The locale for which to get the resource bundle.
+     * @return The resource bundle for the given locale.
+     */
+    abstract IndexedResourceBundle getBundle(final Locale locale);
+
+    /**
+     * Returns a string in the specified locale.
      *
      * @param  locale The locale to look for, or {@code null} for an unlocalized version.
      * @return The string in the specified locale, or in a default locale.
@@ -128,23 +103,10 @@ public class ResourceInternationalString
             // should invokes the 'toString()' method instead.
             locale = Locale.ENGLISH;
         }
-        return getBundle(locale).getString(key);
-    }
-
-    /**
-     * Returns the resource bundle for the given locale. The default implementation fetches
the
-     * bundle from the name given at {@linkplain #ResourceInternationalString(String,String)
-     * construction time}. Subclasses can override this method if they need to fetch the
-     * bundle in an other way.
-     *
-     * @param  locale The locale for which to get the resource bundle.
-     * @return The resource bundle for the given locale.
-     *
-     * @see ResourceBundle#getBundle(String,Locale)
-     */
-    protected ResourceBundle getBundle(final Locale locale) {
-        return (loader == null) ? ResourceBundle.getBundle(resources, locale) :
-                ResourceBundle.getBundle(resources, locale, loader);
+        final IndexedResourceBundle resources = getBundle(locale);
+        return (arguments == Loader.class)
+                ? resources.getString(key)
+                : resources.getString(key, arguments);
     }
 
     /**
@@ -155,10 +117,9 @@ public class ResourceInternationalString
      */
     @Override
     public boolean equals(final Object object) {
-        if (object != null && object.getClass() == getClass()) {
+        if (object instanceof ResourceInternationalString) {
             final ResourceInternationalString that = (ResourceInternationalString) object;
-            return Objects.equals(this.key,       that.key) &&
-                   Objects.equals(this.resources, that.resources);
+            return this.key == that.key && Objects.equals(this.arguments, that.arguments);
         }
         return false;
     }
@@ -170,6 +131,6 @@ public class ResourceInternationalString
      */
     @Override
     public int hashCode() {
-        return key.hashCode() ^ resources.hashCode() ^ (int) serialVersionUID;
+        return key ^ Utilities.deepHashCode(arguments) ^ (int) serialVersionUID;
     }
 }

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/AbstractInternationalString.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/AbstractInternationalString.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/AbstractInternationalString.java
(original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/AbstractInternationalString.java
Fri Sep 28 13:53:18 2012
@@ -25,7 +25,7 @@ import org.opengis.util.InternationalStr
 
 
 /**
- * Base class for {@linkplain String string} that has been internationalized into several
+ * Base class for {@linkplain String string}s that has been internationalized into several
  * {@linkplain Locale locales}. The {@link InternationalString} interface is used as a replacement
  * for the {@link String} class whenever an attribute needs to be internationalization capable.
  * The default value (as returned by {@link #toString()} and other {@link CharSequence} methods)
@@ -122,7 +122,7 @@ public abstract class AbstractInternatio
     /**
      * Returns this string in the given locale. If no string is available in the given locale,
      * then some default locale is used. The default locale is implementation-dependent.
-     * It may or may not be the {@linkplain Locale#getDefault() system default}).
+     * It may or may not be the {@linkplain Locale#getDefault() system default}.
      *
      * @param  locale The desired locale for the string to be returned,
      *         or {@code null} for a string in the implementation default locale.

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/DefaultInternationalString.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/DefaultInternationalString.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/DefaultInternationalString.java
(original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/DefaultInternationalString.java
Fri Sep 28 13:53:18 2012
@@ -152,12 +152,13 @@ public class DefaultInternationalString 
 
     /**
      * Adds a string for the given property key. This is a convenience method for constructing
an
-     * {@code AbstractInternationalString} during iteration through the
+     * {@code DefaultInternationalString} during iteration through the
      * {@linkplain java.util.Map.Entry entries} in a {@link Map}. It infers the {@link Locale}
      * from the property {@code key}, using the following steps:
+     * <p>
      * <ul>
-     *   <li>If the {@code key} do not starts with the specified {@code prefix}, then
-     *       this method do nothing and returns {@code false}.</li>
+     *   <li>If the {@code key} does not start with the specified {@code prefix}, then
+     *       this method does nothing and returns {@code false}.</li>
      *   <li>Otherwise, the characters after the {@code prefix} are parsed as an ISO
language
      *       and country code, and the {@link #add(Locale, String)} method is invoked.</li>
      * </ul>
@@ -167,13 +168,13 @@ public class DefaultInternationalString 
      * property key stands for remarks in {@linkplain Locale#CANADA_FRENCH French Canadian}.
      *
      * @param  prefix The prefix to skip at the beginning of the {@code key}.
-     * @param  key The property key.
-     * @param  string The localized string for the specified {@code key}.
+     * @param  key    The property key which will determined the locale.
+     * @param  value  The localized string for the specified {@code key}.
      * @return {@code true} if the key has been recognized, or {@code false} otherwise.
      * @throws IllegalArgumentException if the locale after the prefix is an illegal code,
      *         or a different string value was already set for the given locale.
      */
-    public boolean add(final String prefix, final String key, final String string)
+    public boolean add(final String prefix, final String key, final String value)
             throws IllegalArgumentException
     {
         if (key.startsWith(prefix)) {
@@ -186,7 +187,7 @@ public class DefaultInternationalString 
                     return false;
                 }
             }
-            add(locale, string);
+            add(locale, value);
             return true;
         }
         return false;
@@ -265,7 +266,7 @@ public class DefaultInternationalString 
      *       {@code null} locale, then that string is returned.</li>
      *   <li>Otherwise, acknowledging that UML identifiers in OGC/ISO specifications
are primarily
      *       expressed in the English language, this method looks for an English string as
an
-     *       approximation of a "unlocalized" string. The {@linkplain Locale#UK} variant
is
+     *       approximation of a "unlocalized" string. The {@link Locale#UK} variant is
      *       preferred because ISO specifications seem to use that language.</li>
      *   <li>If no English string was found, this method looks for a string for the
      *       {@linkplain Locale#getDefault() system default locale}.</li>
@@ -322,24 +323,24 @@ public class DefaultInternationalString 
     /**
      * Returns {@code true} if all localized texts stored in this international string are
      * contained in the specified object. More specifically:
-     *
+     * <p>
      * <ul>
-     *   <li><p>If {@code candidate} is an instance of {@link InternationalString},
then this method
+     *   <li>If {@code candidate} is an instance of {@link InternationalString}, then
this method
      *       returns {@code true} if, for all <var>{@linkplain Locale locale}</var>-<var>{@linkplain
      *       String string}</var> pairs contained in {@code this}, <code>candidate.{@linkplain
      *       InternationalString#toString(Locale) toString}(locale)</code> returns
a string
-     *       {@linkplain String#equals equals} to {@code string}.</p></li>
+     *       {@linkplain String#equals equals} to {@code string}.</li>
      *
-     *   <li><p>If {@code candidate} is an instance of {@link CharSequence},
then this method
+     *   <li>If {@code candidate} is an instance of {@link CharSequence}, then this
method
      *       returns {@code true} if {@link #toString(Locale)} returns a string {@linkplain
      *       String#equals equals} to <code>candidate.{@linkplain CharSequence#toString()
-     *       toString()}</code> for all locales.</p></li>
+     *       toString()}</code> for all locales.</li>
      *
-     *   <li><p>If {@code candidate} is an instance of {@link Map}, then this
methods returns
+     *   <li>If {@code candidate} is an instance of {@link Map}, then this methods
returns
      *       {@code true} if all <var>{@linkplain Locale locale}</var>-<var>{@linkplain
String
-     *       string}</var> pairs are contained into {@code candidate}.</p></li>
+     *       string}</var> pairs are contained into {@code candidate}.</li>
      *
-     *   <li><p>Otherwise, this method returns {@code false}.</p></li>
+     *   <li>Otherwise, this method returns {@code false}.</li>
      * </ul>
      *
      * @param  candidate The object which may contains this international string.

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java
(original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/ResourceInternationalString.java
Fri Sep 28 13:53:18 2012
@@ -39,20 +39,20 @@ import org.apache.sis.internal.Objects;
  * properties file with the right suffix, for example {@code "_en"} for English or {@code
"_fr"}
  * for French. This mechanism is explained in J2SE javadoc for the
  * {@link ResourceBundle#getBundle(String,Locale,ClassLoader) getBundle} static method.
- * <p>
- * <b>Example:</b> If a file named "{@code MyResources.properties}" exists in
the package
- * {@code org.apache.sis.mypackage} and contains a line like "{@code MyKey = some value}",
- * then an international string for "{@code some value}" can be created using the following
- * code:
+ *
+ * {@section Example}
+ * If a file named "{@code MyResources.properties}" exists in the package {@code org.mypackage}
+ * and contains a line like "{@code MyKey = some value}", then an international string for
+ * {@code "some value"} can be created using the following code:
  *
  * {@preformat java
- *     InternationalString value =
- *         new ResourceInternationalString("org.apache.sis.mypackage.MyResources", "MyKey");
+ *     InternationalString value = new ResourceInternationalString("org.mypackage.MyResources",
"MyKey");
  * }
  *
- * The "{@code some value}" string will be localized if the required properties files exist,
for
+ * The {@code "some value"} string will be localized if the required properties files exist,
for
  * example "{@code MyResources_fr.properties}" for French, "{@code MyResources_it.properties}"
- * for Italian, <i>etc.</i>
+ * for Italian, <i>etc</i>.
+ * If needed, users can gain more control by overriding the {@link #getBundle(Locale)} method.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @author  Johann Sorel (Geomatys)
@@ -107,7 +107,22 @@ public class ResourceInternationalString
         this.loader    = loader;
         ensureNonNull("resources", resources);
         ensureNonNull("key",       key);
+    }
 
+    /**
+     * Returns the resource bundle for the given locale. The default implementation fetches
the
+     * bundle from the name given at {@linkplain #ResourceInternationalString(String,String)
+     * construction time}. Subclasses can override this method if they need to fetch the
+     * bundle in an other way.
+     *
+     * @param  locale The locale for which to get the resource bundle.
+     * @return The resource bundle for the given locale.
+     *
+     * @see ResourceBundle#getBundle(String,Locale)
+     */
+    protected ResourceBundle getBundle(final Locale locale) {
+        return (loader == null) ? ResourceBundle.getBundle(resources, locale) :
+                ResourceBundle.getBundle(resources, locale, loader);
     }
 
     /**
@@ -132,22 +147,6 @@ public class ResourceInternationalString
     }
 
     /**
-     * Returns the resource bundle for the given locale. The default implementation fetches
the
-     * bundle from the name given at {@linkplain #ResourceInternationalString(String,String)
-     * construction time}. Subclasses can override this method if they need to fetch the
-     * bundle in an other way.
-     *
-     * @param  locale The locale for which to get the resource bundle.
-     * @return The resource bundle for the given locale.
-     *
-     * @see ResourceBundle#getBundle(String,Locale)
-     */
-    protected ResourceBundle getBundle(final Locale locale) {
-        return (loader == null) ? ResourceBundle.getBundle(resources, locale) :
-                ResourceBundle.getBundle(resources, locale, loader);
-    }
-
-    /**
      * Compares this international string with the specified object for equality.
      *
      * @param object The object to compare with this international string.

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java (original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/type/package-info.java Fri Sep
28 13:53:18 2012
@@ -24,8 +24,8 @@
  *   <li>Implementations of {@link org.opengis.util.InternationalString}:
  *     <ul>
  *       <li>{@link org.apache.sis.util.type.SimpleInternationalString} for wrapping
a single {@link java.lang.String};</li>
- *       <li>{@link org.apache.sis.util.type.DefaultInternationalString} for providing
many localization in a {@link java.util.Map};</li>
- *       <li>{@link org.apache.sis.util.type.ResourceInternationalString} for providing
localization from a {@link java.util.ResourceBundle}.</li>
+ *       <li>{@link org.apache.sis.util.type.DefaultInternationalString} for providing
many localizations in a {@link java.util.Map};</li>
+ *       <li>{@link org.apache.sis.util.type.ResourceInternationalString} for providing
localizations from a {@link java.util.ResourceBundle}.</li>
  *     </ul>
  *   </li>
  *   <li>Implementations of {@link org.opengis.util.GenericName}:

Added: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java?rev=1391467&view=auto
==============================================================================
--- sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java (added)
+++ sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java Fri Sep 28 13:53:18
2012
@@ -0,0 +1,86 @@
+/*
+ * 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.util;
+
+import java.util.Locale;
+
+import org.junit.*;
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link Locales} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.04)
+ * @version 0.3
+ * @module
+ */
+public final strictfp class LocalesTest {
+    /**
+     * Tests the {@link Locales#getAvailableLanguages()} method.
+     */
+    @Test
+    public void testGetAvailableLanguages() {
+        final Locale[] locales = Locales.ALL.getAvailableLanguages();
+        assertTrue ("Expected English locale.",         Arrays.contains(locales, Locale.ENGLISH));
+        assertFalse("US is a country, not a language.", Arrays.contains(locales, Locale.US));
+    }
+
+    /**
+     * Tests the {@link Locales#getAvailableLocales()} method.
+     */
+    @Test
+    public void testGetAvailableLocales() {
+        final Locale[] locales = Locales.SIS.getAvailableLocales();
+        assertTrue(Arrays.contains(locales, Locale.ENGLISH));
+        assertTrue(Arrays.contains(locales, Locale.US));
+        assertTrue(Arrays.contains(locales, Locale.CANADA));
+        assertTrue(Arrays.contains(locales, Locale.FRANCE));
+        assertTrue(Arrays.contains(locales, Locale.CANADA_FRENCH));
+    }
+
+    /**
+     * Tests the {@link Locales#unique(Locale)} method.
+     */
+    @Test
+    public void testUnique() {
+        assertSame(Locale.ENGLISH, Locales.unique(new Locale("en")));
+        assertSame(Locale.FRENCH,  Locales.unique(new Locale("fr")));
+    }
+
+    /**
+     * Tests the {@link Locales#parse(String)} method.
+     * Depends on {@link #testUnique()}.
+     */
+    @Test
+    public void testParse() {
+        assertSame(Locale.FRENCH,        Locales.parse("fr"));
+        assertSame(Locale.FRENCH,        Locales.parse("fra"));
+        assertSame(Locale.CANADA_FRENCH, Locales.parse("fr_CA"));
+        assertSame(Locale.CANADA_FRENCH, Locales.parse("fra_CA"));
+        assertSame(Locale.CANADA_FRENCH, Locales.parse("fr_CAN"));
+        assertSame(Locale.CANADA_FRENCH, Locales.parse("fra_CAN"));
+        assertSame(Locale.ENGLISH,       Locales.parse("en"));
+
+        assertEquals(new Locale("de", "DE"),        Locales.parse("de_DE"));
+        assertEquals(new Locale("",   "GB"),        Locales.parse("_GB"));
+        assertEquals(new Locale("en", "US", "WIN"), Locales.parse("en_US_WIN"));
+        assertEquals(new Locale("de", "", "POSIX"), Locales.parse("de__POSIX"));
+        assertEquals(new Locale("fr", "", "MAC"),   Locales.parse("fr__MAC"));
+    }
+}

Propchange: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/LocalesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/resources/IndexedResourceBundleTest.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/test/java/org/apache/sis/util/resources/IndexedResourceBundleTest.java?rev=1391467&r1=1391466&r2=1391467&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/test/java/org/apache/sis/util/resources/IndexedResourceBundleTest.java
(original)
+++ sis/trunk/sis-utility/src/test/java/org/apache/sis/util/resources/IndexedResourceBundleTest.java
Fri Sep 28 13:53:18 2012
@@ -17,6 +17,7 @@
 package org.apache.sis.util.resources;
 
 import java.util.Locale;
+import org.opengis.util.InternationalString;
 
 import org.junit.*;
 import static org.junit.Assert.*;
@@ -60,4 +61,18 @@ public final strictfp class IndexedResou
         assertEquals("L’argument ‘CRS’ ne doit pas être nul.",
                 Errors.getResources(Locale.FRENCH).getString(Errors.Keys.NullArgument_1,
"CRS"));
     }
+
+    /**
+     * Tests the formatting of an international string.
+     */
+    @Test
+    public void testInternationalString() {
+        InternationalString i18n = Errors.formatInternational(Errors.Keys.NullArgument_1);
+        assertEquals("Argument ‘{0}’ shall not be null.",      i18n.toString(Locale.ENGLISH));
+        assertEquals("L’argument ‘{0}’ ne doit pas être nul.", i18n.toString(Locale.FRENCH));
+
+        i18n = Errors.formatInternational(Errors.Keys.NullArgument_1, "CRS");
+        assertEquals("Argument ‘CRS’ shall not be null.",      i18n.toString(Locale.ENGLISH));
+        assertEquals("L’argument ‘CRS’ ne doit pas être nul.", i18n.toString(Locale.FRENCH));
+    }
 }

Added: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java?rev=1391467&view=auto
==============================================================================
--- sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java (added)
+++ sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java Fri Sep 28
13:53:18 2012
@@ -0,0 +1,58 @@
+/*
+ * 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.util.type;
+
+import org.opengis.metadata.citation.Citation;
+import org.opengis.referencing.datum.Datum;
+import org.opengis.referencing.cs.AxisDirection;
+import org.junit.*;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link GeoAPI} class.
+ *
+ * @author  Martin Desruisseaux (IRD, Geomatys)
+ * @since   0.3 (derived from geotk-2.1)
+ * @version 0.3
+ * @module
+ */
+public final strictfp class TypesTest {
+    /**
+     * Tests the {@link Types#getStandardName(String)} method.
+     */
+    @Test
+    public void testGetStandardName() {
+        assertEquals("CI_Citation",      Types.getStandardName(Citation     .class));
+        assertEquals("CD_Datum",         Types.getStandardName(Datum        .class));
+        assertEquals("CS_AxisDirection", Types.getStandardName(AxisDirection.class));
+    }
+
+    /**
+     * Tests the {@link Types#forStandardName(String)} method.
+     */
+    @Test
+    @Ignore("class-index.properties not available in GeoAPI 3.0.0")
+    public void testForStandardName() {
+        assertEquals(Citation     .class, Types.forStandardName("CI_Citation"));
+        assertEquals(Datum        .class, Types.forStandardName("CD_Datum"));
+        assertEquals(Citation     .class, Types.forStandardName("CI_Citation")); // Value
should be cached.
+        assertEquals(AxisDirection.class, Types.forStandardName("CS_AxisDirection"));
+        assertNull  (                     Types.forStandardName("MD_Dummy"));
+    }
+}

Propchange: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/type/TypesTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message