Array.java

/*
** Module   : Array.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.
**     RFB 20191022 Added LegacyResource annotation
** 002 CA  20191024 Added method support levels and updated the class support level.
** 003 ME  20200331 Added AutoExpand setter.
** 004 ME  20200529 Complete to match the 4GL implementation.
** 005 CA  20210113 Renamed Iiterator.next to Iiterator.next_, to follow NameConverter's rules.
**         20210113 Renamed clear() to clear_(), to follow NameConverter's rules.
** 006 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy signature.
**     ME  20210916 Fix wrong method call on set (increment instead of decrement). Clean-up resize
**                  and fix 'retainAll' method.
**     ME  20210929 Fix clone return value.
**     CA  20220923 Variable definitions (including associated with parameters) must be done always outside of 
**                  the BlockManager API
**     CA  20220513 Renamed _BaseObject_.clone() to legacyClone(), as it can be overriden by sub-classes and
**                  the access mode may collide with the Java's Object.clone().
**     GES 20220520 Upgraded table-handle parameter processing to match API changes.
** 007 CA  20230215 Sanitized the LegacySignature parameters, so their type match their Java method definition.
** 008 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.collections;

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.Assert;
import com.goldencode.p2j.oo.lang.*;
import com.goldencode.p2j.persist.*;
import com.goldencode.p2j.security.ContextLocal;

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

/**
 * Business logic (converted to Java from the 4GL source code
 * in OpenEdge/Core/Collections/Array.cls).
 */
@LegacyResource(resource = "OpenEdge.Core.Collections.Array")
@LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_STUB)
public class Array extends BaseObject implements Icollection
{
   private logical autoExpand = TypeFactory.logical();

   private logical discardOnShrink = TypeFactory.logical();

