/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Lachlan Dowding
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package permafrost.tundra.data.transform.string;

import com.wm.data.IData;
import com.wm.data.IDataCursor;
import com.wm.data.IDataFactory;
import permafrost.tundra.data.IDataHelper;
import permafrost.tundra.data.transform.Transformer;
import permafrost.tundra.data.transform.TransformerMode;

/**
 * Translates an input value to an output value using a translation table.
 */
public class Translator extends Transformer<String, Object> {
    /**
     * The translations used when translating values.
     */
    private final IData translations;
    /**
     * The default value used when translating a value with no translation, and the value used when translating a null
     * value.
     */
    private final Object defaultValue, nullValue;

    /**
     * Creates a new Translator object.
     *
     * @param translations  The translation table used to translate values.
     * @param reverse       If true, the translation table is flipped so keys become values and vice versa.
     */
    public Translator(IData translations, boolean reverse) {
        super(String.class, Object.class, TransformerMode.VALUES, true, true, true, true);
        if (translations == null) {
            this.translations = IDataFactory.create();
            defaultValue = null;
            nullValue = null;
        } else {
            translations = IDataHelper.duplicate(translations, true);
            defaultValue = IDataHelper.remove(translations, "$default");
            nullValue = IDataHelper.remove(translations, "$null");

            if (reverse) {
                this.translations = IDataHelper.flip(translations);
            } else {
                this.translations = translations;
            }
        }
    }

    /**
     * Transforms the given value.
     *
     * @param key   The key associated with the value being transformed.
     * @param value The value to be transformed.
     * @return      The transformed value.
     */
    @Override
    protected Object transformValue(String key, String value) {
        IDataCursor cursor = translations.getCursor();
        try {
            Object defaultValue = this.defaultValue == null ? value : this.defaultValue;
            return IDataHelper.getOrDefault(cursor, value, Object.class, defaultValue);
        } finally {
            cursor.destroy();
        }
    }

    /**
     * Translates a null value.
     *
     * @param key   The key associated with the null value being transformed.
     * @return      The transformed null value.
     */
    @Override
    protected Object transformNull(String key) {
        return nullValue;
    }
}

@@ -1888,16 +1888,147 @@
 llValue;
+%0A    /**%0A     * Whether to exclude missing translation values from translated documents.%0A     */%0A    private final boolean exclude;
 %0A%0A    /*
@@ -2148,24 +2148,135 @@
 ate values.%0A
+     * @param exclude       If true, missing translation values will be excluded from the translated document.%0A
      * @para
@@ -2420,16 +2420,33 @@
 lations,
+ boolean exclude,
  boolean
@@ -2525,27 +2525,31 @@
 LUES, true, 
-tru
+!exclud
 e, true, tru
@@ -2544,32 +2544,64 @@
 e, true, true);%0A
+        this.exclude = exclude;%0A
         if (tran
@@ -2829,32 +2829,39 @@
 e);%0A            
+Object 
 defaultValue = I
@@ -2852,24 +2852,31 @@
 defaultValue
+Forward
  = IDataHelp
@@ -2916,32 +2916,134 @@
 %22);%0A            
+Object defaultValueReverse = IDataHelper.remove(translations, %22$default.reverse%22);%0A            Object 
 nullValue = IDat
@@ -3035,16 +3035,23 @@
 ullValue
+Forward
  = IData
@@ -3088,16 +3088,104 @@
 null%22);%0A
+            Object nullValueReverse = IDataHelper.remove(translations, %22$null.reverse%22);
 %0A       
@@ -3288,65 +3288,281 @@
     
-%7D else %7B%0A                this.translations = translations
+    this.defaultValue = defaultValueReverse;%0A                this.nullValue = nullValueReverse;%0A            %7D else %7B%0A                this.translations = translations;%0A                this.defaultValue = defaultValueForward;%0A                this.nullValue = nullValueForward
 ;%0A  
@@ -3991,19 +3991,34 @@
 ultValue
- = 
+;%0A            if (
 this.def
@@ -4038,18 +4038,220 @@
 null
- ? value :
+) %7B%0A                if (exclude) %7B%0A                    defaultValue = null;%0A                %7D else %7B%0A                    defaultValue = value;%0A                %7D%0A            %7D else %7B%0A                defaultValue =
  thi
@@ -4258,32 +4258,46 @@
 s.defaultValue;%0A
+            %7D%0A
             retu

Change permafrost.tundra.data.transform.string.Translator to support excluding values with no translation, and to support dedicated default and null translations when translation is reversed