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

Print this page




10109      * @since 1.5
10110      */
10111     @HotSpotIntrinsicCandidate
10112     public static char reverseBytes(char ch) {
10113         return (char) (((ch & 0xFF00) >> 8) | (ch << 8));
10114     }
10115 
10116     /**
10117      * Returns the Unicode name of the specified character
10118      * {@code codePoint}, or null if the code point is
10119      * {@link #UNASSIGNED unassigned}.
10120      * <p>
10121      * Note: if the specified character is not assigned a name by
10122      * the <i>UnicodeData</i> file (part of the Unicode Character
10123      * Database maintained by the Unicode Consortium), the returned
10124      * name is the same as the result of expression.
10125      *
10126      * <blockquote>{@code
10127      *     Character.UnicodeBlock.of(codePoint).toString().replace('_', ' ')
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 }


10109      * @since 1.5
10110      */
10111     @HotSpotIntrinsicCandidate
10112     public static char reverseBytes(char ch) {
10113         return (char) (((ch & 0xFF00) >> 8) | (ch << 8));
10114     }
10115 
10116     /**
10117      * Returns the Unicode name of the specified character
10118      * {@code codePoint}, or null if the code point is
10119      * {@link #UNASSIGNED unassigned}.
10120      * <p>
10121      * Note: if the specified character is not assigned a name by
10122      * the <i>UnicodeData</i> file (part of the Unicode Character
10123      * Database maintained by the Unicode Consortium), the returned
10124      * name is the same as the result of expression.
10125      *
10126      * <blockquote>{@code
10127      *     Character.UnicodeBlock.of(codePoint).toString().replace('_', ' ')
10128      *     + " "
10129      *     + Integer.toHexString(codePoint).toUpperCase(Locale.ROOT);
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.ROOT);
10157         // should never come here
10158         return Integer.toHexString(codePoint).toUpperCase(Locale.ROOT);
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.ROOT);
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      * @throws IllegalArgumentException if the specified {@code name}
10184      *         is not a valid Unicode character name.
10185      * @throws NullPointerException if {@code name} is {@code null}
10186      *
10187      * @since 9
10188      */
10189     public static int codePointOf(String name) {
10190         name = name.trim().toUpperCase(Locale.ROOT);
10191         int cp = CharacterName.getInstance().getCodePoint(name);
10192         if (cp != -1)
10193             return cp;
10194         try {
10195             int off = name.lastIndexOf(' ');
10196             if (off != -1) {
10197                 cp = Integer.parseInt(name, off + 1, name.length(), 16);
10198                 if (isValidCodePoint(cp) && name.equals(getName(cp)))
10199                     return cp;
10200             }
10201         } catch (Exception x) {}
10202         throw new IllegalArgumentException("Unrecognized character name :" + name);
10203     }
10204 }