sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1392258 - in /sis/trunk: ide-project/NetBeans/nbproject/ sis-utility/src/main/java/org/apache/sis/util/ sis-utility/src/test/java/org/apache/sis/test/ sis-utility/src/test/java/org/apache/sis/util/
Date Mon, 01 Oct 2012 09:57:08 GMT
Author: desruisseaux
Date: Mon Oct  1 09:57:08 2012
New Revision: 1392258

URL: http://svn.apache.org/viewvc?rev=1392258&view=rev
Log:
Added Unicode-related methods.

Added:
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Characters.java   (with props)
    sis/trunk/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java   (with props)
Modified:
    sis/trunk/ide-project/NetBeans/nbproject/genfiles.properties
    sis/trunk/ide-project/NetBeans/nbproject/project.xml
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Static.java
    sis/trunk/sis-utility/src/main/java/org/apache/sis/util/package-info.java
    sis/trunk/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java

Modified: sis/trunk/ide-project/NetBeans/nbproject/genfiles.properties
URL: http://svn.apache.org/viewvc/sis/trunk/ide-project/NetBeans/nbproject/genfiles.properties?rev=1392258&r1=1392257&r2=1392258&view=diff
==============================================================================
--- sis/trunk/ide-project/NetBeans/nbproject/genfiles.properties (original)
+++ sis/trunk/ide-project/NetBeans/nbproject/genfiles.properties Mon Oct  1 09:57:08 2012
@@ -3,6 +3,6 @@
 build.xml.data.CRC32=58e6b21c
 build.xml.script.CRC32=462eaba0
 build.xml.stylesheet.CRC32=28e38971@1.53.1.46
-nbproject/build-impl.xml.data.CRC32=d6149061
+nbproject/build-impl.xml.data.CRC32=075b665d
 nbproject/build-impl.xml.script.CRC32=a54bea32
 nbproject/build-impl.xml.stylesheet.CRC32=6ddba6b6@1.53.1.46

Modified: sis/trunk/ide-project/NetBeans/nbproject/project.xml
URL: http://svn.apache.org/viewvc/sis/trunk/ide-project/NetBeans/nbproject/project.xml?rev=1392258&r1=1392257&r2=1392258&view=diff
==============================================================================
--- sis/trunk/ide-project/NetBeans/nbproject/project.xml (original)
+++ sis/trunk/ide-project/NetBeans/nbproject/project.xml Mon Oct  1 09:57:08 2012
@@ -19,6 +19,7 @@
         </data>
         <spellchecker-wordlist xmlns="http://www.netbeans.org/ns/spellchecker-wordlist/1">
             <word>bitmask</word>
+            <word>Unicode</word>
         </spellchecker-wordlist>
     </configuration>
 </project>

