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