/*
 * Copyright (c) 2013-2017 Cinchapi Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cinchapi.concourse.importer;

import java.util.Arrays;

import com.cinchapi.concourse.importer.util.Importables;
import com.cinchapi.concourse.util.KeyValue;
import com.google.common.base.CaseFormat;

/**
 * Utilities for import {@link Transformer} objects.
 * 
 * @author Jeff Nelson
 */
public final class Transformers {

    /**
     * A {@link Transformer} that strips invalid characters from the key. Some
     * invalid characters are replaced with valid stand-ins.
     */
    private final Transformer KEY_STRIP_INVALID_CHARS = (key, value) -> {
        StringBuilder sb = new StringBuilder();
        for (char c : key.toCharArray()) {
            if(Importables.isValidKeyCharacter(c)) {
                sb.append(c);
            }
            else if(c == '#') {
                sb.append("num");
            }
            else if(c == ' ') {
                sb.append("_");
            }
        }
        return new KeyValue<>(sb.toString(), value);
    };

    private Transformers() {/* no init */}

    /**
     * Return a {@link CompositeTransformer} that invokes each of the
     * {@code transformers} in order.
     * 
     * @param transformers the {@link Transformer transformers} to invoke
     * @return a {@link CompositeTransformer}
     */
    public Transformer compose(Transformer... transformers) {
        return new CompositeTransformer(Arrays.asList(transformers));
    }
    
    /**
     * Return a {@link Transformer} that converts keys {@code from} one case
     * format {@code to} another one.
     * 
     * @param from the original {@link CaseFormat}
     * @param to the desired {@link CaseFormat}
     * @return the {@link Transformer
     */
    public Transformer keyCaseFormat(CaseFormat from, CaseFormat to) {
        return (key, value) -> {
            key = from.to(to, key);
            return new KeyValue<>(key, value);
        };
    }

    /**
     * Return a {@link Transformer} that strips invalid characters from the key.
     * Some of those characters are replaced with valid stand-ins.
     * <h1>Replacements</h1>
     * <ul>
     * <li>a white space character (e.g. ' ') is replaced with an
     * underscore</li>
     * <li>the pound sign is replaced the string {@code num}</li>
     * </ul>
     * <p>
     * 
     * @return the {@link Transformer}
     */
    public Transformer keyStripInvalidChars() {
        return KEY_STRIP_INVALID_CHARS;
    }

}

@@ -765,16 +765,60 @@
 yValue;%0A
+import com.cinchapi.concourse.util.Strings;%0A
 import c
@@ -851,16 +851,16 @@
 Format;%0A
-
 %0A/**%0A * 
@@ -1622,16 +1622,497 @@
     %7D;%0A%0A
+    /**%0A     * A %7B@link Transformer%7D that will strip single and double quotes from the%0A     * beginning and end of both the key and value.%0A     */%0A    private final Transformer STRIP_QUOTES = (key, value) -%3E %7B%0A        if(Strings.isWithinQuotes(key)) %7B%0A            key = key.substring(1, key.length() - 1);%0A        %7D%0A        if(Strings.isWithinQuotes(value)) %7B%0A            value = value.substring(1, value.length() - 1);%0A        %7D%0A        return new KeyValue%3C%3E(key, value);%0A    %7D;%0A%0A
     priv
@@ -2541,13 +2541,9 @@
   %7D%0A
-    
 %0A
+
     
@@ -3494,24 +3494,24 @@
 idChars() %7B%0A
-
         retu
@@ -3545,10 +3545,239 @@
 %0A    %7D%0A%0A
+    /**%0A     * Return a %7B@link Transformer%7D that will strip single and double quotes%0A     * from the beginning and end of both the key and value.%0A     */%0A    public Transformer stripQuotes() %7B%0A        return STRIP_QUOTES;%0A    %7D%0A%0A
 %7D%0A

add a Transformer that will strip quotes