001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, 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-2008, by Sylvain Vieujot and Contributors.
031 *
032 * Original Author:  Sylvain Vieujot;
033 * Contributor(s):   David Gilbert (for Object Refinery Limited);
034 *
035 * Changes
036 * -------
037 * 18-Feb-2004 : Version 1 contributed by Sylvain Vieujot (DG);
038 *
039 */
040
041package org.jfree.chart.axis;
042
043import java.text.FieldPosition;
044import java.text.NumberFormat;
045import java.text.ParsePosition;
046
047/**
048 * A formatter that displays numbers as directions.
049 */
050public class CompassFormat extends NumberFormat {
051
052    /** North. */
053    private static final String N = "N";
054
055    /** East. */
056    private static final String E = "E";
057
058    /** South. */
059    private static final String S = "S";
060
061    /** West. */
062    private static final String W = "W";
063
064    /** The directions. */
065    public static final String[] DIRECTIONS = {
066        N, N + N + E, N + E, E + N + E, E, E + S + E, S + E, S + S + E, S,
067        S + S + W, S + W, W + S + W, W, W + N + W, N + W, N + N + W, N
068    };
069
070    /**
071     * Creates a new formatter.
072     */
073    public CompassFormat() {
074        super();
075    }
076
077    /**
078     * Returns a string representing the direction.
079     *
080     * @param direction  the direction.
081     *
082     * @return A string.
083     */
084    public String getDirectionCode(double direction) {
085
086        direction = direction % 360;
087        if (direction < 0.0) {
088            direction = direction + 360.0;
089        }
090        int index = ((int) Math.floor(direction / 11.25) + 1) / 2;
091        return DIRECTIONS[index];
092
093    }
094
095    /**
096     * Formats a number into the specified string buffer.
097     *
098     * @param number  the number to format.
099     * @param toAppendTo  the string buffer.
100     * @param pos  the field position (ignored here).
101     *
102     * @return The string buffer.
103     */
104    @Override
105    public StringBuffer format(double number, StringBuffer toAppendTo,
106            FieldPosition pos) {
107        return toAppendTo.append(getDirectionCode(number));
108    }
109
110    /**
111     * Formats a number into the specified string buffer.
112     *
113     * @param number  the number to format.
114     * @param toAppendTo  the string buffer.
115     * @param pos  the field position (ignored here).
116     *
117     * @return The string buffer.
118     */
119    @Override
120    public StringBuffer format(long number, StringBuffer toAppendTo,
121            FieldPosition pos) {
122        return toAppendTo.append(getDirectionCode(number));
123    }
124
125    /**
126     * This method returns <code>null</code> for all inputs.  This class cannot
127     * be used for parsing.
128     *
129     * @param source  the source string.
130     * @param parsePosition  the parse position.
131     *
132     * @return <code>null</code>.
133     */
134    @Override
135    public Number parse(String source, ParsePosition parsePosition) {
136        return null;
137    }
138
139}