001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2021, by Object Refinery Limited 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 * StandardFlowLabelGenerator.java
029 * -------------------------------
030 * (C) Copyright 2021, by Object Refinery Limited and Contributors.
031 *
032 * Original Author:  David Gilbert (for Object Refinery Limited);
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.labels;
038
039import java.io.Serializable;
040import java.util.Formatter;
041import java.util.Objects;
042import org.jfree.chart.util.Args;
043import org.jfree.data.flow.FlowDataset;
044import org.jfree.data.flow.FlowKey;
045
046/**
047 * Standard flow label generator.  Instances of this class are immutable.
048 * 
049 * @since 1.5.3
050 */
051public class StandardFlowLabelGenerator implements FlowLabelGenerator, Serializable {
052    
053    /** The default template for formatting the label. */
054    public static final String DEFAULT_TEMPLATE = "%2$s to %3$s = %4$,.2f";
055    
056    /** The template. */
057    private String template;
058    
059    /**
060     * Creates a new instance with the default template.
061     */
062    public StandardFlowLabelGenerator() {
063        this(DEFAULT_TEMPLATE);    
064    }
065    
066    /**
067     * Creates a new generator with the specified template.  The template
068     * is passed to a Java Formatter instance along with four arguments, the
069     * stage (an integer), the source (a String), the destination (a String)
070     * and the flow value (a Number).
071     * 
072     * @param template  the template ({@code null} not permitted). 
073     */
074    public StandardFlowLabelGenerator(String template) {
075        Args.nullNotPermitted(template, "template");
076        this.template = template;
077    }
078
079    /**
080     * Returns a label for the specified flow.
081     * 
082     * @param dataset  the flow dataset ({@code null} not permitted).
083     * @param key  the flow key ({@code null} not permitted).
084     * 
085     * @return The label (possibly {@code null}). 
086     */
087    @Override
088    public String generateLabel(FlowDataset dataset, FlowKey key) {
089        Args.nullNotPermitted(dataset, "dataset");
090        Args.nullNotPermitted(key, "key");
091        String result;
092        try (Formatter formatter = new Formatter(new StringBuilder())) {
093            Number value = dataset.getFlow(key.getStage(), key.getSource(), key.getDestination());
094            formatter.format(this.template, key.getStage(), key.getSource(), key.getDestination(), value);
095            result = formatter.toString();
096        }
097        return result;
098    }
099    
100    /**
101     * Tests this instance for equality with an arbitrary object.
102     * 
103     * @param obj  the object to test ({@code null} permitted).
104     * 
105     * @return A boolean. 
106     */
107    @Override
108    public boolean equals(Object obj) {
109        if (!(obj instanceof StandardFlowLabelGenerator)) {
110            return false;
111        }
112        StandardFlowLabelGenerator that = (StandardFlowLabelGenerator) obj;
113        if (!this.template.equals(that.template)) {
114            return false;
115        }
116        return true;
117    }
118
119    @Override
120    public int hashCode() {
121        int hash = 3;
122        hash = 97 * hash + Objects.hashCode(this.template);
123        return hash;
124    }
125
126}
127