001    /* ===========================================================
002     * JFreeChart : a free chart library for the Java(tm) platform
003     * ===========================================================
004     *
005     * (C) Copyright 2000-2011, 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     * PowerFunction2D.java
029     * --------------------
030     * (C) Copyright 2002-2009, by Object Refinery Limited.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * Changes:
036     * --------
037     * 01-Oct-2002 : Version 1 (DG);
038     * 28-May-2009 : Added accessor methods for co-efficients, implemented
039     *               equals() and hashCode(), and added Serialization support (DG);
040     *
041     */
042    
043    package org.jfree.data.function;
044    
045    import java.io.Serializable;
046    import org.jfree.chart.HashUtilities;
047    
048    /**
049     * A function of the form y = a * x ^ b.
050     */
051    public class PowerFunction2D implements Function2D, Serializable {
052    
053        /** The 'a' coefficient. */
054        private double a;
055    
056        /** The 'b' coefficient. */
057        private double b;
058    
059        /**
060         * Creates a new power function.
061         *
062         * @param a  the 'a' coefficient.
063         * @param b  the 'b' coefficient.
064         */
065        public PowerFunction2D(double a, double b) {
066            this.a = a;
067            this.b = b;
068        }
069    
070        /**
071         * Returns the 'a' coefficient that was specified in the constructor.
072         *
073         * @return The 'a' coefficient.
074         *
075         * @since 1.0.14
076         */
077        public double getA() {
078            return this.a;
079        }
080    
081        /**
082         * Returns the 'b' coefficient that was specified in the constructor.
083         *
084         * @return The 'b' coefficient.
085         *
086         * @since 1.0.14
087         */
088        public double getB() {
089            return this.b;
090        }
091    
092        /**
093         * Returns the value of the function for a given input ('x').
094         *
095         * @param x  the x-value.
096         *
097         * @return The value.
098         */
099        public double getValue(double x) {
100            return this.a * Math.pow(x, this.b);
101        }
102    
103        /**
104         * Tests this function for equality with an arbitrary object.
105         *
106         * @param obj  the object (<code>null</code> permitted).
107         *
108         * @return A boolean.
109         */
110        public boolean equals(Object obj) {
111            if (!(obj instanceof PowerFunction2D)) {
112                return false;
113            }
114            PowerFunction2D that = (PowerFunction2D) obj;
115            if (this.a != that.a) {
116                return false;
117            }
118            if (this.b != that.b) {
119                return false;
120            }
121            return true;
122        }
123    
124        /**
125         * Returns a hash code for this instance.
126         *
127         * @return A hash code.
128         */
129        public int hashCode() {
130            int result = 29;
131            result = HashUtilities.hashCode(result, this.a);
132            result = HashUtilities.hashCode(result, this.b);
133            return result;
134        }
135    
136    }