Class GraphComponent

  1  
  2 /*
  3  * Copyright (c) 2016 Vivid Solutions.
  4  *
  5  * All rights reserved. This program and the accompanying materials
  6  * are made available under the terms of the Eclipse Public License 2.0
  7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  9  * and the Eclipse Distribution License is available at
 10  *
 11  * http://www.eclipse.org/org/documents/edl-v10.php.
 12  */
 13 package org.locationtech.jts.planargraph;
 14  
 15 import java.util.Iterator;
 16  
 17 /**
 18  * The base class for all graph component classes.
 19  * Maintains flags of use in generic graph algorithms.
 20  * Provides two flags:
 21  * <ul>
 22  * <li><b>marked</b> - typically this is used to indicate a state that persists
 23  * for the course of the graph's lifetime.  For instance, it can be
 24  * used to indicate that a component has been logically deleted from the graph.
 25  * <li><b>visited</b> - this is used to indicate that a component has been processed
 26  * or visited by an single graph algorithm.  For instance, a breadth-first traversal of the
 27  * graph might use this to indicate that a node has already been traversed.
 28  * The visited flag may be set and cleared many times during the lifetime of a graph.
 29  * </ul>
 30  *
 31  * <p>
 32  * Graph components support storing user context data.  This will typically be
 33  * used by client algorithms which use planar graphs.
 34  *
 35  * @version 1.7
 36  */
 37 public abstract class GraphComponent
 38 {
 39   /**
 40    * Sets the Visited state for all {@link GraphComponent}s in an {@link Iterator}
 41    *
 42    * @param i the Iterator to scan
 43    * @param visited the state to set the visited flag to
 44    */
 45   public static void setVisited(Iterator i, boolean visited)
 46   {
 47     while (i.hasNext()) {
 48       GraphComponent comp = (GraphComponent) i.next();
 49       comp.setVisited(visited);
 50     }
 51   }
 52  
 53   /**
 54    * Sets the Marked state for all {@link GraphComponent}s in an {@link Iterator}
 55    *
 56    * @param i the Iterator to scan
 57    * @param marked the state to set the Marked flag to
 58    */
 59   public static void setMarked(Iterator i, boolean marked)
 60   {
 61     while (i.hasNext()) {
 62       GraphComponent comp = (GraphComponent) i.next();
 63       comp.setMarked(marked);
 64     }
 65   }
 66  
 67   /**
 68    * Finds the first {@link GraphComponent} in a {@link Iterator} set
 69    * which has the specified visited state.
 70    *
 71    * @param i an Iterator of GraphComponents
 72    * @param visitedState the visited state to test
 73    * @return the first component found, or <code>null</code> if none found
 74    */
 75   public static GraphComponent getComponentWithVisitedState(Iterator i, boolean visitedState)
 76   {
 77     while (i.hasNext()) {
 78       GraphComponent comp = (GraphComponent) i.next();
 79       if (comp.isVisited() == visitedState)
 80         return comp;
 81     }
 82     return null;
 83   }
 84  
 85   protected boolean isMarked = false;
 86   protected boolean isVisited = false;
 87   private Object data;
 88  
 89   public GraphComponent() {
 90   }
 91  
 92   /**
 93    * Tests if a component has been visited during the course of a graph algorithm
 94    * @return <code>true</code> if the component has been visited
 95    */
 96   public boolean isVisited() { return isVisited; }
 97  
 98   /**
 99    * Sets the visited flag for this component.
100    * @param isVisited the desired value of the visited flag
101    */
102   public void setVisited(boolean isVisited) { this.isVisited = isVisited; }
103  
104   /**
105    * Tests if a component has been marked at some point during the processing
106    * involving this graph.
107    * @return <code>true</code> if the component has been marked
108    */
109   public boolean isMarked() { return isMarked; }
110  
111   /**
112    * Sets the marked flag for this component.
113    * @param isMarked the desired value of the marked flag
114    */
115   public void setMarked(boolean isMarked) { this.isMarked = isMarked; }
116  
117   /**
118    * Sets the user-defined data for this component.
119    *
120    * @param data an Object containing user-defined data
121    */
122   public void setContext(Object data) { this.data = data; }
123  
124   /**
125    * Gets the user-defined data for this component.
126    *
127    * @return the user-defined data
128    */
129   public Object getContext() { return data; }
130  
131   /**
132    * Sets the user-defined data for this component.
133    *
134    * @param data an Object containing user-defined data
135    */
136   public void setData(Object data) { this.data = data; }
137  
138   /**
139    * Gets the user-defined data for this component.
140    *
141    * @return the user-defined data
142    */
143   public Object getData() { return data; }
144  
145   /**
146    * Tests whether this component has been removed from its containing graph
147    *
148    * @return <code>true</code> if this component is removed
149    */
150   public abstract boolean isRemoved();
151 }
152