sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1617481 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/metadata/ sis-metadata/src/test/java/org/apache/sis/metadata/ sis-utility/src/main/java/org/apache/sis/util/ sis-utility/src/test/java/org/apache/sis/util/
Date Tue, 12 Aug 2014 14:14:08 GMT
Author: desruisseaux
Date: Tue Aug 12 14:14:08 2014
New Revision: 1617481

URL: http://svn.apache.org/r1617481
Log:
More accurate return type in MetadataStandard.getInterface/getImplementation(...) methods,
and added a trivial Characters.isHexadecimal(int) method to be needed later for a JSON parser.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
    sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTestCase.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Characters.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java?rev=1617481&r1=1617480&r2=1617481&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
[UTF-8] Tue Aug 12 14:14:08 2014
@@ -88,7 +88,7 @@ import static org.apache.sis.util.Argume
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-2.4)
- * @version 0.3
+ * @version 0.5
  * @module
  *
  * @see AbstractMetadata
@@ -412,6 +412,7 @@ public class MetadataStandard implements
      * The standard package is usually made of interfaces and code lists only, but this is
      * not verified by this method.</div>
      *
+     * @param  <T>  The compile-time {@code type}.
      * @param  type The implementation class.
      * @return The interface implemented by the given implementation class.
      * @throws ClassCastException if the specified implementation class does
@@ -419,23 +420,28 @@ public class MetadataStandard implements
      *
      * @see AbstractMetadata#getInterface()
      */
