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