   private object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] value = TypeFactory
            .objectExtent(BaseObject.class);

   private static ContextLocal<integer> defaultArraySize = new ContextLocal<integer>()
   {
      protected integer initialValue()
      {
         return new integer(10);
      }
   };

   @LegacySignature(type = Type.EXECUTE)
   public void __core_collections_Array_execute__()
   {
      onBlockLevel(Condition.ERROR, Action.THROW);
   }

   @LegacySignature(returns = "INTEGER", type = Type.GETTER, name = "DefaultArraySize")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public static integer getDefautArraySize()
   {
      ObjectOps.load(Array.class);
      return function(Array.class, "DefaultArraySize", integer.class, new Block((Body) () -> {
         returnNormal(defaultArraySize.get());
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.GETTER, name = "AutoExpand")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical getAutoExpand()
   {
      return function(this, "AutoExpand", logical.class, new Block((Body) () -> {
         returnNormal(autoExpand);
      }));
   }

   /**
    * Set auto expand.
    * 
    * @param _autoExpand The auto expand flag
    */
   @LegacySignature(type = Type.SETTER, name = "AutoExpand", parameters = {
            @LegacyParameter(name = "autoexpand", type = "LOGICAL", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void setAutoExpand(logical _autoExpand)
   {
      logical autoExpand = TypeFactory.initInput(_autoExpand);
      internalProcedure(this, "AutoExpand", new Block((Body) () -> {
         this.autoExpand.assign(autoExpand);
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.GETTER, name = "DiscardOnShrink")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical getDiscardOnShrink()
   {
      return function(this, "DiscardOnShrink", logical.class, new Block((Body) () -> {
         returnNormal(discardOnShrink);
      }));
   }

   /**
    * Set discard on shrink.
    * 
    * @param _discardOnShrink The discard on shrink flag
    */
   @LegacySignature(type = Type.SETTER, name = "DiscardOnShrink", parameters = {
            @LegacyParameter(name = "discardOnShrink", type = "LOGICAL", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void setDiscardOnShrink(logical _discardOnShrink)
   {
      logical discardOnShrink = TypeFactory.initInput(_discardOnShrink);
      internalProcedure(this, "DiscardOnShrink", new Block((Body) () -> {
         this.discardOnShrink.assign(discardOnShrink);
      }));
   }

   @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(ArrayAssigner.lengthOf(value));
      }));
   }

   /**
    * Set size.
    * 
    * @param _newSize The new array size
    */
   @LegacySignature(type = Type.SETTER, name = "Size", parameters = {
            @LegacyParameter(name = "newSize", type = "INTEGER", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void setSize(integer _newSize)
   {
      integer newSize = TypeFactory.initInput(_newSize);
      object[][] tmpValue = { TypeFactory.objectExtent(BaseObject.class) };

      internalProcedure(this, "Size", new Block((Body) () -> {
         if (newSize.isUnknown())
         {
            if (!discardOnShrink.booleanValue())
            {
               undoThrowTopLevel(ObjectOps.newInstance(ResizeError.class, "II",
                        new character("Array"), new character("smaller")));
            }

            value = ArrayAssigner.resize(value, newSize);
         }
         else
         {
            integer initSize = getSize();

            if (!CompareOps._isEqual(initSize, newSize))
            {
               if (initSize.isUnknown())
               {
                  value = ArrayAssigner.resize(value, newSize);
               }
               else
               {
                  if (CompareOps._isLessThan(newSize, initSize) && !discardOnShrink.booleanValue())
                     undoThrowTopLevel(ObjectOps.newInstance(ResizeError.class, "II",
                              new character("Array"), new character("smaller")));
                  
                  tmpValue[0] = ArrayAssigner.resize(tmpValue[0], initSize.intValue());
                  tmpValue[0] = (object<? extends _BaseObject_>[]) ArrayAssigner.assignMulti(tmpValue[0], value);

                  value = ArrayAssigner.resize(value, new integer());
                  value = ArrayAssigner.resize(value, newSize);

                  for (int i = 0; i < initSize.intValue(); i++)
                  {
                     value[i] = tmpValue[0][i];
                  }
               }
            }
         }
      }));
   }

   @LegacySignature(returns = "OBJECT", qualified = "Progress.Lang.Object", type = Type.GETTER, name = "Value", extent = -1)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   protected object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] getValue()
   {
      return extentFunction(this, "Value", object.class, -1, new Block((Body) () -> {
         returnExtentNormal(value);
      }));
   }

   @LegacySignature(type = Type.CONSTRUCTOR)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public static void __core_collections_Array_constructor__static__()
   {
      externalProcedure(Array.class, new Block((Body) () -> {
         onBlockLevel(Condition.ERROR, Action.THROW);
      }));
   }

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

   @LegacySignature(type = Type.CONSTRUCTOR, parameters = {
            @LegacyParameter(name = "p1", type = "INTEGER", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void __core_collections_Array_constructor__(final integer _p1)
   {
      integer p1 = TypeFactory.initInput(_p1);

      internalProcedure(this, "__core_collections_Array_constructor__", new Block((Body) () -> {
         __lang_BaseObject_constructor__();

         setSize(p1);
         autoExpand.assign(false);
         discardOnShrink.assign(false);
      }));
   }

   @LegacySignature(type = Type.CONSTRUCTOR, parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", extent = -1, qualified = "progress.lang.object", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void __core_collections_Array_constructor__(
            final object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] _p1)
   {
      object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] p1 = TypeFactory.initInput(_p1);

      internalProcedure(this, "__core_collections_Array_constructor__", new Block((Body) () -> {
         __core_collections_Array_constructor__(ArrayAssigner.lengthOf(p1));
         addArray(p1);
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Add", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical add(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _p1)
   {
      return function(this, "Add", logical.class, new Block((Body) () -> {
         setValue(_p1);
         returnNormal(true);
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "AddAll", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "openedge.core.collections.icollection", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical addAll(
            final object<? extends com.goldencode.p2j.oo.core.collections.Icollection> _p1)
   {
      object<? extends com.goldencode.p2j.oo.core.collections.Icollection> p1 = TypeFactory
               .initInput(_p1);

      return function(this, "AddAll", logical.class, new Block((Body) () -> {
         returnNormal(addArray(p1.ref().toArray()));
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "AddArray", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", extent = -1, qualified = "progress.lang.object", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical addArray(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] _p1)
   {
      object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] p1 = TypeFactory.initInput(_p1);

      return function(this, "AddArray", logical.class, new Block((Body) () -> {
         // OE bug, no check if extent is unknown - size is set to unknown
         if (p1.length > 0) 
         {
            integer size = getSize();
            
            setSize(new integer(p1.length + size.getValue()));
   
            for (int i = 1; i <= p1.length; i++)
            {
               setValue(p1[i - 1], new integer(size.getValue() + i));
            }
         }
         returnNormal(true);
      }));
   }

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

   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Clone", qualified = "progress.lang.object")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public object<? extends com.goldencode.p2j.oo.lang._BaseObject_> legacyClone()
   {
      object<? extends Icollection> clone = TypeFactory.object(Icollection.class);
      
      return function(this, "Clone", object.class, new Block((Body) () -> {
         clone.assign(ObjectOps.newInstance(getClass()));

         cloneElements(clone);

         returnNormal(clone);
      }));
   }

   @LegacySignature(type = Type.METHOD, name = "CloneElements", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "openedge.core.collections.icollection", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   protected void cloneElements(
            final object<? extends com.goldencode.p2j.oo.core.collections.Icollection> _p1)
   {
      object<? extends com.goldencode.p2j.oo.core.collections.Icollection> p1 = TypeFactory
               .initInput(_p1);

      internalProcedure(this, "CloneElements", new Block((Body) () -> {

         Iiterator it = iterator().ref();
         while (it.hasNext().booleanValue())
         {
            p1.ref().add(it.next_().ref().legacyClone());
         }
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Contains", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical contains(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _p1)
   {
      object<? extends com.goldencode.p2j.oo.lang._BaseObject_> p1 = TypeFactory.initInput(_p1);

      return function(this, "Contains", logical.class, new Block((Body) () -> {
         for (int i = 1; i <= value.length; i++)
         {
            if (value[i].ref().legacyEquals(p1).booleanValue())
            {
               returnNormal(true);
            }
         }

         returnNormal(false);
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "ContainsAll", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "openedge.core.collections.icollection", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical containsAll(
            final object<? extends com.goldencode.p2j.oo.core.collections.Icollection> _p1)
   {
      object<? extends com.goldencode.p2j.oo.core.collections.Icollection> p1 = TypeFactory
               .initInput(_p1);

      return function(this, "ContainsAll", logical.class, new Block((Body) () -> {

         Assert.notNull(p1, new character("Check collection"));

         if (!p1.ref().isEmpty().booleanValue())
         {
            if (p1.ref().getSize().intValue() > value.length)
               returnNormal(false);

            object<? extends Iiterator> iterator = p1.ref().iterator();

            while (iterator.ref().hasNext().booleanValue())
            {
               if (!contains(iterator.ref().next_()).booleanValue())
               {
                  returnNormal(false);
               }
            }
         }

         returnNormal(true);
      }));
   }

   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "GetValue", qualified = "progress.lang.object", parameters = {
            @LegacyParameter(name = "p1", type = "INTEGER", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public object<? extends com.goldencode.p2j.oo.lang._BaseObject_> getValue(final integer _p1)
   {
      integer p1 = TypeFactory.initInput(_p1);

      return function(this, "GetValue", object.class, new Block((Body) () -> {

         Assert.notNullOrZero(p1, new character("Extent"));

         returnNormal(ArrayAssigner.subscript(this.value, p1));
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "IsEmpty")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical isEmpty()
   {
      return function(this, "IsEmpty", logical.class, new Block((Body) () -> {
         returnNormal(value.length == 0);
      }));
   }

   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "Iterator", qualified = "openedge.core.collections.iiterator")
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public object<? extends com.goldencode.p2j.oo.core.collections.Iiterator> iterator()
   {
      return function(this, "Iterator", object.class, new Block((Body) () -> {
         returnNormal(ObjectOps.newInstance(ArrayIterator.class, "I", new object(this)));
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Remove", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical remove(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _p1)
   {
      object<? extends com.goldencode.p2j.oo.lang._BaseObject_> p1 = TypeFactory.initInput(_p1);

      return function(this, "Remove", logical.class, new Block((Body) () -> {

         for (int i = 1; i <= value.length; i++)
         {
            object<? extends _BaseObject_> obj = ArrayAssigner.subscript(this.value, i);
            if (obj.ref().legacyEquals(p1).booleanValue())
            {
               // decrement reference to object
               ObjectOps.decrement(obj.ref());

               // set it to invalid
               ArrayAssigner.assignSingle(this.value, i, new object<>());
               returnNormal(new logical(true));
            }
         }

         returnNormal(new logical(false));
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "RemoveAll", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "openedge.core.collections.icollection", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical removeAll(
            final object<? extends com.goldencode.p2j.oo.core.collections.Icollection> _p1)
   {
      object<? extends com.goldencode.p2j.oo.core.collections.Icollection> p1 = TypeFactory
               .initInput(_p1);

      return function(this, "RemoveAll", logical.class, new Block((Body) () -> {
         boolean lAny = false;

         object<? extends Iiterator> iterator = p1.ref().iterator();

         while (iterator.ref().hasNext().booleanValue())
         {
            object<? extends _BaseObject_> o = iterator.ref().next_();
            while (remove(o).booleanValue())
               lAny = true;
         }

         returnNormal(lAny);
      }));
   }

   @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "RetainAll", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "openedge.core.collections.icollection", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public logical retainAll(
            final object<? extends com.goldencode.p2j.oo.core.collections.Icollection> _p1)
   {
      object<? extends com.goldencode.p2j.oo.core.collections.Icollection> p1 = TypeFactory
               .initInput(_p1);

      return function(this, "RetainAll", logical.class, new Block((Body) () -> {
         boolean lAny = false;

         object<? extends Iiterator> iterator = iterator();

         while (iterator.ref().hasNext().booleanValue())
         {
            object<? extends _BaseObject_> o = iterator.ref().next_();
            if (!p1.ref().contains(o).booleanValue())
            {
               while (remove(o).booleanValue())
                  lAny = true;
            }
         }

         returnNormal(lAny);
      }));
   }

   @LegacySignature(type = Type.METHOD, name = "SetValue", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void setValue(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _p1)
   {
      internalProcedure(this, "SetValue", new Block((Body) () -> {
         setValue(_p1, getSize().isUnknown() ? getSize() : new integer(MathOps.plus(getSize(), 1)));
      }));
   }

   @LegacySignature(type = Type.METHOD, name = "SetValue", parameters = {
            @LegacyParameter(name = "p1", type = "OBJECT", qualified = "progress.lang.object", mode = "INPUT"),
            @LegacyParameter(name = "p2", type = "INTEGER", mode = "INPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public void setValue(final object<? extends com.goldencode.p2j.oo.lang._BaseObject_> _p1,
            final integer _p2)
   {
      object<? extends com.goldencode.p2j.oo.lang._BaseObject_> p1 = TypeFactory.initInput(_p1);
      integer p2 = TypeFactory.initInput(_p2);

      internalProcedure(this, "SetValue", new Block((Body) () -> {

         if (CompareOps._isGreaterThan(p2, getSize()))
         {
            if (autoExpand.booleanValue())
            {
               setSize(new integer(MathOps.plus(p2, MathOps.round(MathOps.multiply(getSize(), 0.5), 0))));
            }
            else
            {
               undoThrowTopLevel(ObjectOps.newInstance(ResizeError.class, "II",
                        new character("Array"), new character("larger")));
            }

         }

         // decrement reference to previous object if any
         object<? extends _BaseObject_> obj = ArrayAssigner.subscript(value, p2);
         
         if (!obj.isUnknown())
            ObjectOps.decrement(obj.ref());

         ArrayAssigner.assignSingle(value, p2, p1);

         // increment reference to object
         if (!p1.isUnknown())
            ObjectOps.increment(p1.ref());
      }));
   }

   @LegacySignature(returns = "OBJECT", type = Type.METHOD, name = "ToArray", qualified = "progress.lang.object", extent = -1)
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_FULL)
   public object<? extends com.goldencode.p2j.oo.lang._BaseObject_>[] toArray()
   {
      return extentFunction(this, "ToArray", object.class, -1, new Block((Body) () -> {
         returnExtentNormal(value);
      }));
   }

   @LegacySignature(type = Type.METHOD, name = "ToTable", parameters = {
            @LegacyParameter(name = "p1", type = "HANDLE", mode = "OUTPUT") })
   @LegacyResourceSupport(supportLvl = CVT_LVL_FULL | RT_LVL_STUB)
   public void toTable(final OutputTableHandle _p1)
   {
      handle p1 = new handle();

      // TODO: convert array to temp table
      internalProcedure(this, "ToTable", new Block((Init) () ->
      {
         TemporaryBuffer.createDynamicTable(_p1, p1);
      }));
   }
}