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 codepoints to their Unicode escaped value suitable for Java source.
  20.  *
  21.  * @since 1.0
  22.  */
  23. public class JavaUnicodeEscaper extends UnicodeEscaper {

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

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

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

  62.     /**
  63.      * <p>
  64.      * Constructs a <code>JavaUnicodeEscaper</code> outside of the specified values (exclusive).
  65.      * </p>
  66.      *
  67.      * @param codepointLow
  68.      *            below which to escape
  69.      * @param codepointHigh
  70.      *            above which to escape
  71.      * @return the newly created {@code UnicodeEscaper} instance
  72.      */
  73.     public static JavaUnicodeEscaper outsideOf(final int codepointLow, final int codepointHigh) {
  74.         return new JavaUnicodeEscaper(codepointLow, codepointHigh, false);
  75.     }

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

  93.     /**
  94.      * Converts the given codepoint to a hex string of the form {@code "\\uXXXX\\uXXXX"}
  95.      *
  96.      * @param codepoint
  97.      *            a Unicode code point
  98.      * @return the hex string for the given codepoint
  99.      */
  100.     @Override
  101.     protected String toUtf16Escape(final int codepoint) {
  102.         final char[] surrogatePair = Character.toChars(codepoint);
  103.         return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
  104.     }

  105. }