001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2014, 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 * CompassFormat.java 029 * ------------------ 030 * (C) Copyright 2003-2014, by Sylvain Vieujot and Contributors. 031 * 032 * Original Author: Sylvain Vieujot; 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * Simon Legner (GitHub #298); 035 * 036 * Changes 037 * ------- 038 * 18-Feb-2004 : Version 1 contributed by Sylvain Vieujot (DG); 039 * 04-Feb-2014 : Make direction strings user-definable (SL); 040 * 041 */ 042 043package org.jfree.chart.axis; 044 045import java.text.FieldPosition; 046import java.text.NumberFormat; 047import java.text.ParsePosition; 048import org.jfree.chart.util.ParamChecks; 049 050/** 051 * A formatter that displays numbers as directions. 052 */ 053public class CompassFormat extends NumberFormat { 054 055 /** The directions. */ 056 public final String[] directions; 057 058 /** 059 * Creates a new formatter using English identifiers. 060 */ 061 public CompassFormat() { 062 this("N", "E", "S", "W"); 063 } 064 065 /** 066 * Creates a new formatter using the specified identifiers for 067 * the base wind directions. 068 * 069 * @param n the code for NORTH. 070 * @param e the code for EAST. 071 * @param s the code for SOUTH. 072 * @param w the code for WEST. 073 * 074 * @since 1.0.18 075 */ 076 public CompassFormat(String n, String e, String s, String w) { 077 this(new String[] { 078 n, n + n + e, n + e, e + n + e, e, e + s + e, s + e, s + s + e, s, 079 s + s + w, s + w, w + s + w, w, w + n + w, n + w, n + n + w 080 }); 081 } 082 083 /** 084 * Creates a new formatter using the specified identifiers. 085 * 086 * @param directions an array containing 16 strings representing 087 * the directions of a compass. 088 * 089 * @since 1.0.18 090 */ 091 public CompassFormat(String[] directions) { 092 super(); 093 ParamChecks.nullNotPermitted(directions, "directions"); 094 if (directions.length != 16) { 095 throw new IllegalArgumentException("The 'directions' array must " 096 + "contain exactly 16 elements"); 097 } 098 this.directions = directions; 099 } 100 101 /** 102 * Returns a string representing the direction. 103 * 104 * @param direction the direction. 105 * 106 * @return A string. 107 */ 108 public String getDirectionCode(double direction) { 109 direction = direction % 360; 110 if (direction < 0.0) { 111 direction = direction + 360.0; 112 } 113 int index = ((int) Math.floor(direction / 11.25) + 1) / 2; 114 return directions[index]; 115 } 116 117 /** 118 * Formats a number into the specified string buffer. 119 * 120 * @param number the number to format. 121 * @param toAppendTo the string buffer. 122 * @param pos the field position (ignored here). 123 * 124 * @return The string buffer. 125 */ 126 @Override 127 public StringBuffer format(double number, StringBuffer toAppendTo, 128 FieldPosition pos) { 129 return toAppendTo.append(getDirectionCode(number)); 130 } 131 132 /** 133 * Formats a number into the specified string buffer. 134 * 135 * @param number the number to format. 136 * @param toAppendTo the string buffer. 137 * @param pos the field position (ignored here). 138 * 139 * @return The string buffer. 140 */ 141 @Override 142 public StringBuffer format(long number, StringBuffer toAppendTo, 143 FieldPosition pos) { 144 return toAppendTo.append(getDirectionCode(number)); 145 } 146 147 /** 148 * This method returns <code>null</code> for all inputs. This class cannot 149 * be used for parsing. 150 * 151 * @param source the source string. 152 * @param parsePosition the parse position. 153 * 154 * @return <code>null</code>. 155 */ 156 @Override 157 public Number parse(String source, ParsePosition parsePosition) { 158 return null; 159 } 160 161}