Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/javax/imageio/spi/DigraphNode.java
41155 views
1
/*
2
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.imageio.spi;
27
28
import java.io.Serial;
29
import java.io.Serializable;
30
import java.util.HashSet;
31
import java.util.Iterator;
32
import java.util.Set;
33
34
/**
35
* A node in a directed graph. In addition to an arbitrary
36
* {@code Object} containing user data associated with the node,
37
* each node maintains a {@code Set}s of nodes which are pointed
38
* to by the current node (available from {@code getOutNodes}).
39
* The in-degree of the node (that is, number of nodes that point to
40
* the current node) may be queried.
41
*
42
*/
43
class DigraphNode<E> implements Cloneable, Serializable {
44
45
/**
46
* Use serialVersionUID from JDK 9 for interoperability.
47
*/
48
@Serial
49
private static final long serialVersionUID = 5308261378582246841L;
50
51
/** The data associated with this node. */
52
@SuppressWarnings("serial") // Not statically typed as Serializable
53
protected E data;
54
55
/**
56
* A {@code Set} of neighboring nodes pointed to by this
57
* node.
58
*/
59
@SuppressWarnings("serial") // Not statically typed as Serializable
60
protected Set<DigraphNode<E>> outNodes = new HashSet<>();
61
62
/** The in-degree of the node. */
63
protected int inDegree = 0;
64
65
/**
66
* A {@code Set} of neighboring nodes that point to this
67
* node.
68
*/
69
@SuppressWarnings("serial") // Not statically typed as Serializable
70
private Set<DigraphNode<E>> inNodes = new HashSet<>();
71
72
public DigraphNode(E data) {
73
this.data = data;
74
}
75
76
/** Returns the {@code Object} referenced by this node. */
77
public E getData() {
78
return data;
79
}
80
81
/**
82
* Returns an {@code Iterator} containing the nodes pointed
83
* to by this node.
84
*/
85
public Iterator<DigraphNode<E>> getOutNodes() {
86
return outNodes.iterator();
87
}
88
89
/**
90
* Adds a directed edge to the graph. The outNodes list of this
91
* node is updated and the in-degree of the other node is incremented.
92
*
93
* @param node a {@code DigraphNode}.
94
*
95
* @return {@code true} if the node was not previously the
96
* target of an edge.
97
*/
98
public boolean addEdge(DigraphNode<E> node) {
99
if (outNodes.contains(node)) {
100
return false;
101
}
102
103
outNodes.add(node);
104
node.inNodes.add(this);
105
node.incrementInDegree();
106
return true;
107
}
108
109
/**
110
* Returns {@code true} if an edge exists between this node
111
* and the given node.
112
*
113
* @param node a {@code DigraphNode}.
114
*
115
* @return {@code true} if the node is the target of an edge.
116
*/
117
public boolean hasEdge(DigraphNode<E> node) {
118
return outNodes.contains(node);
119
}
120
121
/**
122
* Removes a directed edge from the graph. The outNodes list of this
123
* node is updated and the in-degree of the other node is decremented.
124
*
125
* @return {@code true} if the node was previously the target
126
* of an edge.
127
*/
128
public boolean removeEdge(DigraphNode<E> node) {
129
if (!outNodes.contains(node)) {
130
return false;
131
}
132
133
outNodes.remove(node);
134
node.inNodes.remove(this);
135
node.decrementInDegree();
136
return true;
137
}
138
139
/**
140
* Removes this node from the graph, updating neighboring nodes
141
* appropriately.
142
*/
143
public void dispose() {
144
Object[] inNodesArray = inNodes.toArray();
145
for(int i=0; i<inNodesArray.length; i++) {
146
@SuppressWarnings("unchecked")
147
DigraphNode<E> node = (DigraphNode<E>)inNodesArray[i];
148
node.removeEdge(this);
149
}
150
151
Object[] outNodesArray = outNodes.toArray();
152
for(int i=0; i<outNodesArray.length; i++) {
153
@SuppressWarnings("unchecked")
154
DigraphNode<E> node = (DigraphNode<E>)outNodesArray[i];
155
removeEdge(node);
156
}
157
}
158
159
/** Returns the in-degree of this node. */
160
public int getInDegree() {
161
return inDegree;
162
}
163
164
/** Increments the in-degree of this node. */
165
private void incrementInDegree() {
166
++inDegree;
167
}
168
169
/** Decrements the in-degree of this node. */
170
private void decrementInDegree() {
171
--inDegree;
172
}
173
}
174
175