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     * LineFunction2D.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    
047    import org.jfree.chart.HashUtilities;
048    
049    /**
050     * A function in the form y = a + bx.
051     */
052    public class LineFunction2D implements Function2D, Serializable {
053    
054        /** The intercept. */
055        private double a;
056    
057        /** The slope of the line. */
058        private double b;
059    
060        /**
061         * Constructs a new line function.
062         *
063         * @param a  the intercept.
064         * @param b  the slope.
065         */
066        public LineFunction2D(double a, double b) {
067            this.a = a;
068            this.b = b;
069        }
070    
071        /**
072         * Returns the 'a' coefficient that was specified in the constructor.
073         *
074         * @return The 'a' coefficient.
075         *
076         * @since 1.0.14
077         */
078        public double getIntercept() {
079            return this.a;
080        }
081    
082        /**
083         * Returns the 'b' coefficient that was specified in the constructor.
084         *
085         * @return The 'b' coefficient.
086         *
087         * @since 1.0.14
088         */
089        public double getSlope() {
090            return this.b;
091        }
092    
093        /**
094         * Returns the function value.
095         *
096         * @param x  the x-value.
097         *
098         * @return The value.
099         */
100        public double getValue(double x) {
101            return this.a + this.b * x;
102        }
103    
104        /**
105         * Tests this function for equality with an arbitrary object.
106         *
107         * @param obj  the object (<code>null</code> permitted).
108         *
109         * @return A boolean.
110         */
111        public boolean equals(Object obj) {
112            if (!(obj instanceof LineFunction2D)) {
113                return false;
114            }
115            LineFunction2D that = (LineFunction2D) obj;
116            if (this.a != that.a) {
117                return false;
118            }
119            if (this.b != that.b) {
120                return false;
121            }
122            return true;
123        }
124    
125        /**
126         * Returns a hash code for this instance.
127         * 
128         * @return A hash code.
129         */
130        public int hashCode() {
131            int result = 29;
132            result = HashUtilities.hashCode(result, this.a);
133            result = HashUtilities.hashCode(result, this.b);
134            return result;
135        }
136    }