001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3.text.translate;
018
019/**
020 * Translates code points to their Unicode escaped value suitable for Java source.
021 *
022 * @since 3.2
023 * @deprecated As of 3.6, use Apache Commons Text
024 * <a href="https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/translate/UnicodeEscaper.html">
025 * UnicodeEscaper</a> instead
026 */
027@Deprecated
028public class JavaUnicodeEscaper extends UnicodeEscaper {
029
030    /**
031     * Constructs a {@link JavaUnicodeEscaper} above the specified value (exclusive).
032     *
033     * @param codePoint
034     *            above which to escape
035     * @return the newly created {@link UnicodeEscaper} instance
036     */
037    public static JavaUnicodeEscaper above(final int codePoint) {
038        return outsideOf(0, codePoint);
039    }
040
041    /**
042     * Constructs a {@link JavaUnicodeEscaper} below the specified value (exclusive).
043     *
044     * @param codePoint
045     *            below which to escape
046     * @return the newly created {@link UnicodeEscaper} instance
047     */
048    public static JavaUnicodeEscaper below(final int codePoint) {
049        return outsideOf(codePoint, Integer.MAX_VALUE);
050    }
051
052    /**
053     * Constructs a {@link JavaUnicodeEscaper} between the specified values (inclusive).
054     *
055     * @param codePointLow
056     *            above which to escape
057     * @param codePointHigh
058     *            below which to escape
059     * @return the newly created {@link UnicodeEscaper} instance
060     */
061    public static JavaUnicodeEscaper between(final int codePointLow, final int codePointHigh) {
062        return new JavaUnicodeEscaper(codePointLow, codePointHigh, true);
063    }
064
065    /**
066     * Constructs a {@link JavaUnicodeEscaper} outside of the specified values (exclusive).
067     *
068     * @param codePointLow
069     *            below which to escape
070     * @param codePointHigh
071     *            above which to escape
072     * @return the newly created {@link UnicodeEscaper} instance
073     */
074    public static JavaUnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
075        return new JavaUnicodeEscaper(codePointLow, codePointHigh, false);
076    }
077
078    /**
079     * Constructs a {@link JavaUnicodeEscaper} for the specified range. This is the underlying method for the
080     * other constructors/builders. The {@code below} and {@code above} boundaries are inclusive when
081     * {@code between} is {@code true} and exclusive when it is {@code false}.
082     *
083     * @param below
084     *            int value representing the lowest code point boundary
085     * @param above
086     *            int value representing the highest code point boundary
087     * @param between
088     *            whether to escape between the boundaries or outside them
089     */
090    public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
091        super(below, above, between);
092    }
093
094    /**
095     * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX\\uXXXX"}
096     *
097     * @param codePoint
098     *            a Unicode code point
099     * @return the hexadecimal string for the given code point
100     */
101    @Override
102    protected String toUtf16Escape(final int codePoint) {
103        final char[] surrogatePair = Character.toChars(codePoint);
104        return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
105    }
106
107}