001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * ------------
028 * NodeKey.java
029 * ------------
030 * (C) Copyright 2022-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.flow;
038
039import java.io.Serializable;
040import java.util.Objects;
041import org.jfree.chart.util.Args;
042import org.jfree.chart.util.PublicCloneable;
043
044/**
045 * A key that identifies a node in a {@link FlowDataset}.  Instances of this
046 * class are immutable.
047 * 
048 * @param <K> the type for the keys used to identify sources and destinations 
049 *     ({@code String} is a good default choice).
050 * 
051 * @since 1.5.3
052 */
053public class NodeKey <K extends Comparable<K>> implements PublicCloneable, Serializable {
054
055    /** 
056     * The key for a node property that, if defined (at the dataset level), 
057     * contains a {@code Boolean} value for the selection status of the node.
058     */
059    public static final String SELECTED_PROPERTY_KEY = "selected";
060    
061    /** The stage. */
062    private final int stage;
063    
064    /* The source node. */
065    private final K node;
066    
067    /**
068     * Creates a new key referencing a node in a {@link FlowDataset}.
069     * 
070     * @param stage  the stage.
071     * @param node  the node key.
072     */
073    public NodeKey(int stage, K node) {
074        Args.nullNotPermitted(node, "node");
075        this.stage = stage;
076        this.node = node;
077    }
078
079    /**
080     * Returns the stage number.
081     * 
082     * @return The stage number. 
083     */
084    public int getStage() {
085        return this.stage;
086    }
087
088    /**
089     * Returns the identifier for the node.
090     * 
091     * @return The identifier for the node (never {@code null}).
092     */
093    public K getNode() {
094        return this.node;
095    }
096    
097    /**
098     * Returns a string representation of this instance, primarily for 
099     * debugging purposes.
100     * 
101     * @return A string. 
102     */
103    @Override
104    public String toString() {
105        return "[NodeKey: " + stage + ", " + node + "]";
106    }
107
108    /**
109     * Tests this instance for equality with an arbitrary object.
110     * 
111     * @param obj  the object ({@code null} permitted).
112     * 
113     * @return A boolean. 
114     */
115    @Override
116    public boolean equals(Object obj) {
117        if (this == obj) {
118            return true;
119        }
120        if (obj == null) {
121            return false;
122        }
123        if (getClass() != obj.getClass()) {
124            return false;
125        }
126        final NodeKey<?> other = (NodeKey<?>) obj;
127        if (this.stage != other.stage) {
128            return false;
129        }
130        if (!Objects.equals(this.node, other.node)) {
131            return false;
132        }
133        return true;
134    }
135    
136    /**
137     * Returns a hashcode for this instance.
138     * 
139     * @return A hashcode.
140     */
141    @Override
142    public int hashCode() {
143        int hash = 3;
144        hash = 53 * hash + this.stage;
145        hash = 53 * hash + Objects.hashCode(this.node);
146        return hash;
147    }
148
149    @Override
150    public Object clone() throws CloneNotSupportedException {
151        return super.clone();
152    }
153
154}