# ConfigBuilder.java

    /*
    ** Module   : ConfigBuilder.java
    ** Abstract : Implementation of the builtin class.
    **
    ** Copyright (c) 2019-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------------Description----------------------------------------
    ** 001 CA  20190526 First version, stubs taken by converting the skeleton using FWD.
    ** 002 IAS 20190923 Implementation of the business logic
    ** 003 CA  20191024 Added method support levels and updated the class support level.
    ** 004 ME  20201202 Options map is case insensitive in 4GL.
    **         20201207 Use character as map key since unknown keys are allowed in 4GL.
    **                  Add new methods as of OE12.2.
    ** 005 CA  20210221 Added 'extent' for getOptionStringArrayValue method.
    ** 006 ME  20210310 Increment object reference on option set, decrement on remove.
    **     ME  20210310 Return ObjectVar on getOptionObjectValue so the reference is incremented.
    **     VVT 20210917 Javadoc fixed.
    **     CA  20220120 Do not used TypeFactory.object when the OO reference must not be tracked or registered.
    **                  Do not use TypeFactory.object for internal usages, use ObjectVar if the reference must
    **                  be tracked.
    **                  All TypeFactory.object variable definitions must be done outside of the top-level block.
    **     CA  20220923 Variable definitions (including associated with parameters) must be done always outside of
    **                  the BlockManager API
    ** 007 CA  20231113 The 'execute' method must be annotated with LegacySignature Type.Execute, and also can be
    **                  dropped if is a no-op.
    ** 008 AL2 20240930 Added destructor which calls ClearOptions. Added reference count management to
    **                  ClearOptions and SetOption.
    */

    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.oo.core.util;

    import static com.goldencode.p2j.util.BlockManager.*;
    import static com.goldencode.p2j.report.ReportConstants.*;
    import static com.goldencode.p2j.util.ErrorManager.*;

    import java.util.*;

    import com.goldencode.p2j.oo.core.*;
    import com.goldencode.p2j.oo.json.objectmodel.*;
    import com.goldencode.p2j.oo.lang.*;
    import com.goldencode.p2j.util.*;
    import com.goldencode.p2j.util.InternalEntry.*;

    /**
     *    helper class for configuration data used by builders
     */
    @LegacyResource(resource = "OpenEdge.Core.Util.ConfigBuilder")
    @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
    public abstract class ConfigBuilder
    extends BaseObject
    {
       private static final String JSON_ARRAY = "Progress.Json.ObjectModel.JsonArray";

       /** Option helpers by type map. */
       private static final Map<Class<?>, OptionTypeHelper<?>> TYPES = Collections.unmodifiableMap(
             new HashMap<Class<?>, OptionTypeHelper<?>>()
             {
             {
                put(decimal.class, new OptionTypeHelper<decimal>("number")
                {
                   @Override
                   public decimal newInstance()
                   {
                      return new decimal();
                   }
                });
                put(object.class, new OptionTypeHelper<object<? extends BaseObject>>("object")
                {
                   @Override
                   public object<? extends BaseObject> newInstance()
                   {
                      return new ObjectVar<>(BaseObject.class);
                   }

                });
                put(character.class, new OptionTypeHelper<character>("string")
                {
                   @Override
                   public character newInstance()
                   {
                      return new character();
                   }
                });
                put(logical.class, new OptionTypeHelper<logical>("logical")
                {
                   @Override
                   public logical newInstance()
                   {
                      return new logical();
                   }
                });
                put(datetimetz.class, new OptionTypeHelper<datetimetz>("datetime")
                {
                   @Override
                   public datetimetz newInstance()
                   {
                      return new datetimetz();
                   }
                });
             }}
       );

       /** Options by name map. */
       protected final Map<character, OptionValueHolder> options = new HashMap<character, ConfigBuilder.OptionValueHolder>();

       /**
        * Constructor.
        */
       @LegacySignature(type = Type.CONSTRUCTOR)
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public void __core_util_ConfigBuilder_constructor__()
       {
          internalProcedure(this, "__core_util_ConfigBuilder_constructor__", new Block((Body) () ->
          {
             __lang_BaseObject_constructor__();
          }));
       }

       /**
        * Appends a character value to an array.
        *
        * @param _p1 optiona name.
        * @param _p2 value to append.
        *
        * @return <code>true</code> if option was updated.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "AppendArrayCharacterValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "p2", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical appendArrayCharacterValue(final character _p1, final character _p2)
       {
          character key = TypeFactory.initInput(_p1);
          character val = TypeFactory.initInput(_p2);
          object<? extends JsonArray> arr = TypeFactory.object(JsonArray.class);

          return function(this, "AppendArrayCharacterValue", logical.class, new Block((Body) () ->
          {
             Assert.notNullOrEmpty(key, new character("Config name"));

            arr.assign(!hasOption(key).booleanValue() ?
                   ObjectOps.newInstance(JsonArray.class) :
                   ObjectOps.cast(getOption(key, object.class), JsonArray.class)
             );
             arr.ref().add(val);
             returnNormal(setOption(key, arr));
          }));
       }

       /**
        * Clears all options for this builder.
        */
       @LegacySignature(type = Type.METHOD, name = "ClearOptions")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public void clearOptions()
       {
          internalProcedure(this, "ClearOptions", new Block((Body) () ->
          {
             options.values().forEach(entry ->
             {
                Object val = entry.value();
                if (val != null && val instanceof object && ((object<?>) val)._isValid())
                {
                   ObjectOps.decrement(((object<?>) val).ref());
                }
             });

             options.clear();
          }));
       }

       /**
        * Returns an option's datetime value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "DATETIMETZ", type = Type.METHOD, name = "GetOptionDateTimeValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public datetimetz getOptionDateTimeValue(final character _p1)
       {
          character p1 = TypeFactory.initInput(_p1);

          return function(this, "GetOptionDateTimeValue", datetimetz.class, new Block((Body) () ->
          {
             returnNormal(getOption(p1, datetimetz.class));
          }));
       }

       /**
        * Returns an option's logical value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "GetOptionLogicalValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical getOptionLogicalValue(final character _p1)
       {
          character p1 = TypeFactory.initInput(_p1);

          return function(this, "GetOptionLogicalValue", logical.class, new Block((Body) () ->
          {
             returnNormal(getOption(p1, logical.class));
          }));
       }

       /**
        * Returns an option's longchar value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "LONGCHAR", type = Type.METHOD, name = "GetOptionLongcharValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public longchar getOptionLongcharValue(final character _p1)
       {
          character p1 = TypeFactory.initInput(_p1);

          return function(this, "GetOptionLongcharValue", longchar.class, new Block((Body) () ->
          {
             longchar val = TypeFactory.longchar();
             val.assign(getOption(p1, character.class));
             returnNormal(val);
          }));
       }

       /**
        * Returns an option's numeric value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "DECIMAL", type = Type.METHOD, name = "GetOptionNumericValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public decimal getOptionNumericValue(final character _p1)
       {
          character p1 = TypeFactory.initInput(_p1);

          return function(this, "GetOptionNumericValue", decimal.class, new Block((Body) () ->
          {
             returnNormal(getOption(p1, decimal.class));
          }));
       }

       /**
        * Returns an option's object value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "GetOptionObjectValue", qualified = "progress.lang.object", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public object<? extends _BaseObject_> getOptionObjectValue(final character _p1)
       {
          character p1 = TypeFactory.initInput(_p1);
          object<? extends _BaseObject_> ret = TypeFactory.object(_BaseObject_.class);

          return function(this, "GetOptionObjectValue", object.class, new Block((Body) () ->
          {
             ret.assign(getOption(p1, object.class));

             returnNormal(ret);
          }));
       }

       /**
        * Returns an option's character[] value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "CHARACTER", type = Type.METHOD, extent = -1, name = "GetOptionStringArrayValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public character[] getOptionStringArrayValue(final character _p1)
       {
          character key = TypeFactory.initInput(_p1);
          object<? extends JsonArray> arr = TypeFactory.object(JsonArray.class);
          character[][] val = { TypeFactory.characterExtent() };

          return extentFunction(this, "GetOptionStringArrayValue", character.class, new Block((Body) () ->
          {
             object<? extends _BaseObject_>  obj = getOption(key, object.class);
             if (obj.isValid().booleanValue())
             {
                Assert.isType(obj, ObjectOps.getLegacyClass(JsonArray.class));
                arr.assign(ObjectOps.cast(obj, JsonArray.class));
                JsonArray json = arr.ref();
                if(ObjectOps.isValid(json))
                {
                   val[0] = ArrayAssigner.resize(val[0], json.getLength().intValue());
                   for(int i = 0; i < val.length; i++)
                   {
                      ArrayAssigner.assignSingle(val[0], i + 1, json.getCharacter(new integer(i + 1)));
                   }
                }

             }
             returnExtentNormal(val[0]);
          }));
       }

       /**
        * Returns an option's character value.
        *
        * @param _p1 option name.
        *
        * @return the value.
        */
       @LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "GetOptionStringValue", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public character getOptionStringValue(final character _p1)
       {
          character p1 = TypeFactory.initInput(_p1);

          return function(this, "GetOptionStringValue", character.class, new Block((Body) () ->
          {
             returnNormal(getOption(p1, character.class));
          }));
       }

       /**
        *  Checks whether a config option already exists.
        *
        * @param _p1 option name.
        *
        * @return <code>true</code> if the named configuration option exists.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "HasOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical hasOption(final character _p1)
       {
          character key = TypeFactory.initInput(_p1);

          return function(this, "HasOption", logical.class, new Block((Body) () ->
          {
             Assert.notNullOrEmpty(key, new character("Config name"));
             returnNormal(
                   new logical(options.containsKey(key))
             );
          }));
       }

       /**
        * Check if options map (characters) contains the string key.
        *
        * @param key the key name
        *
        * @return {@code true} is there is an option with the given name
        */
       protected boolean _hasOption (String key) {
          return options.containsKey(new character(key));
       }

       /**
        * Removes an option
        *
        * @param _p1 option name
        *
        * @return <code>true</code> if the named configuration was removed.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "RemoveOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical removeOption(final character _p1)
       {
          character key = TypeFactory.initInput(_p1);

          return function(this, "RemoveOption", logical.class, new Block((Body) () ->
          {
             OptionValueHolder val = options.remove(key);

             if (val != null && val.value() != null && val.type() == "object") {
                object obj = (object) val.value();
                if (obj._isValid())
                   ObjectOps.decrement(obj.ref());
             }

             returnNormal(new logical( val != null));
          }));
       }

       /**
        * Stores a character value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "p2", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final character _p1, final character _p2)
       {
          character p1 = TypeFactory.initInput(_p1);
          character p2 = TypeFactory.initInput(_p2);

          return function(this, "SetOption", logical.class, new Block((Body) () ->
          {
             returnNormal(setOption(p1, p2, character.class));
          }));
       }

       /**
        * Stores a character value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters = {
                @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
                @LegacyParameter(name = "p2", type = "CHARACTER", extent = -1, mode = "INPUT") })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final character _p1, final character[] _p2)
       {
          character p1 = TypeFactory.initInput(_p1);
          character p2[] = TypeFactory.initInput(_p2);
          object<JsonArray> json = TypeFactory.object(JsonArray.class);

          return function(this, "SetOption", logical.class,
                   new Block((Body) () -> {
                      if (p2.length > 0)
                      {
                         // work around issue on ctor with extent parameter
                         json.assign(ObjectOps.newInstance(JsonArray.class));

                         for (character chr : p2)
                         {
                            json.ref().add(chr);
                         }
                      }

                      returnNormal(setOption(p1, json));
                   }));
       }

       /**
        * Stores a datetime-tz value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "p2", type = "DATETIMETZ", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final character _p1, final datetimetz _p2)
       {
          character p1 = TypeFactory.initInput(_p1);
          datetimetz p2 = TypeFactory.initInput(_p2);

          return function(this, "SetOption", logical.class, new Block((Body) () ->
          {
             returnNormal(setOption(p1, p2, datetimetz.class));
          }));
       }

       /**
        * Stores a decimal value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "p2", type = "DECIMAL", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final character _p1, final decimal _p2)
       {
          character p1 = TypeFactory.initInput(_p1);
          decimal p2 = TypeFactory.initInput(_p2);

          return function(this, "SetOption", logical.class, new Block((Body) () ->
          {
             returnNormal(setOption(p1, p2, decimal.class));
          }));
       }

       /**
        * Stores a logical value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "p2", type = "LOGICAL", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final character _p1, final logical _p2)
       {
          character p1 = TypeFactory.initInput(_p1);
          logical p2 = TypeFactory.initInput(_p2);

          return function(this, "SetOption", logical.class, new Block((Body) () ->
          {
             returnNormal(setOption(p1, p2, logical.class));
          }));
       }

       /**
        * Stores an object value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters =
       {
          @LegacyParameter(name = "p1", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "p2", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final character _p1, final object<? extends _BaseObject_> _p2)
       {
          character p1 = TypeFactory.initInput(_p1);
          object<? extends _BaseObject_> p2 = TypeFactory.initInput(_p2);

          return function(this, "SetOption", logical.class, new Block((Body) () ->
          {
             if (p2._isValid())
             {
                ObjectOps.increment(p2.ref());
             }

             returnNormal(setOption(p1, p2, object.class));
          }));
       }

       /**
        * Stores an object value as an option.
        *
        * @param _p1 option name.
        * @param _p2 option value,
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "SetOption", parameters = {
                @LegacyParameter(name = "p1", type = "OBJECT", qualified = "progress.lang.class", mode = "INPUT"),
                @LegacyParameter(name = "p2", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT") })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical setOption(final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _p1,
                final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _p2)
       {
          object<? extends LegacyClass> p1 = TypeFactory.initInput(_p1);
          object<? extends _BaseObject_> p2 = TypeFactory.initInput(_p2);

          return function(this, "SetOption", logical.class, new Block((Body) () -> {
             Assert.notNull(p1, new character("Config name"));

             returnNormal(setOption(p1.ref().getTypeName(), p2));
          }));
       }

       /**
        * Destructor.
        */
       @LegacySignature(type = Type.DESTRUCTOR)
       public void __core_util_ConfigBuilder_destructor__()
       {
          internalProcedure(ConfigBuilder.class, this, "__core_util_ConfigBuilder_destructor__", new Block((Body) () ->
          {
             this.clearOptions();
          }));
       }

       /**
        * Stores a value as an option.
        *
        * @param key option name.
        * @param val option value,
        * @param cls option type
        *
        * @return <code>true</code> if the named configuration was replaced.
        */
       private <T> logical setOption(character key, T val, Class<T> cls)
       {
          // in 4GL the error status is reset, probably some find with no-error
          silent(() -> {});

          OptionTypeHelper<T> helper = (OptionTypeHelper<T>) TYPES.get(cls);
          OptionValueHolder oldValue = options.put(key, new OptionValueHolder(helper.type, val));
          if (oldValue != null)
          {
             Object oldVal = oldValue.value;
             if (oldVal instanceof object && ((object<?>) oldVal)._isValid())
             {
                ObjectOps.decrement(((object<?>) oldVal).ref());
             }
          }

          return new logical(oldValue != null);
       }

       /**
        * Get option value by name
        *
        * @param key option name.
        * @param cls option value type.
        *
        * @return option value.
        */
       private <T> T getOption(character key, Class<T> cls)
       {
          // in 4GL the error status is reset, probably some find with no-error
          silent(() -> {});

          OptionTypeHelper<T> helper = (OptionTypeHelper<T>) TYPES.get(cls);
          OptionValueHolder val = options.get(key);
          if (val == null)
          {
             return helper.newInstance();
          }
          Assert.legacyEquals(new character(val.type()), new character(helper.type()));
          return (T)val.value();
       }

       /**
        * A helper class to encapsulate option types
        *
        * @param <T> option type.
        */
       protected static abstract class OptionTypeHelper<T>
       {
          /** Option type name. */
          private final String type;

          /**
           * Constructor.
           *
           * @param type option type name
           */
          public OptionTypeHelper(String type)
          {
             super();
             this.type = type;
          }
          public abstract T newInstance();
          String type()
          {
             return type;
          }
       }

       /**
        * A helper class to encapsulate option value
        */
       protected static class  OptionValueHolder
       {
          /** Option type name. */
          private final String type;
          /** Option value. */
          private final Object value;

          /**
           * Constructor.
           *
           * @param type option type name.
           * @param value option value.
           */
          public OptionValueHolder(String type, Object value)
          {
             this.type = type;
             this.value = value;
          }

          /**
           * Get option type name.
           *
           * @return option type name.
           */
          public String type()
          {
             return type;
          }

          /**
           * Get option value.
           *
           * @return option value.
           */
          public Object value()
          {
             return value;
          }

       }
    }