001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2022, 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 * StandardCrosshairLabelGenerator.java 029 * ------------------------------------ 030 * (C) Copyright 2009-2022, by David Gilbert. 031 * 032 * Original Author: David Gilbert; 033 * Contributor(s): -; 034 * 035 */ 036 037package org.jfree.chart.labels; 038 039import java.io.Serializable; 040import java.text.MessageFormat; 041import java.text.NumberFormat; 042import org.jfree.chart.internal.Args; 043import org.jfree.chart.plot.Crosshair; 044 045/** 046 * A default label generator. 047 */ 048public class StandardCrosshairLabelGenerator implements CrosshairLabelGenerator, 049 Serializable { 050 051 /** The label format string. */ 052 private final String labelTemplate; 053 054 /** A number formatter for the value. */ 055 private final NumberFormat numberFormat; 056 057 /** 058 * Creates a new instance with default attributes. 059 */ 060 public StandardCrosshairLabelGenerator() { 061 this("{0}", NumberFormat.getNumberInstance()); 062 } 063 064 /** 065 * Creates a new instance with the specified attributes. 066 * 067 * @param labelTemplate the label template ({@code null} not 068 * permitted). 069 * @param numberFormat the number formatter ({@code null} not 070 * permitted). 071 */ 072 public StandardCrosshairLabelGenerator(String labelTemplate, 073 NumberFormat numberFormat) { 074 super(); 075 Args.nullNotPermitted(labelTemplate, "labelTemplate"); 076 Args.nullNotPermitted(numberFormat, "numberFormat"); 077 this.labelTemplate = labelTemplate; 078 this.numberFormat = numberFormat; 079 } 080 081 /** 082 * Returns the label template string. 083 * 084 * @return The label template string (never {@code null}). 085 */ 086 public String getLabelTemplate() { 087 return this.labelTemplate; 088 } 089 090 /** 091 * Returns the number formatter. 092 * 093 * @return The formatter (never {@code null}). 094 */ 095 public NumberFormat getNumberFormat() { 096 return this.numberFormat; 097 } 098 099 /** 100 * Returns a string that can be used as the label for a crosshair. 101 * 102 * @param crosshair the crosshair ({@code null} not permitted). 103 * 104 * @return The label (possibly {@code null}). 105 */ 106 @Override 107 public String generateLabel(Crosshair crosshair) { 108 Object[] v = new Object[] {this.numberFormat.format( 109 crosshair.getValue())}; 110 String result = MessageFormat.format(this.labelTemplate, v); 111 return result; 112 } 113 114 /** 115 * Tests this generator for equality with an arbitrary object. 116 * 117 * @param obj the object ({@code null} permitted). 118 * 119 * @return A boolean. 120 */ 121 @Override 122 public boolean equals(Object obj) { 123 if (obj == this) { 124 return true; 125 } 126 if (!(obj instanceof StandardCrosshairLabelGenerator)) { 127 return false; 128 } 129 StandardCrosshairLabelGenerator that 130 = (StandardCrosshairLabelGenerator) obj; 131 if (!this.labelTemplate.equals(that.labelTemplate)) { 132 return false; 133 } 134 if (!this.numberFormat.equals(that.numberFormat)) { 135 return false; 136 } 137 return true; 138 } 139 140 /** 141 * Returns a hash code for this instance. 142 * 143 * @return A hash code for this instance. 144 */ 145 @Override 146 public int hashCode() { 147 return this.labelTemplate.hashCode(); 148 } 149 150}