/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.base;

import com.google.common.base.Converter;

import java.util.regex.Pattern;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.spine3.util.Exceptions.newIllegalArgumentException;

/**
 * Serves as enclosure and disclosure for the elements into and from quotes.
 */
class ItemQuoter {

    private static final char QUOTE_SYMBOL = '\"';

    private ItemQuoter() {
        // Disable instantiation from the outside.
    }

    static Converter<String, String> converter() {
        return new QuotedItemConverter();
    }

    /**
     * Checks that the {@code CharSequence} contains escaped quotes.
     *
     * @param stringToCheck the sequence of chars to check
     * @return {@code true} if sequence contains further
     * and prior escaped quotes, {@code false} otherwise
     */
    static boolean isQuotedString(CharSequence stringToCheck) {
        final int stringLength = stringToCheck.length();

        if (stringLength < 2) {
            return false;
        }

        boolean result = isQuote(stringToCheck.charAt(1)) &&
                         isQuote(stringToCheck.charAt(stringLength - 1));
        return result;
    }

    private static boolean isQuote(char character) {
        return character == QUOTE_SYMBOL;
    }

    private static class QuotedItemConverter extends Converter<String, String> {

        @Override
        protected String doForward(String s) {
            return quoteElement(s);
        }

        private static String quoteElement(String stringToQuote) {
            checkNotNull(stringToQuote);
            return QUOTE_SYMBOL + stringToQuote + QUOTE_SYMBOL;
        }

        @Override
        protected String doBackward(String s) {
            checkElement(s);
            return unquoteElement(s);
        }

        private static void checkElement(CharSequence element) {
            final boolean isQuoted = isQuotedString(element);
            if (!isQuoted) {
                throw newIllegalArgumentException("Illegal format of the element: " + element);
            }
        }

        private static String unquoteElement(String value) {
            checkNotNull(value);
            final String unquotedValue = Pattern.compile("\\\\")
                                                .matcher(value.substring(2, value.length() - 2))
                                                .replaceAll("");
            return unquotedValue;
        }
    }
}

@@ -1258,27 +1258,16 @@
 %0A * 
-Serves as e
+E
 nclos
-ur
 e
+s
  and
@@ -1278,42 +1278,42 @@
 clos
-ure for the elements into and from
+es the %7B@code String%7D objects with
  quo
@@ -1521,32 +1521,32 @@
 %3E converter() %7B%0A
+
         return n
@@ -1582,731 +1582,8 @@
  %7D%0A%0A
-    /**%0A     * Checks that the %7B@code CharSequence%7D contains escaped quotes.%0A     *%0A     * @param stringToCheck the sequence of chars to check%0A     * @return %7B@code true%7D if sequence contains further%0A     * and prior escaped quotes, %7B@code false%7D otherwise%0A     */%0A    static boolean isQuotedString(CharSequence stringToCheck) %7B%0A        final int stringLength = stringToCheck.length();%0A%0A        if (stringLength %3C 2) %7B%0A            return false;%0A        %7D%0A%0A        boolean result = isQuote(stringToCheck.charAt(1)) &&%0A                         isQuote(stringToCheck.charAt(stringLength - 1));%0A        return result;%0A    %7D%0A%0A    private static boolean isQuote(char character) %7B%0A        return character == QUOTE_SYMBOL;%0A    %7D%0A%0A
     
@@ -2736,21 +2736,828 @@
 e;%0A        %7D
+%0A%0A        /**%0A         * Checks that the %7B@code CharSequence%7D contains escaped quotes.%0A         *%0A         * @param stringToCheck the sequence of chars to check%0A         * @return %7B@code true%7D if sequence contains further%0A         * and prior escaped quotes, %7B@code false%7D otherwise%0A         */%0A        private static boolean isQuotedString(CharSequence stringToCheck) %7B%0A            final int stringLength = stringToCheck.length();%0A%0A            if (stringLength %3C 2) %7B%0A                return false;%0A            %7D%0A%0A            boolean result = isQuote(stringToCheck.charAt(1)) &&%0A                             isQuote(stringToCheck.charAt(stringLength - 1));%0A            return result;%0A        %7D%0A%0A        private static boolean isQuote(char character) %7B%0A            return character == QUOTE_SYMBOL;%0A        %7D
 %0A    %7D%0A%7D%0A

Fix the documentation and reduce visibility of the method.