# FlowNode.java

    /*
    ** Module   : FlowNode.java
    ** Abstract : Encodes a single node in the flow chart.
    **
    ** Copyright (c) 2018, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- ---------------------------------Description----------------------------------
    ** 001 CA  20181003 First version.
    */

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

    import java.util.*;

    /**
     * Defines a node used by the flow chart.
     */
    public class FlowNode
    {
       /** The node's ID. */
       private int id;

       /** The node's type.  For AST nodes, this is the AST token text. */
       private String type;

       /** The node's displayed text. */
       private String text;

       /**
        * The depth of this node.  This is used in terms of top-to-bottom flow: if the code is branching
        * from an e.g. IF statement, the THEN/ELSE branches will start from the same depth, but the
        * next statement will continue with the maximum depth from these branches.
        */
       private int depth;

       /** When multiple nodes have the same depth, this is used to order them. */
       private int order;

       /**
        * For nodes within the same block (group), this ID is used to distinguish nodes with the same
        * depth (in cases where the code is branching for THEN/ELSE).  The parent ID will be set
        * to the statement which caused the branching (IF).
        */
       private Integer parentId = null;

       /**
        * Specify the group ID for this node: all nodes within the same block will have the same I
        * group ID.  Nodes defined directly in the top-level block will have no group assigned.
        */
       private Integer groupId = null;

       /**
        * Marks special nodes which are part of the loop flow (condition for WHILE/TO, assignment and
        * increment expression for TO).
        */
       private boolean loop;

       /**
        * Used only internally by the flow chart processing - defines the nodes which are executed,
        * within this block (or branch).  Used to link to them to the next executed statement, after
        * this block or branch.
        */
       private Set<FlowNode> exits;

       /**
        * Create an instance.
        * @param    id
        *           The node's ID.
        * @param    type
        *           The node's type.
        */
       public FlowNode(int id, String type)
       {
          this.id = id;
          this.type = type;
          this.text = type;
       }

       /**
        * Get the node's {@link #id}.
        *
        * @return   See above.
        */
       public int getId()
       {
          return id;
       }

       /**
        * Set the node's {@link #id}.
        *
        * @param    id
        *           The node's id.
        */
       public void setId(int id)
       {
          this.id = id;
       }

       /**
        * Get the node's {@link #type}.
        *
        * @return   See above.
        */
       public String getType()
       {
          return type;
       }

       /**
        * Set the node's {@link #type}.
        *
        * @param    type
        *           The node's type.
        */
       public void setType(String type)
       {
          this.type = type;
       }

       /**
        * Get the node's {@link #text}.
        *
        * @return   See above.
        */
       public String getText()
       {
          return text;
       }

       /**
        * Set the node's {@link #text}.
        *
        * @param    text
        *           The node's text.
        */
       public void setText(String text)
       {
          this.text = text;
       }

       /**
        * Get the node's {@link #depth}.
        *
        * @return   See above.
        */
       public int getDepth()
       {
          return depth;
       }

       /**
        * Set the node's {@link #depth}.
        *
        * @param    depth
        *           The node's depth.
        */
       public void setDepth(int depth)
       {
          this.depth = depth;
       }

       /**
        * Get the node's {@link #order}.
        *
        * @return   See above.
        */
       public int getOrder()
       {
          return order;
       }

       /**
        * Set the node's {@link #order}.
        *
        * @param    order
        *           The node's order.
        */
       public void setOrder(int order)
       {
          this.order = order;
       }

       /**
        * Get the node's {@link #parentId}.   May be <code>null</code>.
        *
        * @return   See above.
        */
       public Integer getParentId()
       {
          return parentId;
       }

       /**
        * Set the node's {@link #parentId}.
        *
        * @param   parentId
        *          The parent ID.
        */
       public void setParentId(Integer parentId)
       {
          this.parentId = parentId;
       }

       /**
        * Get the node's {@link #groupId}.   May be <code>null</code>.
        *
        * @return   See above.
        */
       public Integer getGroupId()
       {
          return groupId;
       }

       /**
        * Set the node's {@link #groupId}.
        *
        * @param   groupId
        *          The group ID.
        */
       public void setGroupId(Integer groupId)
       {
          this.groupId = groupId;
       }

       /**
        * Get the {@link #loop} flag.  Marks nodes associated with a loop execution.
        *
        * @return   <code>true</code> if this node is associated with a loop execution.
        */
       public boolean isLoop()
       {
          return loop;
       }

       /**
        * Set the {@link #loop} flag.  Marks nodes associated with a loop execution.
        *
        * @param    loop
        *           The loop state.
        */
       public void setLoop(boolean loop)
       {
          this.loop = loop;
       }

       /**
        * Get all the exits node (nodes associated with node(s) which will be reached once this block
        * has finished).  This may contain END/EXIT nodes, or statement nodes.
        *
        * @return   See above.
        */
       public Set<FlowNode> exits()
       {
          return exits == null ? Collections.emptySet() : Collections.unmodifiableSet(exits);
       }

       /**
        * Add a node as an exit for this node.
        *
        * @param    node
        *           The exit node.
        */
       public void addExit(FlowNode node)
       {
          if (exits == null)
          {
             exits = new HashSet<>();
          }

          exits.add(node);
       }

       /**
        * Add one or more nodes as exits for this node.
        *
        * @param    exits
        *           The exit nodes.
        */
       public void addExits(Set<FlowNode> exits)
       {
          if (this.exits == null)
          {
             this.exits = new HashSet<>();
          }

          this.exits.addAll(exits);
       }

       /**
        * Check if this instance is the same as the other one (if {@link #id} is the same).
        *
        * @param    obj
        *           The instance to compare.
        *
        * @return   See above.
        */
       @Override
       public boolean equals(Object obj)
       {
          if (!(obj instanceof FlowNode))
          {
             return false;
          }

          FlowNode node = (FlowNode) obj;

          return this.id == node.id;
       }

       /**
        * Compute the unique hashcode for this instance, using the {@link #id}.
        *
        * @return   See above.
        */
       @Override
       public int hashCode()
       {
          return Integer.hashCode(id);
       }
    }