-    public Class<?> getInterface(final Class<?> type) throws ClassCastException
{
+    @SuppressWarnings("unchecked")
+    public <T> Class<? super T> getInterface(final Class<T> type) throws
ClassCastException {
         ensureNonNull("type", type);
+        final Class<?> standard;
         synchronized (accessors) {
             final Object value = accessors.get(type);
             if (value != null) {
                 if (value instanceof PropertyAccessor) {
-                    return ((PropertyAccessor) value).type;
+                    standard = ((PropertyAccessor) value).type;
+                } else {
+                    standard = (Class<?>) value;
                 }
-                return (Class<?>) value;
-            }
-            final Class<?> standard = findInterface(type);
-            if (standard == null) {
-                throw new ClassCastException(Errors.format(Errors.Keys.UnknownType_1, type));
+            } else {
+                standard = findInterface(type);
+                if (standard == null) {
+                    throw new ClassCastException(Errors.format(Errors.Keys.UnknownType_1,
type));
+                }
+                accessors.put(type, standard);
             }
-            accessors.put(type, standard);
-            return standard;
         }
+        assert standard.isAssignableFrom(type) : type;
+        return (Class<? super T>) standard;
     }
 
     /**
@@ -443,10 +449,11 @@ public class MetadataStandard implements
      * The default implementation returns {@code null} if every cases. Subclasses shall
      * override this method in order to map GeoAPI interfaces to their implementation.
      *
+     * @param  <T>  The compile-time {@code type}.
      * @param  type The interface, typically from the {@code org.opengis.metadata} package.
      * @return The implementation class, or {@code null} if none.
      */
-    public Class<?> getImplementation(final Class<?> type) {
+    public <T> Class<? extends T> getImplementation(final Class<T> type)
{
         return null;
     }
 

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java?rev=1617481&r1=1617480&r2=1617481&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
[UTF-8] Tue Aug 12 14:14:08 2014
@@ -98,11 +98,12 @@ final class StandardImplementation exten
      * Returns the implementation class for the given interface, or {@code null} if none.
      * This class uses heuristic rules based on naming conventions.
      *
+     * @param  <T>  The compile-time {@code type}.
      * @param  type The interface, typically from the {@code org.opengis.metadata} package.
      * @return The implementation class, or {@code null} if none.
      */
     @Override
-    public Class<?> getImplementation(final Class<?> type) {
+    public <T> Class<? extends T> getImplementation(final Class<T> type)
{
         /*
          * We require the type to be an interface in order to exclude
          * CodeLists, Enums and Exceptions.
@@ -113,7 +114,7 @@ final class StandardImplementation exten
                 synchronized (implementations) {
                     Class<?> candidate = implementations.get(type);
                     if (candidate != null) {
-                        return (candidate != Void.TYPE) ? candidate : null;
+                        return (candidate != Void.TYPE) ? candidate.asSubclass(type) : null;
                     }
                     /*
                      * Prepares a buffer with a copy of the class name in which the interface
@@ -141,7 +142,7 @@ final class StandardImplementation exten
                     try {
                         candidate = Class.forName(name);
                         implementations.put(type, candidate);
-                        return candidate;
+                        return candidate.asSubclass(type);
                     } catch (ClassNotFoundException e) {
                         Logging.recoverableException(MetadataStandard.class, "getImplementation",
e);
                     }

Modified: sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTestCase.java?rev=1617481&r1=1617480&r2=1617481&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTestCase.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTestCase.java
[UTF-8] Tue Aug 12 14:14:08 2014
@@ -79,9 +79,9 @@ public abstract strictfp class MetadataT
     @Override
     protected <T> Class<? extends T> getImplementation(final Class<T> type)
{
         assertTrue(standard.isMetadata(type));
-        final Class<?> impl = standard.getImplementation(type);
+        final Class<? extends T> impl = standard.getImplementation(type);
         assertNotNull(type.getName(), impl);
-        return impl.asSubclass(type);
+        return impl;
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Characters.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Characters.java?rev=1617481&r1=1617480&r2=1617481&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Characters.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/Characters.java [UTF-8]
Tue Aug 12 14:14:08 2014
@@ -27,7 +27,7 @@ import org.apache.sis.util.resources.Err
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-2.1)
- * @version 0.3
+ * @version 0.5
  * @module
  */
 public final class Characters extends Static {
@@ -97,6 +97,25 @@ public final class Characters extends St
     }
 
     /**
+     * Returns {@code true} if the given character is an hexadecimal digit.
+     * This method returns {@code true} if {@code c} is between {@code '0'} and {@code '9'}
inclusive,
+     * or between {@code 'A'} and {@code 'F'} inclusive, or between {@code 'a'} and {@code
'f'} inclusive.
+     *
+     * @param  c The character to test.
+     * @return {@code true} if the given character is an hexadecimal digit.
+     *
+     * @since 0.5
+     */
+    public static boolean isHexadecimal(int c) {
+        /*
+         * The &= ~32 is a cheap conversion of lower-case letters to upper-case letters.
+         * It is not a rigorous conversion since it does not check if 'c' is a letter,
+         * but for the purpose of this method it is okay.
+         */
+        return (c >= '0' && c <= '9') || ((c &= ~32) >= 'A' &&
c <= 'F');
+    }
+
+    /**
      * Determines whether the given character is a superscript. Most (but not all) superscripts
      * have a Unicode value in the [2070 … 207F] range. Superscripts are the following
symbols:
      *
@@ -115,7 +134,7 @@ public final class Characters extends St
             case '\u2071': // Would be the '¹', '²' and '³' values if they were declared
in the
             case '\u2072': // normal range. Since they are not, those values are unassigned.
             case '\u2073': return false;
-            default:       return (c>='⁰' && c<='ⁿ');
+            default:       return (c >= '⁰' && c <= 'ⁿ');
         }
     }
 
@@ -131,7 +150,7 @@ public final class Characters extends St
      * @return {@code true} if the given character is a subscript.
      */
     public static boolean isSubScript(final int c) {
-        return (c>='₀' && c<='₎');
+        return (c >= '₀' && c <= '₎');
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java?rev=1617481&r1=1617480&r2=1617481&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java
[UTF-8] Tue Aug 12 14:14:08 2014
@@ -29,7 +29,7 @@ import static org.apache.sis.util.Charac
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.00)
- * @version 0.3
+ * @version 0.5
  * @module
  */
 public final strictfp class CharactersTest extends TestCase {
@@ -58,6 +58,36 @@ public final strictfp class CharactersTe
     }
 
     /**
+     * Tests the {@link Characters#isHexadecimal()} method.
+     *
+     * @since 0.5
+     */
+    @Test
+    public void testIsHexadecimal() {
+        assertTrue(isHexadecimal('0'));
+        assertTrue(isHexadecimal('5'));
+        assertTrue(isHexadecimal('9'));
+        assertTrue(isHexadecimal('A'));
+        assertTrue(isHexadecimal('C'));
+        assertTrue(isHexadecimal('F'));
+        assertTrue(isHexadecimal('a'));
+        assertTrue(isHexadecimal('c'));
+        assertTrue(isHexadecimal('f'));
+
+        assertFalse(isHexadecimal(' '));
+        assertFalse(isHexadecimal('_'));
+        assertFalse(isHexadecimal(':'));
+        assertFalse(isHexadecimal('/'));
+        assertFalse(isHexadecimal('>'));
+        assertFalse(isHexadecimal('@'));
+        assertFalse(isHexadecimal('`'));
+        assertFalse(isHexadecimal('G'));
+        assertFalse(isHexadecimal('Q'));
+        assertFalse(isHexadecimal('g'));
+        assertFalse(isHexadecimal('q'));
+    }
+
+    /**
      * Tests {@link Characters#toSuperScript(char)}.
      */
     @Test



Mime
View raw message