# RamNode.java

    /*
    ** Module   :RamNode.java
    ** Abstract :RamRemapper node.
    **
    ** Copyright (c) 2005-2025, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ----------------Description-----------------
    ** 001 SIY 20050222  @20050  Created initial version
    ** 002 SIY 20050308  @20372  Organized imports: removed redundant and
    **                           added explicit class names.
    ** 003 SIY 20050328  @20590  Added setName() method. Fixed comments.
    ** 004 SIY 20050406  @21024  Renamed class from XmlNode to RamNode
    **                           during refactoring of the XmlRemapper.
    **                           Added a number of convenience methods.
    ** 005 SIY 20050501  @21199  Extracted common functionality into DirNode,
    **                           updated documentation.
    ** 006 NVS 20060426  @25726  Added protection against wrong class names
    ** 007 SIY 20090712  @43137  Some code cleanup.
    ** 008 SP  20250416          LinkedHashMap was replaced with CaseInsensitiveLinkedHashMap in attributes
    **                           and child nodes storage. Removed toLowerCase() on node names.
    */

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

    /**
     * This class is used to hold node data in the memory. It extends and, where
     * appropriate, overrides <code>DirNode</code> methods and this does allow
     * to build entire tree in memory using <code>RamNode</code> instances.
     *
     * @author  SIY
     * @version 1.0
     */
    class RamNode
    extends DirNode
    {
       /** Reference to real node (for use by node substitution techniques) */
       private RamNode realNode = null;

       /**
        * Constructs an empty node of given class and name. Note that constructed
        * node may be not valid until all mandatory attributes are filled.
        *
        * @param   nodeClass
        *          Object Class of the created node.
        * @param   name
        *          Node name.
        * @param   realNode
        *          Reference to real node for which this node is a substitution.
        */
       RamNode(ObjectClass nodeClass, String name, RamNode realNode)
       {
          super(nodeClass, name);

          this.realNode = realNode;

          if (nodeClass == null)
          {
             throw new RuntimeException("invalid directory class for " + name +
                                        " node " + realNode);
          }
       }

       /**
        * Constructs an empty node of given class and name. Note that constructed
        * node may be not valid until all mandatory attributes are filled.
        *
        * @param   nodeClass
        *          Object Class of the created node.
        * @param   name
        *          Node name.
        */
       RamNode(ObjectClass nodeClass, String name)
       {
          super(nodeClass, name);

          if (nodeClass == null)
          {
             throw new RuntimeException("invalid directory class for " + name);
          }
       }

       /**
        * Add a new child node if this is allowed.
        *
        * @param   child
        *          A node which will be added as child.
        *
        * @return  <code>true</code> if operation was successful.
        */
       boolean addChild(RamNode child)
       {
          if (child == null)
          {
             return false;
          }
          if (nodeClass.isClassLeaf())
          {
             return false;
          }
          if (getChild(child.getName()) != null) //Node exists
          {
             return false;
          }
          childList.put(child.getName(), child);
          return true;
       }

       /**
        * Get a child node if node with such a name exists.
        *
        * @param   child
        *          Name of the child to return.
        *
        * @return  Reference to the child node or <code>null</code> if there is
        *          no such node present.
        */
       RamNode getChild(String child)
       {
          if (child == null)
          {
             return null;
          }
          return (RamNode) childList.get(child);
       }

       /**
        * Get a list of all child nodes.
        *
        * @return  An array of all child nodes.
        */
       RamNode[] getChildList()
       {
          return childList.values().toArray(new RamNode[childList.size()]);
       }

       /**
        * Return a reference to node for which this node was created as a
        * replacement.
        *
        * @return  Reference to real node.
        */
       RamNode getRealNode()
       {
          return realNode;
       }

       /**
        * {@inheritDoc}
        */
       String[] getChildNames()
       {
          RamNode[] list = getChildList();
          if (list == null)
          {
             return null;
          }
          String[] res = new String[list.length];
          for (int i = 0; i < res.length; i++)
          {
             res[i] = new String(list[i].getName());
          }
          return res;
       }
    }