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