Path: blob/master/src/java.desktop/share/classes/javax/imageio/spi/DigraphNode.java
41155 views
/*1* Copyright (c) 2000, 2021, 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.Serial;28import java.io.Serializable;29import java.util.HashSet;30import java.util.Iterator;31import java.util.Set;3233/**34* A node in a directed graph. In addition to an arbitrary35* {@code Object} containing user data associated with the node,36* each node maintains a {@code Set}s of nodes which are pointed37* to by the current node (available from {@code getOutNodes}).38* The in-degree of the node (that is, number of nodes that point to39* the current node) may be queried.40*41*/42class DigraphNode<E> implements Cloneable, Serializable {4344/**45* Use serialVersionUID from JDK 9 for interoperability.46*/47@Serial48private static final long serialVersionUID = 5308261378582246841L;4950/** The data associated with this node. */51@SuppressWarnings("serial") // Not statically typed as Serializable52protected E data;5354/**55* A {@code Set} of neighboring nodes pointed to by this56* node.57*/58@SuppressWarnings("serial") // Not statically typed as Serializable59protected Set<DigraphNode<E>> outNodes = new HashSet<>();6061/** The in-degree of the node. */62protected int inDegree = 0;6364/**65* A {@code Set} of neighboring nodes that point to this66* node.67*/68@SuppressWarnings("serial") // Not statically typed as Serializable69private Set<DigraphNode<E>> inNodes = new HashSet<>();7071public DigraphNode(E data) {72this.data = data;73}7475/** Returns the {@code Object} referenced by this node. */76public E getData() {77return data;78}7980/**81* Returns an {@code Iterator} containing the nodes pointed82* to by this node.83*/84public Iterator<DigraphNode<E>> getOutNodes() {85return outNodes.iterator();86}8788/**89* Adds a directed edge to the graph. The outNodes list of this90* node is updated and the in-degree of the other node is incremented.91*92* @param node a {@code DigraphNode}.93*94* @return {@code true} if the node was not previously the95* target of an edge.96*/97public boolean addEdge(DigraphNode<E> node) {98if (outNodes.contains(node)) {99return false;100}101102outNodes.add(node);103node.inNodes.add(this);104node.incrementInDegree();105return true;106}107108/**109* Returns {@code true} if an edge exists between this node110* and the given node.111*112* @param node a {@code DigraphNode}.113*114* @return {@code true} if the node is the target of an edge.115*/116public boolean hasEdge(DigraphNode<E> node) {117return outNodes.contains(node);118}119120/**121* Removes a directed edge from the graph. The outNodes list of this122* node is updated and the in-degree of the other node is decremented.123*124* @return {@code true} if the node was previously the target125* of an edge.126*/127public boolean removeEdge(DigraphNode<E> node) {128if (!outNodes.contains(node)) {129return false;130}131132outNodes.remove(node);133node.inNodes.remove(this);134node.decrementInDegree();135return true;136}137138/**139* Removes this node from the graph, updating neighboring nodes140* appropriately.141*/142public void dispose() {143Object[] inNodesArray = inNodes.toArray();144for(int i=0; i<inNodesArray.length; i++) {145@SuppressWarnings("unchecked")146DigraphNode<E> node = (DigraphNode<E>)inNodesArray[i];147node.removeEdge(this);148}149150Object[] outNodesArray = outNodes.toArray();151for(int i=0; i<outNodesArray.length; i++) {152@SuppressWarnings("unchecked")153DigraphNode<E> node = (DigraphNode<E>)outNodesArray[i];154removeEdge(node);155}156}157158/** Returns the in-degree of this node. */159public int getInDegree() {160return inDegree;161}162163/** Increments the in-degree of this node. */164private void incrementInDegree() {165++inDegree;166}167168/** Decrements the in-degree of this node. */169private void decrementInDegree() {170--inDegree;171}172}173174175