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