Added: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Characters.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Characters.java?rev=1392258&view=auto
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Characters.java (added)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Characters.java Mon Oct  1 09:57:08
2012
@@ -0,0 +1,174 @@
+/*
+ * 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;
+
+
+/**
+ * Static methods working on {@code char} values.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-2.1)
+ * @version 0.3
+ * @module
+ */
+public final class Characters extends Static {
+    /**
+     * The <cite>no-break space</cite> (Unicode {@code \\u00A0}, HTML {@code
&nbsp;}).
+     * Apache SIS uses Unicode symbols directly in the source code for easier reading,
+     * except for no-break spaces since they can not be visually distinguished from the
+     * ordinary space (Unicode {@code \\u0020}).
+     */
+    public static final char NO_BREAK_SPACE = '\u00A0';
+
+    /**
+     * Do not allow instantiation of this class.
+     */
+    private Characters() {
+    }
+
+    /**
+     * 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:
+     *
+     * {@preformat text
+     *   ⁰ ¹ ² ³ ⁴ ⁵ ⁶ ⁷ ⁸ ⁹ ⁺ ⁻ ⁼
⁽ ⁾ ⁿ
+     * }
+     *
+     * @param  c The character to test.
+     * @return {@code true} if the given character is a superscript.
+     */
+    public static boolean isSuperScript(final char c) {
+        switch (c) {
+            case '¹':      // Legacy values in "Latin-1 supplement" space: 00B9, 00B2
and 00B3.
+            case '²':      // Those values are outside the normal [2070 … 207F] range.
+            case '³':      return true;
+            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<='ⁿ');
+        }
+    }
+
+    /**
+     * Determines whether the given character is a subscript. All subscripts have
+     * a Unicode value in the [2080 … 208E]. Subscripts are the following symbols:
+     *
+     * {@preformat text
+     *   ₀ ₁ ₂ ₃ ₄ ₅ ₆ ₇ ₈ ₉ ₊ ₋
₌ ₍ ₎
+     * }
+     *
+     * @param  c The character to test.
+     * @return {@code true} if the given character is a subscript.
+     */
+    public static boolean isSubScript(final char c) {
+        return (c>='₀' && c<='₎');
+    }
+
+    /**
+     * Converts the given character argument to superscript.
+     * Only the following characters can be converted (other characters are left unchanged):
+     *
+     * {@preformat text
+     *     0 1 2 3 4 5 6 7 8 9 + - = ( ) n
+     * }
+     *
+     * @param  c The character to convert.
+     * @return The given character as a superscript, or {@code c}
+     *         if the given character can not be converted.
+     */
+    public static char toSuperScript(char c) {
+        switch (c) {
+            case '1': c = '¹'; break;  // 00B9
+            case '2': c = '²'; break;  // 00B2
+            case '3': c = '³'; break;  // 00B3
+            case '+': c = '⁺'; break;  // 207A
+            case '-': c = '⁻'; break;  // 207B
+            case '=': c = '⁼'; break;  // 207C
+            case '(': c = '⁽'; break;  // 207D
+            case ')': c = '⁾'; break;  // 207E
+            case 'n': c = 'ⁿ'; break;  // 207F
+            default: {
+                if (c >= '0' && c <= '9') {
+                    c += ('⁰' - '0');
+                }
+                break;
+            }
+        }
+        return c;
+    }
+
+    /**
+     * Converts the given character argument to subscript.
+     * Only the following characters can be converted (other characters are left unchanged):
+     *
+     * {@preformat text
+     *     0 1 2 3 4 5 6 7 8 9 + - = ( )
+     * }
+     *
+     * @param  c The character to convert.
+     * @return The given character as a subscript, or {@code c}
+     *         if the given character can not be converted.
+     */
+    public static char toSubScript(char c) {
+        switch (c) {
+            case '+': c = '₊'; break;  // 208A
+            case '-': c = '₋'; break;  // 208B
+            case '=': c = '₌'; break;  // 208C
+            case '(': c = '₍'; break;  // 208D
+            case ')': c = '₎'; break;  // 208E
+            default: {
+                if (c >= '0' && c <= '9') {
+                    c += ('₀' - '0');
+                }
+                break;
+            }
+        }
+        return c;
+    }
+
+    /**
+     * Converts the given character argument to normal script.
+     *
+     * @param  c The character to convert.
+     * @return The given character as a normal script, or {@code c} if the
+     *         given character was not a superscript or a subscript.
+     */
+    public static char toNormalScript(char c) {
+        switch (c) {
+            case '\u2071': // Exceptions to the default case. They would be the ¹²³
+            case '\u2072': // cases if they were not defined in the Latin-1 range.
+            case '\u2073':               break;
+            case '¹':           c = '1'; break;
+            case '²':           c = '2'; break;
+            case '³':           c = '3'; break;
+            case '⁺': case '₊': c = '+'; break;
+            case '⁻': case '₋': c = '-'; break;
+            case '⁼': case '₌': c = '='; break;
+            case '⁽': case '₍': c = '('; break;
+            case '⁾': case '₎': c = ')'; break;
+            case 'ⁿ':           c = 'n'; break;
+            default: {
+                if (c >= '⁰' && c <= '₉') {
+                    if      (c <= '⁹') c -= ('⁰' - '0');
+                    else if (c >= '₀') c -= ('₀' - '0');
+                }
+                break;
+            }
+        }
+        return c;
+    }
+}

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

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

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Static.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Static.java?rev=1392258&r1=1392257&r2=1392258&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Static.java (original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/Static.java Mon Oct  1 09:57:08
2012
@@ -23,6 +23,8 @@ package org.apache.sis.util;
  *
  * <table class="sis">
  * <tr><th colspan="2">Basic classes of the Java language</th></tr>
+ * <tr><td>{@link Characters}</td>
+ *     <td>Find subscript and superscript digit characters.</td></tr>
  * <tr><td>{@link CharSequences}</td>
  *     <td>Methods working on {@link CharSequence} or {@link String} instances.</td></tr>
  * <tr><td>{@link StringBuilders}</td>

Modified: sis/trunk/sis-utility/src/main/java/org/apache/sis/util/package-info.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/main/java/org/apache/sis/util/package-info.java?rev=1392258&r1=1392257&r2=1392258&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/main/java/org/apache/sis/util/package-info.java (original)
+++ sis/trunk/sis-utility/src/main/java/org/apache/sis/util/package-info.java Mon Oct  1 09:57:08
2012
@@ -22,6 +22,7 @@
  *   <li><p>General purpose static methods working on:</p>
  *   <ul>
  *     <li>Primitive or basic Java types:
+ *         {@link org.apache.sis.util.Characters},
  *         {@link org.apache.sis.util.CharSequences},
  *         {@link org.apache.sis.util.StringBuilders},
  *         {@link org.apache.sis.util.Locales},

Modified: sis/trunk/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java?rev=1392258&r1=1392257&r2=1392258&view=diff
==============================================================================
--- sis/trunk/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java (original)
+++ sis/trunk/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java Mon Oct  1
09:57:08 2012
@@ -46,6 +46,7 @@ import org.apache.sis.util.ArgumentCheck
 
 import static java.lang.StrictMath.*;
 import static org.opengis.test.Assert.*;
+import static org.apache.sis.util.Characters.NO_BREAK_SPACE;
 
 
 /**
@@ -596,14 +597,14 @@ public strictfp class XMLComparator {
             node = node.getParentNode();
         }
         if (hierarchy.equals(expected)) {
-            buffer.append("\u2514\u2500Same as expected").append(lineSeparator);
+            buffer.append("└─Same as expected").append(lineSeparator);
         } else {
             int indent = 2;
             for (int i=hierarchy.size(); --i>=0;) {
                 for (int j=indent; --j>=0;) {
-                    buffer.append('\u00A0');
+                    buffer.append(NO_BREAK_SPACE);
                 }
-                buffer.append("\u2514\u2500").append(hierarchy.get(i)).append(lineSeparator);
+                buffer.append("└─").append(hierarchy.get(i)).append(lineSeparator);
                 indent += 4;
             }
         }

Added: sis/trunk/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java
URL: http://svn.apache.org/viewvc/sis/trunk/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java?rev=1392258&view=auto
==============================================================================
--- sis/trunk/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java (added)
+++ sis/trunk/sis-utility/src/test/java/org/apache/sis/util/CharactersTest.java Mon Oct  1
09:57:08 2012
@@ -0,0 +1,70 @@
+/*
+ * 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 org.junit.Test;
+import org.apache.sis.test.TestCase;
+
+import static org.junit.Assert.*;
+import static org.apache.sis.util.Characters.*;
+
+
+/**
+ * Tests the {@link Characters} utility methods.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.00)
+ * @version 0.3
+ * @module
+ */
+public final strictfp class CharactersTest extends TestCase {
+    /**
+     * Tests {@link Characters#toSuperScript(char)}.
+     */
+    @Test
+    public void testSuperScript() {
+        for (char c='0'; c<='9'; c++) {
+            final char s = toSuperScript(c);
+            assertFalse(s == c);
+            assertFalse(isSuperScript(c));
+            assertTrue (isSuperScript(s));
+            assertEquals(c, toNormalScript(s));
+        }
+        final char c = 'A';
+        assertEquals(c, toSuperScript(c));
+        assertEquals(c, toNormalScript(c));
+        assertFalse(isSuperScript(c));
+    }
+
+    /**
+     * Tests {@link Characters#toSubScript(char)}.
+     */
+    @Test
+    public void testSubScript() {
+        for (char c='0'; c<='9'; c++) {
+            final char s = toSubScript(c);
+            assertFalse(s == c);
+            assertFalse(isSubScript(c));
+            assertTrue (isSubScript(s));
+            assertEquals(c, toNormalScript(s));
+        }
+        final char c = 'a';
+        assertEquals(c, toSubScript(c));
+        assertEquals(c, toNormalScript(c));
+        assertFalse(isSubScript(c));
+    }
+}

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

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



Mime
View raw message