Building Parsers with Java

sjm.examples.sling
Class Abs

java.lang.Object
  |
  +--sjm.examples.sling.SlingFunction
        |
        +--sjm.examples.sling.Abs

public class Abs
extends SlingFunction

This class wraps an absolute value function around another source function.

The absolute value of x is just x if x is positive, and is –x if x is negative.


Fields inherited from class sjm.examples.sling.SlingFunction
source
 
Constructor Summary
Abs()
          Constructs abs(t).
Abs(SlingFunction source)
          Constructs a function object that wraps an abs function around the given source function.
 
Method Summary
 Point f(double t)
          Returns, essentially, abs(source.f(t)).
 java.lang.String toString()
          Returns a string representation of this function.
 
Methods inherited from class sjm.examples.sling.SlingFunction
eval, extrema, fresh
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Abs

public Abs()
Constructs abs(t).

Abs

public Abs(SlingFunction source)
Constructs a function object that wraps an abs function around the given source function.
Method Detail

f

public Point f(double t)
Returns, essentially, abs(source.f(t)).

Subclasses of SlingFunction return a 2D point from the f() method. The abs function needs only one argument from its source, not a whole point. This method ignores the x component of the source's f(t) value, and uses the y component as an argument to Math.abs.

The Math.abs function returns a single number, so this method uses t as the x component of the constant that it returns. Thus the return value is:

    new Point(t, Math.abs(source.f(t).y)) 
 
Overrides:
f in class SlingFunction
Parameters:
t - a number that represents how far along a plot is, and thus tells which point to return
Returns:
a new point: (t, abs(source.f(t).y))

toString

public java.lang.String toString()
Returns a string representation of this function.
Overrides:
toString in class java.lang.Object

by Steve Metsker