JavaUnicodeEscaper.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.text.translate;

  18. /**
  19.  * Translates code points to their Unicode escaped value suitable for Java source.
  20.  *
  21.  * @since 3.2
  22.  * @deprecated As of 3.6, use Apache Commons Text
  23.  * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/UnicodeEscaper.html">
  24.  * UnicodeEscaper</a> instead
  25.  */
  26. @Deprecated
  27. public class JavaUnicodeEscaper extends UnicodeEscaper {

  28.     /**
  29.      * Constructs a {@link JavaUnicodeEscaper} above the specified value (exclusive).
  30.      *
  31.      * @param codePoint
  32.      *            above which to escape
  33.      * @return the newly created {@link UnicodeEscaper} instance
  34.      */
  35.     public static JavaUnicodeEscaper above(final int codePoint) {
  36.         return outsideOf(0, codePoint);
  37.     }

  38.     /**
  39.      * Constructs a {@link JavaUnicodeEscaper} below the specified value (exclusive).
  40.      *
  41.      * @param codePoint
  42.      *            below which to escape
  43.      * @return the newly created {@link UnicodeEscaper} instance
  44.      */
  45.     public static JavaUnicodeEscaper below(final int codePoint) {
  46.         return outsideOf(codePoint, Integer.MAX_VALUE);
  47.     }

  48.     /**
  49.      * Constructs a {@link JavaUnicodeEscaper} between the specified values (inclusive).
  50.      *
  51.      * @param codePointLow
  52.      *            above which to escape
  53.      * @param codePointHigh
  54.      *            below which to escape
  55.      * @return the newly created {@link UnicodeEscaper} instance
  56.      */
  57.     public static JavaUnicodeEscaper between(final int codePointLow, final int codePointHigh) {
  58.         return new JavaUnicodeEscaper(codePointLow, codePointHigh, true);
  59.     }

  60.     /**
  61.      * Constructs a {@link JavaUnicodeEscaper} outside of the specified values (exclusive).
  62.      *
  63.      * @param codePointLow
  64.      *            below which to escape
  65.      * @param codePointHigh
  66.      *            above which to escape
  67.      * @return the newly created {@link UnicodeEscaper} instance
  68.      */
  69.     public static JavaUnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
  70.         return new JavaUnicodeEscaper(codePointLow, codePointHigh, false);
  71.     }

  72.     /**
  73.      * Constructs a {@link JavaUnicodeEscaper} for the specified range. This is the underlying method for the
  74.      * other constructors/builders. The {@code below} and {@code above} boundaries are inclusive when
  75.      * {@code between} is {@code true} and exclusive when it is {@code false}.
  76.      *
  77.      * @param below
  78.      *            int value representing the lowest code point boundary
  79.      * @param above
  80.      *            int value representing the highest code point boundary
  81.      * @param between
  82.      *            whether to escape between the boundaries or outside them
  83.      */
  84.     public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
  85.         super(below, above, between);
  86.     }

  87.     /**
  88.      * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX\\uXXXX"}
  89.      *
  90.      * @param codePoint
  91.      *            a Unicode code point
  92.      * @return the hexadecimal string for the given code point
  93.      */
  94.     @Override
  95.     protected String toUtf16Escape(final int codePoint) {
  96.         final char[] surrogatePair = Character.toChars(codePoint);
  97.         return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
  98.     }

  99. }