View Javadoc
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  /**
20   * Translates code points to their Unicode escaped value suitable for Java source.
21   *
22   * @since 1.0
23   */
24  public class JavaUnicodeEscaper extends UnicodeEscaper {
25  
26      /**
27       * Constructs a {@code JavaUnicodeEscaper} above the specified value (exclusive).
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      /**
38       * Constructs a {@code JavaUnicodeEscaper} below the specified value (exclusive).
39       *
40       * @param codePoint
41       *            below which to escape
42       * @return The newly created {@code UnicodeEscaper} instance
43       */
44      public static JavaUnicodeEscaper below(final int codePoint) {
45          return outsideOf(codePoint, Integer.MAX_VALUE);
46      }
47  
48      /**
49       * Constructs a {@code 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 {@code UnicodeEscaper} instance
56       */
57      public static JavaUnicodeEscaper between(final int codePointLow, final int codePointHigh) {
58          return new JavaUnicodeEscaper(codePointLow, codePointHigh, true);
59      }
60  
61      /**
62       * Constructs a {@code JavaUnicodeEscaper} outside of the specified values (exclusive).
63       *
64       * @param codePointLow
65       *            below which to escape
66       * @param codePointHigh
67       *            above which to escape
68       * @return The newly created {@code UnicodeEscaper} instance
69       */
70      public static JavaUnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
71          return new JavaUnicodeEscaper(codePointLow, codePointHigh, false);
72      }
73  
74      /**
75       * Constructs a {@code JavaUnicodeEscaper} for the specified range. This is the underlying method for the
76       * other constructors/builders. The {@code below} and {@code above} boundaries are inclusive when
77       * {@code between} is {@code true} and exclusive when it is {@code false}.
78       *
79       * @param below
80       *            int value representing the lowest code point boundary
81       * @param above
82       *            int value representing the highest code point boundary
83       * @param between
84       *            whether to escape between the boundaries or outside them
85       */
86      public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
87          super(below, above, between);
88      }
89  
90      /**
91       * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX\\uXXXX"}.
92       *
93       * @param codePoint
94       *            a Unicode code point
95       * @return The hexadecimal string for the given code point
96       */
97      @Override
98      protected String toUtf16Escape(final int codePoint) {
99          final char[] surrogatePair = Character.toChars(codePoint);
100         return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
101     }
102 
103 }