# BuilderRegistry.java

    /*
    ** Module   : BuilderRegistry.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  20200930 Fix and add new methods as per OE12.2.
    **     CA  20210113 Renamed clear() to clear_(), to follow NameConverter's rules.
    **     CA  20210221 Added 'extent' for getKeys method.
    ** 005 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy signature.
    **     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.
    ** 006 CA  20231113 The 'execute' method must be annotated with LegacySignature Type.Execute, and also can be
    **                  dropped if is a no-op.
    */

    /*
    ** 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 com.goldencode.p2j.util.*;
    import com.goldencode.p2j.util.BlockManager.Action;
    import com.goldencode.p2j.util.BlockManager.Condition;
    import com.goldencode.p2j.oo.core.*;
    import com.goldencode.p2j.oo.lang.*;

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

    import java.util.*;
    import java.util.stream.*;

    /**
     * General registry for name/Progress.Lang.Class pairs used
     * by the various builders.
     */
    @LegacyResource(resource = "OpenEdge.Core.Util.BuilderRegistry")
    @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
    public class BuilderRegistry
    extends BaseObject
    {
       private static final String CORE_UTIL_BUILDER_REGISTRY = "openedge.core.util.builderregistry";

       /** Registry data - case insensitive map */
       protected final Map<String, LegacyClass> registry = new TreeMap<String, LegacyClass>(String.CASE_INSENSITIVE_ORDER);

       /** Value type */
       protected object<? extends LegacyClass> valueType = UndoableFactory.object(LegacyClass.class);

       /**
        * Execute method.
        */
       @LegacySignature(type = Type.EXECUTE)
       public void __core_util_BuilderRegistry_execute__()
       {
          onBlockLevel(Condition.ERROR, Action.THROW);
       }

       /**
        * Get registry size.
        *
        * @return registry size.
        */
       @LegacySignature(returns = "INTEGER", type = Type.GETTER, name = "Size")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public integer getSize()
       {
          return function(this, "Size", integer.class, new Block((Body) () ->
          {
             returnNormal(new integer(registry.size()));
          }));
       }

       /**
        * Get value type.
        *
        * @return value type.
        */
       @LegacySignature(returns = "OBJECT", qualified = "Progress.Lang.Class", type = Type.GETTER, name = "ValueType")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public object<? extends LegacyClass> getValueType()
       {
          return function(this, "ValueType", object.class, new Block((Body) () ->
          {
             returnNormal(valueType);
          }));
       }

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

       /**
        * Constructor.
        *
        * @param poRegistry registry
        */
       @LegacySignature(type = Type.CONSTRUCTOR, parameters =
       {
          @LegacyParameter(name = "poRegistry", type = "OBJECT",
                qualified = CORE_UTIL_BUILDER_REGISTRY, mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public void __core_util_BuilderRegistry_constructor___1(
             final object<? extends BuilderRegistry> poRegistry)
       {
          internalProcedure(this, "__core_util_BuilderRegistry_constructor__", new Block((Body) () ->
          {
             assertNotNull(poRegistry, "Source registry");
             __lang_BaseObject_constructor__();
             this.valueType.assign(poRegistry.ref().getValueType());
             putAll(poRegistry);
          }));
       }

       /**
        * Constructor
        *
        * @param _poTypeKey value type.
        */
       @LegacySignature(type = Type.CONSTRUCTOR, parameters =
       {
          @LegacyParameter(name = "poTypeKey", type = "OBJECT",
                qualified = "progress.lang.class", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public void __core_util_BuilderRegistry_constructor__(
             final object<? extends LegacyClass> _poTypeKey)
       {
          object<? extends LegacyClass> poTypeKey = TypeFactory.initInput(_poTypeKey);

          internalProcedure(this, "__core_util_BuilderRegistry_constructor__", new Block((Body) () ->
          {
             assertNotNull(poTypeKey, "Value type");
             __lang_BaseObject_constructor__();
             this.valueType.assign(poTypeKey);
          }));
       }

       /**
        * Clear registry
        */
       @LegacySignature(type = Type.METHOD, name = "Clear")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public void clear_()
       {
          internalProcedure(this, "Clear", new Block((Body)() -> {
             registry.clear();
          }));
       }

       /**
        * Get value by key
        *
        * @param _pcTypeKey key
        *
        * @return value
        */

       @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Get", qualified = "progress.lang.class", parameters =
       {
          @LegacyParameter(name = "pcTypeKey", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public object<? extends LegacyClass> get(final character _pcTypeKey)
       {
          character pcTypeKey = TypeFactory.initInput(_pcTypeKey);

          return function(this, "Get", object.class, new Block((Body)() -> {
             assertNotNull(pcTypeKey, "Type key");
             returnNormal(_get(pcTypeKey.getValue()));
          }));
       }

       @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Get", qualified = "progress.lang.class", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "OBJECT", qualified = "progress.lang.class", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public object<? extends com.goldencode.p2j.oo.lang.LegacyClass> get_2(final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _pTypeKey)
       {
          object<? extends com.goldencode.p2j.oo.lang.LegacyClass> pTypeKey = TypeFactory.initInput(_pTypeKey);

          return function(BuilderRegistry.class, this, "Get", object.class, new Block((Body) () -> {
             returnNormal(pTypeKey._isValid() ? get(pTypeKey.ref().getTypeName()) : new object());
          }));
       }

       @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Get", qualified = "progress.lang.class", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "OBJECT", qualified = "progress.lang.enum", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public object<? extends com.goldencode.p2j.oo.lang.LegacyClass> get_3(final object<? extends com.goldencode.p2j.oo.lang.LegacyEnum> _pTypeKey)
       {
          object<? extends com.goldencode.p2j.oo.lang.LegacyEnum> pTypeKey = TypeFactory.initInput(_pTypeKey);

          return function(BuilderRegistry.class, this, "Get", object.class, new Block((Body) () -> {
             returnNormal(pTypeKey._isValid() ? get(pTypeKey.ref().toLegacyString()) : new object());
          }));
       }

       public object<? extends LegacyClass> _get(final String key)
       {
          LegacyClass cls = key == null ? null : registry.get(key);

          return cls == null ? new object() : new object(cls);
       }

       /**
        * Get keys.
        *
        * @return keys.
        */
       @LegacySignature(returns = "CHARACTER", type = Type.METHOD, extent = -1, name = "GetKeys")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public character[] getKeys()
       {
          return extentFunction(this, "GetKeys", character.class, new Block((Body)() -> {
             List<character> klist = registry.keySet().stream().
                      map(character::new).collect(Collectors.toList());
             character[] rkeys = klist.toArray(new character[klist.size()]);
             returnExtentNormal(rkeys);
          }));
       }

       /**
        * Check if key exists
        *
        * @param _pcTypeKey key
        *
        * @return <code>true</code> if key exists
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Has", parameters = {
                @LegacyParameter(name = "pcTypeKey", type = "CHARACTER", mode = "INPUT") })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
       public logical has(final character _pcTypeKey)
       {
          character pcTypeKey = TypeFactory.initInput(_pcTypeKey);

          return function(this, "Has", logical.class, new Block((Body) () -> {
             assertNotNull(pcTypeKey, "Type key");

             returnNormal(new logical(registry.containsKey(pcTypeKey.getValue())));
          }));
       }

       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Has", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "OBJECT", qualified = "progress.lang.class", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical has(final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _pTypeKey)
       {
          return function(BuilderRegistry.class, this, "Has", logical.class, new Block((Body) () -> {
             assertNotNull(_pTypeKey, "Type key");

             returnNormal(has(_pTypeKey.ref().getTypeName()));
          }));
       }

       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Has", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "OBJECT", qualified = "progress.lang.enum", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical has_1(final object<? extends com.goldencode.p2j.oo.lang.LegacyEnum> _pTypeKey)
       {
          return function(BuilderRegistry.class, this, "Has", logical.class, new Block((Body) () -> {
             assertNotNull(_pTypeKey, "Type key");

             returnNormal(has(_pTypeKey.ref().toLegacyString()));
          }));
       }

       /**
        * Put key/value.
        *
        * @param _pcTypeKey key.
        * @param _poType value.
        *
        * @return <code>true</code> if value was replaced.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Put", parameters =
       {
          @LegacyParameter(name = "pcTypeKey", type = "CHARACTER", mode = "INPUT"),
          @LegacyParameter(name = "poType", type = "OBJECT",
             qualified = "progress.lang.class", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical put(final character _pcTypeKey, final object<? extends LegacyClass> _poType)
       {
          character pcTypeKey = TypeFactory.initInput(_pcTypeKey);
          object<? extends LegacyClass> poType = TypeFactory.initInput(_poType);

          return function(this, "Put", logical.class, new Block((Body) () -> {
             Assert.isType(poType, valueType);
             assertNotNull(pcTypeKey, "Type key");

             returnNormal(new logical(_put(pcTypeKey.getValue(), poType)));
           }));
       }

       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Put", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "CHARACTER", mode = "INPUT"),
                      @LegacyParameter(name = "pType", type = "CHARACTER", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical put(final character _pTypeKey, final character _pType)
       {
          return function(BuilderRegistry.class, this, "Put", logical.class, new Block((Body) () -> {
             Class<? extends _BaseObject_> typeCls = _pType.isUnknown() ? null : ObjectOps.resolveClass(_pType.getValue());

             if (typeCls == null)
                returnNormal(put(_pTypeKey, new ObjectVar(LegacyClass.class)));

             returnNormal(put(_pTypeKey, ObjectOps.getLegacyClass(typeCls)));

          }));
       }

       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Put", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "OBJECT", qualified = "progress.lang.class", mode = "INPUT"),
                      @LegacyParameter(name = "pType", type = "OBJECT", qualified = "progress.lang.class", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical put_2(final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _pTypeKey, final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _pType)
       {
          return function(BuilderRegistry.class, this, "Put", logical.class, new Block((Body) () -> {
             assertNotNull(_pTypeKey, "Type key");

             returnNormal(put(_pTypeKey.ref().getTypeName(), _pType));
          }));
       }

       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Put", parameters =
             {
                      @LegacyParameter(name = "pTypeKey", type = "OBJECT", qualified = "progress.lang.enum", mode = "INPUT"),
                      @LegacyParameter(name = "pType", type = "OBJECT", qualified = "progress.lang.class", mode = "INPUT")
             })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical put_3(final object<? extends com.goldencode.p2j.oo.lang.LegacyEnum> _pTypeKey, final object<? extends com.goldencode.p2j.oo.lang.LegacyClass> _pType)
       {
          return function(BuilderRegistry.class, this, "Put", logical.class, new Block((Body) () -> {
             assertNotNull(_pTypeKey, "Type key");

             returnNormal(put(_pTypeKey.ref().toLegacyString(), _pType));
          }));
       }

       public boolean _put(final String pcTypeKey, final object<? extends LegacyClass> poType)
       {
          return poType._isValid() && registry.put(pcTypeKey, poType.ref()) != null;
       }

       /**
        * Put all keys/values from another registry.
        *
        * @param poRegistry registry.
        */
       @LegacySignature(type = Type.METHOD, name = "PutAll", parameters =
       {
          @LegacyParameter(name = "poRegistry", type = "OBJECT",
                qualified = CORE_UTIL_BUILDER_REGISTRY, mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public void putAll(final object<? extends BuilderRegistry> poRegistry)
       {
          internalProcedure(this, "PutAll", new Block((Body)() ->  {
             assertNotNull(poRegistry, "Source registry");
             Assert.isType(poRegistry.ref().valueType, this.valueType);
             this.registry.putAll(poRegistry.ref().registry);
          }));
       }

       /**
        * Remove value by key.
        *
        * @param _pcTypeKey key.
        *
        * @return <code>true</code> if value was removed.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Remove", parameters =
       {
          @LegacyParameter(name = "pcTypeKey", type = "CHARACTER", mode = "INPUT")
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       public logical remove(final character _pcTypeKey)
       {
          character pcTypeKey = TypeFactory.initInput(_pcTypeKey);

          return function(this, "Remove", logical.class, new Block((Body) () -> {
             assertNotNull(pcTypeKey, "Type key");
             returnNormal(new logical(registry.remove(pcTypeKey.getValue()) != null));
           }));
       }

    }