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.text.translate;
018
019/**
020 * Translates code points to their Unicode escaped value suitable for Java source.
021 *
022 * @since 1.0
023 */
024public class JavaUnicodeEscaper extends UnicodeEscaper {
025
026    /**
027     * Constructs a {@code JavaUnicodeEscaper} above the specified value (exclusive).
028     *
029     * @param codePoint
030     *            above which to escape
031     * @return The newly created {@code UnicodeEscaper} instance
032     */
033    public static JavaUnicodeEscaper above(final int codePoint) {
034        return outsideOf(0, codePoint);
035    }
036
037    /**
038     * Constructs a {@code JavaUnicodeEscaper} below the specified value (exclusive).
039     *
040     * @param codePoint
041     *            below which to escape
042     * @return The newly created {@code UnicodeEscaper} instance
043     */
044    public static JavaUnicodeEscaper below(final int codePoint) {
045        return outsideOf(codePoint, Integer.MAX_VALUE);
046    }
047
048    /**
049     * Constructs a {@code JavaUnicodeEscaper} between the specified values (inclusive).
050     *
051     * @param codePointLow
052     *            above which to escape
053     * @param codePointHigh
054     *            below which to escape
055     * @return The newly created {@code UnicodeEscaper} instance
056     */
057    public static JavaUnicodeEscaper between(final int codePointLow, final int codePointHigh) {
058        return new JavaUnicodeEscaper(codePointLow, codePointHigh, true);
059    }
060
061    /**
062     * Constructs a {@code JavaUnicodeEscaper} outside of the specified values (exclusive).
063     *
064     * @param codePointLow
065     *            below which to escape
066     * @param codePointHigh
067     *            above which to escape
068     * @return The newly created {@code UnicodeEscaper} instance
069     */
070    public static JavaUnicodeEscaper outsideOf(final int codePointLow, final int codePointHigh) {
071        return new JavaUnicodeEscaper(codePointLow, codePointHigh, false);
072    }
073
074    /**
075     * Constructs a {@code JavaUnicodeEscaper} for the specified range. This is the underlying method for the
076     * other constructors/builders. The {@code below} and {@code above} boundaries are inclusive when
077     * {@code between} is {@code true} and exclusive when it is {@code false}.
078     *
079     * @param below
080     *            int value representing the lowest code point boundary
081     * @param above
082     *            int value representing the highest code point boundary
083     * @param between
084     *            whether to escape between the boundaries or outside them
085     */
086    public JavaUnicodeEscaper(final int below, final int above, final boolean between) {
087        super(below, above, between);
088    }
089
090    /**
091     * Converts the given code point to a hexadecimal string of the form {@code "\\uXXXX\\uXXXX"}.
092     *
093     * @param codePoint
094     *            a Unicode code point
095     * @return The hexadecimal string for the given code point
096     */
097    @Override
098    protected String toUtf16Escape(final int codePoint) {
099        final char[] surrogatePair = Character.toChars(codePoint);
100        return "\\u" + hex(surrogatePair[0]) + "\\u" + hex(surrogatePair[1]);
101    }
102
103}