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.text.translate;

  18. /**
  19.  * Translates code points to their Unicode escaped value suitable for Java source.
  20.  *
  21.  * @since 1.0
  22.  */
  23. public class JavaUnicodeEscaper extends UnicodeEscaper {

  24.     /**
  25.      * Constructs a {@code JavaUnicodeEscaper} above the specified value (exclusive).
  26.      *
  27.      * @param codePoint
  28.      *            above which to escape
  29.      * @return The newly created {@code UnicodeEscaper} instance
  30.      */
  31.     public static JavaUnicodeEscaper above(final int codePoint) {
  32.         return outsideOf(0, codePoint);
  33.     }

  34.     /**
  35.      * Constructs a {@code JavaUnicodeEscaper} below the specified value (exclusive).
  36.      *
  37.      * @param codePoint
  38.      *            below which to escape
  39.      * @return The newly created {@code UnicodeEscaper} instance
  40.      */
  41.     public static JavaUnicodeEscaper below(final int codePoint) {
  42.         return outsideOf(codePoint, Integer.MAX_VALUE);
  43.     }

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

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

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

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

  95. }