src/java.base/share/classes/java/lang/Character.java

Print this page




10128      *     + " "
10129      *     + Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10130      *
10131      * }</blockquote>
10132      *
10133      * @param  codePoint the character (Unicode code point)
10134      *
10135      * @return the Unicode name of the specified character, or null if
10136      *         the code point is unassigned.
10137      *
10138      * @exception IllegalArgumentException if the specified
10139      *            {@code codePoint} is not a valid Unicode
10140      *            code point.
10141      *
10142      * @since 1.7
10143      */
10144     public static String getName(int codePoint) {
10145         if (!isValidCodePoint(codePoint)) {
10146             throw new IllegalArgumentException();
10147         }
10148         String name = CharacterName.get(codePoint);
10149         if (name != null)
10150             return name;
10151         if (getType(codePoint) == UNASSIGNED)
10152             return null;
10153         UnicodeBlock block = UnicodeBlock.of(codePoint);
10154         if (block != null)
10155             return block.toString().replace('_', ' ') + " "
10156                    + Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10157         // should never come here
10158         return Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10159     }











































10160 }


10128      *     + " "
10129      *     + Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10130      *
10131      * }</blockquote>
10132      *
10133      * @param  codePoint the character (Unicode code point)
10134      *
10135      * @return the Unicode name of the specified character, or null if
10136      *         the code point is unassigned.
10137      *
10138      * @exception IllegalArgumentException if the specified
10139      *            {@code codePoint} is not a valid Unicode
10140      *            code point.
10141      *
10142      * @since 1.7
10143      */
10144     public static String getName(int codePoint) {
10145         if (!isValidCodePoint(codePoint)) {
10146             throw new IllegalArgumentException();
10147         }
10148         String name = CharacterName.getInstance().getName(codePoint);
10149         if (name != null)
10150             return name;
10151         if (getType(codePoint) == UNASSIGNED)
10152             return null;
10153         UnicodeBlock block = UnicodeBlock.of(codePoint);
10154         if (block != null)
10155             return block.toString().replace('_', ' ') + " "
10156                    + Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10157         // should never come here
10158         return Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10159     }
10160 
10161     /**
10162      * Returns the code point value of the Unicode character specified by
10163      * the given Unicode character name.
10164      * <p>
10165      * Note: if a character is not assigned a name by the <i>UnicodeData</i>
10166      * file (part of the Unicode Character Database maintained by the Unicode
10167      * Consortium), its name is defined as the result of expression
10168      *
10169      * <blockquote>{@code
10170      *     Character.UnicodeBlock.of(codePoint).toString().replace('_', ' ')
10171      *     + " "
10172      *     + Integer.toHexString(codePoint).toUpperCase(Locale.ENGLISH);
10173      *
10174      * }</blockquote>
10175      * <p>
10176      * The {@code name} matching is case insensitive, with any leading and
10177      * trailing whitespace character removed.
10178      *
10179      * @param  name the Unicode character name
10180      *
10181      * @return the code point value of the character specified by its name.
10182      *
10183      * @exception IllegalArgumentException if the specified {@code name}
10184      *            is not a valid Unicode character name.
10185      *
10186      * @since 9
10187      */
10188     public static int codePointOf(String name) {
10189         name = name.trim().toUpperCase(Locale.ENGLISH);
10190         int cp = CharacterName.getInstance().getCodePoint(name);
10191         if (cp != -1)
10192             return cp;
10193         try {
10194             int off = name.lastIndexOf(' ');
10195             if (off != -1) {
10196                 cp = Integer.parseInt(name, off + 1, name.length(), 16);
10197                 if (isValidCodePoint(cp) && name.equals(getName(cp)))
10198                     return cp;
10199             }
10200         } catch (Exception x) {}
10201         throw new IllegalArgumentException("Unrecognized character name :" + name);
10202     }
10203 }