Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/imageio/spi/DigraphNode.java
38830 views
/*1* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio.spi;2627import java.io.Serializable;28import java.util.HashSet;29import java.util.Iterator;30import java.util.Set;3132/**33* A node in a directed graph. In addition to an arbitrary34* <code>Object</code> containing user data associated with the node,35* each node maintains a <code>Set</code>s of nodes which are pointed36* to by the current node (available from <code>getOutNodes</code>).37* The in-degree of the node (that is, number of nodes that point to38* the current node) may be queried.39*40*/41class DigraphNode implements Cloneable, Serializable {4243/** The data associated with this node. */44protected Object data;4546/**47* A <code>Set</code> of neighboring nodes pointed to by this48* node.49*/50protected Set outNodes = new HashSet();5152/** The in-degree of the node. */53protected int inDegree = 0;5455/**56* A <code>Set</code> of neighboring nodes that point to this57* node.58*/59private Set inNodes = new HashSet();6061public DigraphNode(Object data) {62this.data = data;63}6465/** Returns the <code>Object</code> referenced by this node. */66public Object getData() {67return data;68}6970/**71* Returns an <code>Iterator</code> containing the nodes pointed72* to by this node.73*/74public Iterator getOutNodes() {75return outNodes.iterator();76}7778/**79* Adds a directed edge to the graph. The outNodes list of this80* node is updated and the in-degree of the other node is incremented.81*82* @param node a <code>DigraphNode</code>.83*84* @return <code>true</code> if the node was not previously the85* target of an edge.86*/87public boolean addEdge(DigraphNode node) {88if (outNodes.contains(node)) {89return false;90}9192outNodes.add(node);93node.inNodes.add(this);94node.incrementInDegree();95return true;96}9798/**99* Returns <code>true</code> if an edge exists between this node100* and the given node.101*102* @param node a <code>DigraphNode</code>.103*104* @return <code>true</code> if the node is the target of an edge.105*/106public boolean hasEdge(DigraphNode node) {107return outNodes.contains(node);108}109110/**111* Removes a directed edge from the graph. The outNodes list of this112* node is updated and the in-degree of the other node is decremented.113*114* @return <code>true</code> if the node was previously the target115* of an edge.116*/117public boolean removeEdge(DigraphNode node) {118if (!outNodes.contains(node)) {119return false;120}121122outNodes.remove(node);123node.inNodes.remove(this);124node.decrementInDegree();125return true;126}127128/**129* Removes this node from the graph, updating neighboring nodes130* appropriately.131*/132public void dispose() {133Object[] inNodesArray = inNodes.toArray();134for(int i=0; i<inNodesArray.length; i++) {135DigraphNode node = (DigraphNode) inNodesArray[i];136node.removeEdge(this);137}138139Object[] outNodesArray = outNodes.toArray();140for(int i=0; i<outNodesArray.length; i++) {141DigraphNode node = (DigraphNode) outNodesArray[i];142removeEdge(node);143}144}145146/** Returns the in-degree of this node. */147public int getInDegree() {148return inDegree;149}150151/** Increments the in-degree of this node. */152private void incrementInDegree() {153++inDegree;154}155156/** Decrements the in-degree of this node. */157private void decrementInDegree() {158--inDegree;159}160}161162163