# BaseObject.java

    /*
    ** Module   : BaseObject.java
    ** Abstract : Base class for all OO 4GL classes (replaces Progress.Lang.Object).
    **
    ** Copyright (c) 2018-2024, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --------------------------------------Description---------------------------------------
    ** 001 GES 20181208 First version.
    ** 002 CA  20190219 Runtime implementation.
    ** 003 CA  20190423 4GL's Progress.Lang.Object:equals and toString emits as legacyEquals and
    **                  legacyToString, to not collide with Java's Object.equals and toString.
    ** 004 CA  20190509 Always use <? extends _BaseObject_>.
    ** 005 CA  20190720 Emit the 'this' reference for the BlockManager APIs.
    ** 006 CA  20191023 Fixed the LegacySignature.name for legacyEquals method.
    **     GES 20191024 Added method support levels and updated the class support level.
    ** 007 CA  20200330 Fixed legacy ToString method.
    **     GES 20200520 Minor NPE protection added.
    **     GES 20200603 Moved class registration into a static initializer.
    ** 008 ME  20210115 Override equals using legacyEquals and hashCode to use resource id (if possible).
    **         20210209 Revert changes, removed hashCode/equals method.
    ** 009 CA  20210221 Fixed 'qualified', 'extent' and 'returns' annotations at the legacy
    **                  signature.
    **     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().
    ** 010 ME  20230829 NextSibling/PrevSibling returns unknown for internal/untracked instances.
    ** 011 CA  20231113 The 'execute' method must be annotated with LegacySignature Type.Execute, and also can be
    **                  dropped if is a no-op.
    ** 012 CA  20240324 Added a flag to allow tracking the objects valid state without a map lookup.  This flag is
    **                  set to false when the instance gets deleted.
    ** 013 CA  20240918 Removed default methods from interfaces and moved them as concrete implementations, as
    **                  default methods cause the Java metaspace to spike one order of magnitude.
    */

    /*
    ** 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.lang;

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

    import com.goldencode.p2j.oo.core.*;
    import com.goldencode.p2j.util.*;
    import com.goldencode.p2j.util.InternalEntry.Type;

    /**
     * Base class for all OO 4GL classes (replaces Progress.Lang.Object). Any change made to the
     * public API of this case must also be present in {@code _BaseObject_}.
     */
    @LegacyResource(resource = "Progress.Lang.Object")
    @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
    public class BaseObject
    implements _BaseObject_
    {
       static
       {
          ObjectOps.registerClass("Progress.Lang.Object", BaseObject.class);
       }

       /** Flag indicating if this instance is valid or was deleted. */
       private boolean valid = false;

       /**
        * Implicit, no parameter, method associated with the implicit legacy constructor for the
        * <code>Progress.Lang.Object</code> class.
        */
       @LegacySignature(type = Type.CONSTRUCTOR)
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public void __lang_BaseObject_constructor__()
       {
          internalProcedure(this, "__lang_BaseObject_constructor__", new Block((Body) () ->
          {
             // no-op
          }));
       }

       /**
        * Reports if the given instance is managed as part of the legacy 4GL object life cycle. This
        * includes reference counting, linking in the SESSION object chain, managed construction and
        * destruction.
        *
        * @return   Always {@code true}.
        */
       @Override
       public boolean isTracked()
       {
          return true;
       }

       /**
        * Get the object's valid state (if it was deleted or not).
        *
        * @return   See above.
        */
       @Override
       public final boolean __isValidInternal__()
       {
          return valid;
       }

       /**
        * Set the object's valid state (if it was deleted or not).
        *
        * @param    valid
        *           Flag indicating if this instance is valid or not.
        */
       @Override
       public final void __setValidInternal__(boolean valid)
       {
          this.valid = valid;
       }

       /**
        * Obtains the next object instance in the linked list of all existing objects.
        *
        * @return   The next object instance.
        */
       @LegacySignature(returns = "OBJECT", qualified = "Progress.Lang.Object", type = Type.GETTER, name = "next-sibling")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public object<? extends _BaseObject_> getNextSibling()
       {
          if (!isTracked())
          {
             return new object<>();
          }

          ObjectResource ores = ObjectOps.asResource(this);
          handle hres = ores.getNextSibling();
          return hres._isValid() ? new object<>((ObjectResource) hres.get())
                                 : new object<>();
       }

       /**
        * Obtains the previous object instance in the linked list of all existing objects.
        *
        * @return   The previous object instance.
        */
       @LegacySignature(returns = "OBJECT", qualified = "Progress.Lang.Object", type = Type.GETTER, name = "prev-sibling")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public object<? extends _BaseObject_> getPrevSibling()
       {
          if (!isTracked())
          {
             return new object<>();
          }

          ObjectResource ores = ObjectOps.asResource(this);
          handle hres = ores.getPrevSibling();
          return hres._isValid() ? new object<>((ObjectResource) hres.get())
                                 : new object<>();
       }

       /**
        * Obtains the class of this object instance.
        *
        * @return   The object's class.
        */
       @LegacySignature(returns = "OBJECT", qualified = "Progress.Lang.Class", type = Type.METHOD, name = "GetClass")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public object<? extends LegacyClass> getLegacyClass()
       {
          return new object<>(ObjectOps.getClassInstance(this.getClass()));
       }

       /**
        * Check if this reference matches the given one.
        *
        * @param    other
        *           The other reference.
        *
        * @return   {@code true} if the other reference is not unknown value and if the reference
        *           is the same as this object's reference.
        */
       @LegacySignature(returns = "LOGICAL", type = Type.METHOD, name = "Equals", parameters =
       {
          @LegacyParameter(name = "other", type = "OBJECT", mode = "INPUT", qualified="Progress.Lang.Object"),
       })
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public logical legacyEquals(object<? extends _BaseObject_> other)
       {
          return new logical(other != null && !other.isUnknown() && other.ref() == this);
       }

       /**
        * Get the string representation for this instance.
        * <p>
        * Although the original method name is {@code toString()}, this version returns a
        * {@code character} type which would conflict with the {@code java.lang.Object.toString()}
        * version.  For this reason, the name is mapped differently during conversion.
        *
        * @return   The type's name followed by underscore and its resource ID.
        */
       @LegacySignature(returns = "CHARACTER", type = Type.METHOD, name = "ToString")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public character toLegacyString()
       {
          object<? extends LegacyClass> ocls = getLegacyClass();
          LegacyClass cls = ocls.ref();
          ocls.setUnknown();
          character name = cls.getTypeName();
          // we can't override Object.toString(), it won't compile
          return new character(name.toStringMessage() + "_" + object.resourceId(new object<>(this)));
       }

       /**
        * No-op implementation for clone.
        *
        * @return   An unknown object.
        */
       @LegacySignature(returns = "OBJECT", qualified = "Progress.Lang.Object", type = Type.METHOD, name = "Clone")
       @LegacyResourceSupport(supportLvl = CVT_LVL_FULL|RT_LVL_FULL)
       @Override
       public object<? extends _BaseObject_> legacyClone()
       {
          ErrorManager.recordOrThrowError(13444, "Clone method is undefined for the class",
                                         false, false);
          return new object<>();
       }

       protected static void assertNotNull(object<? extends _BaseObject_> obj, String name)
       {
          Assert.notNull(obj, new character(name));
       }

       protected static void assertNotNull(character str, String name)
       {
          Assert.notNull(str, new character(name));
       }
    }