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 * 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 043package org.jfree.data.function; 044 045import java.io.Serializable; 046 047import org.jfree.chart.HashUtilities; 048 049/** 050 * A function in the form y = a + bx. 051 */ 052public 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 @Override 101 public double getValue(double x) { 102 return this.a + this.b * x; 103 } 104 105 /** 106 * Tests this function for equality with an arbitrary object. 107 * 108 * @param obj the object (<code>null</code> permitted). 109 * 110 * @return A boolean. 111 */ 112 @Override 113 public boolean equals(Object obj) { 114 if (!(obj instanceof LineFunction2D)) { 115 return false; 116 } 117 LineFunction2D that = (LineFunction2D) obj; 118 if (this.a != that.a) { 119 return false; 120 } 121 if (this.b != that.b) { 122 return false; 123 } 124 return true; 125 } 126 127 /** 128 * Returns a hash code for this instance. 129 * 130 * @return A hash code. 131 */ 132 @Override 133 public int hashCode() { 134 int result = 29; 135 result = HashUtilities.hashCode(result, this.a); 136 result = HashUtilities.hashCode(result, this.b); 137 return result; 138 } 139}