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 * StandardCategorySeriesLabelGenerator.java 029 * ----------------------------------------- 030 * (C) Copyright 2005-present, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): Tracy Hiltbrand (equals/hashCode comply with EqualsVerifier); 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import java.text.MessageFormat; 041import java.util.Objects; 042 043import org.jfree.chart.util.Args; 044import org.jfree.chart.util.PublicCloneable; 045import org.jfree.data.category.CategoryDataset; 046 047/** 048 * A standard series label generator for plots that use data from 049 * a {@link org.jfree.data.category.CategoryDataset}. 050 */ 051public class StandardCategorySeriesLabelGenerator implements 052 CategorySeriesLabelGenerator, Cloneable, PublicCloneable, Serializable { 053 054 /** For serialization. */ 055 private static final long serialVersionUID = 4630760091523940820L; 056 057 /** The default item label format. */ 058 public static final String DEFAULT_LABEL_FORMAT = "{0}"; 059 060 /** The format pattern. */ 061 private String formatPattern; 062 063 /** 064 * Creates a default series label generator (uses 065 * {@link #DEFAULT_LABEL_FORMAT}). 066 */ 067 public StandardCategorySeriesLabelGenerator() { 068 this(DEFAULT_LABEL_FORMAT); 069 } 070 071 /** 072 * Creates a new series label generator. 073 * 074 * @param format the format pattern ({@code null} not permitted). 075 */ 076 public StandardCategorySeriesLabelGenerator(String format) { 077 Args.nullNotPermitted(format, "format"); 078 this.formatPattern = format; 079 } 080 081 /** 082 * Generates a label for the specified series. 083 * 084 * @param dataset the dataset ({@code null} not permitted). 085 * @param series the series. 086 * 087 * @return A series label. 088 */ 089 @Override 090 public String generateLabel(CategoryDataset dataset, int series) { 091 Args.nullNotPermitted(dataset, "dataset"); 092 String label = MessageFormat.format(this.formatPattern, 093 createItemArray(dataset, series)); 094 return label; 095 } 096 097 /** 098 * Creates the array of items that can be passed to the 099 * {@link MessageFormat} class for creating labels. 100 * 101 * @param dataset the dataset ({@code null} not permitted). 102 * @param series the series (zero-based index). 103 * 104 * @return The items (never {@code null}). 105 */ 106 protected Object[] createItemArray(CategoryDataset dataset, int series) { 107 Object[] result = new Object[1]; 108 result[0] = dataset.getRowKey(series).toString(); 109 return result; 110 } 111 112 /** 113 * Returns an independent copy of the generator. 114 * 115 * @return A clone. 116 * 117 * @throws CloneNotSupportedException if cloning is not supported. 118 */ 119 @Override 120 public Object clone() throws CloneNotSupportedException { 121 return super.clone(); 122 } 123 124 /** 125 * Tests this object for equality with an arbitrary object. 126 * 127 * @param obj the other object ({@code null} permitted). 128 * 129 * @return A boolean. 130 */ 131 @Override 132 public boolean equals(Object obj) { 133 if (obj == this) { 134 return true; 135 } 136 if (!(obj instanceof StandardCategorySeriesLabelGenerator)) { 137 return false; 138 } 139 StandardCategorySeriesLabelGenerator that 140 = (StandardCategorySeriesLabelGenerator) obj; 141 if (!Objects.equals(this.formatPattern, that.formatPattern)) { 142 return false; 143 } 144 return true; 145 } 146 147 /** 148 * Returns a hash code for this instance. 149 * 150 * @return A hash code. 151 */ 152 @Override 153 public int hashCode() { 154 int hash = 5; 155 hash = 53 * hash + Objects.hashCode(this.formatPattern); 156 return hash; 157 } 158 159}