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 * FlowKey.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 flow within a dataset. 046 * 047 * @param <K> the type for the keys used to identify sources and destinations 048 * ({@code String} is a good default choice). 049 * 050 * @since 1.5.3 051 */ 052public class FlowKey<K extends Comparable<K>> implements PublicCloneable, Serializable { 053 054 /** 055 * The key for a flow property that, if defined (at the dataset level), 056 * contains a {@code Boolean} value for the selection status of the flow. 057 */ 058 public static final String SELECTED_PROPERTY_KEY = "selected"; 059 060 /** The stage. */ 061 private final int stage; 062 063 /* The source node. */ 064 private final K source; 065 066 /* The destination node. */ 067 private final K destination; 068 069 /** 070 * Creates a new instance. 071 * 072 * @param stage the stage. 073 * @param source the source identifier ({@code null} not permitted). 074 * @param destination the destination identifier ({@code null} not permitted). 075 */ 076 public FlowKey(int stage, K source, K destination) { 077 Args.nullNotPermitted(source, "source"); 078 Args.nullNotPermitted(destination, "destination"); 079 this.stage = stage; 080 this.source = source; 081 this.destination = destination; 082 } 083 084 /** 085 * Returns the stage number for the flow. 086 * 087 * @return The stage number. 088 */ 089 public int getStage() { 090 return this.stage; 091 } 092 093 /** 094 * Returns the source identifier. 095 * 096 * @return The source identifier (never {@code null}). 097 */ 098 public K getSource() { 099 return source; 100 } 101 102 /** 103 * Returns the destination identifier. 104 * 105 * @return The destination identifier (never {@code null}). 106 */ 107 public K getDestination() { 108 return destination; 109 } 110 111 /** 112 * Returns a string representation of this instance, primarily for 113 * debugging purposes. 114 * 115 * @return A string. 116 */ 117 @Override 118 public String toString() { 119 return "[FlowKey: " + this.stage + ", " + this.source + " -> " + this.destination + "]"; 120 } 121 122 /** 123 * Tests this instance for equality with an arbitrary object. 124 * 125 * @param obj the object ({@code null} permitted). 126 * 127 * @return A boolean. 128 */ 129 @Override 130 public boolean equals(Object obj) { 131 if (this == obj) { 132 return true; 133 } 134 if (obj == null) { 135 return false; 136 } 137 if (getClass() != obj.getClass()) { 138 return false; 139 } 140 final FlowKey<?> other = (FlowKey<?>) obj; 141 if (this.stage != other.stage) { 142 return false; 143 } 144 if (!Objects.equals(this.source, other.source)) { 145 return false; 146 } 147 if (!Objects.equals(this.destination, other.destination)) { 148 return false; 149 } 150 return true; 151 } 152 153 /** 154 * Returns a hashcode for this instance. 155 * 156 * @return A hashcode. 157 */ 158 @Override 159 public int hashCode() { 160 int hash = 5; 161 hash = 67 * hash + this.stage; 162 hash = 67 * hash + Objects.hashCode(this.source); 163 hash = 67 * hash + Objects.hashCode(this.destination); 164 return hash; 165 } 166 167 @Override 168 public Object clone() throws CloneNotSupportedException { 169 return super.clone(); 170 } 171 172} 173