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 * HistogramType.java
029 * ------------------
030 * (C) Copyright 2004-present, by David Gilbert and Contributors.
031 *
032 * Original Author:  David Gilbert;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.data.statistics;
038
039import java.io.ObjectStreamException;
040import java.io.Serializable;
041
042/**
043 * A class for creating constants to represent the histogram type.  See Bloch's
044 * enum tip in 'Effective Java'.
045 */
046public class HistogramType implements Serializable {
047
048    /** For serialization. */
049    private static final long serialVersionUID = 2618927186251997727L;
050
051    /** Frequency histogram. */
052    public static final HistogramType FREQUENCY
053        = new HistogramType("FREQUENCY");
054
055    /** Relative frequency. */
056    public static final HistogramType RELATIVE_FREQUENCY
057        = new HistogramType("RELATIVE_FREQUENCY");
058
059    /** Scale area to one. */
060    public static final HistogramType SCALE_AREA_TO_1
061        = new HistogramType("SCALE_AREA_TO_1");
062
063    /** The type name. */
064    private String name;
065
066    /**
067     * Creates a new type.
068     *
069     * @param name  the name.
070     */
071    private HistogramType(String name) {
072        this.name = name;
073    }
074
075    /**
076     * Returns a string representing the object.
077     *
078     * @return The string.
079     */
080    @Override
081    public String toString() {
082        return this.name;
083    }
084
085    /**
086     * Tests this type for equality with an arbitrary object.
087     *
088     * @param obj  the object to test against.
089     *
090     * @return A boolean.
091     */
092    @Override
093    public boolean equals(Object obj) {
094
095        if (obj == null) {
096            return false;
097        }
098
099        if (obj == this) {
100            return true;
101        }
102
103        if (!(obj instanceof HistogramType)) {
104            return false;
105        }
106
107        HistogramType t = (HistogramType) obj;
108        if (!this.name.equals(t.name)) {
109            return false;
110        }
111
112        return true;
113
114    }
115
116    /**
117     * Returns a hash code value for the object.
118     *
119     * @return The hashcode
120     */
121    @Override
122    public int hashCode() {
123        return this.name.hashCode();
124    }
125
126    /**
127     * Ensures that serialization returns the unique instances.
128     *
129     * @return The object.
130     *
131     * @throws ObjectStreamException if there is a problem.
132     */
133    private Object readResolve() throws ObjectStreamException {
134        if (this.equals(HistogramType.FREQUENCY)) {
135            return HistogramType.FREQUENCY;
136        }
137        else if (this.equals(HistogramType.RELATIVE_FREQUENCY)) {
138            return HistogramType.RELATIVE_FREQUENCY;
139        }
140        else if (this.equals(HistogramType.SCALE_AREA_TO_1)) {
141            return HistogramType.SCALE_AREA_TO_1;
142        }
143        return null;
144    }
145
146}
147