Building Parsers with Java

sjm.examples.sling
Class SlingFunction

java.lang.Object
  |
  +--sjm.examples.sling.SlingFunction
Direct Known Subclasses:
Abs, Arithmetic, Cartesian, Ceil, Cos, Floor, Point, Polar, Random, Scale, Sin, Slider, Sling, T, Tan, Variable

public abstract class SlingFunction
extends java.lang.Object
implements java.lang.Cloneable

SlingFunction is an abstract class that requires subclasses to implement the method Point f(double t). Subclasses typically accept other functions in their constructors, and wrap some base function around these subfunctions. The SlingFunction class stores the subfunctions in a SlingFunction array it calls source. The function that a subclass supplies should correspond to the name of the class, such as Sin.

This class provides two methods that subclasses typically will not override: fresh and eval. The fresh method creates a special type of clone. The eval method creates a new version of an object, evaluating any variables in the object's subfunctions to non-variable values.


Field Summary
protected  SlingFunction[] source
           
 
Constructor Summary
SlingFunction()
          Construct a function that wraps nothing.
SlingFunction(SlingFunction source)
          Construct a function that wraps the provided function.
SlingFunction(SlingFunction[] source)
          Construct a function that wraps the provided functions.
SlingFunction(SlingFunction source0, SlingFunction source1)
          Construct a function that wraps the provided functions.
 
Method Summary
 SlingFunction eval()
          Creates a new version of an object, evaluating any variables in the object's subfunctions to non-variable values.
 Extrema extrema(int nPoint)
          Return the extreme values of this function will reach when rendered with the given number of points.
abstract  Point f(double t)
          This is the function that all subclasses implement, so that each function class provides a two-dimensional function of time.
 SlingFunction fresh()
          Creates a copy of the object and initializes the source array to an array of the right length, but leaves the elements of the array empty.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

source

protected SlingFunction[] source
Constructor Detail

SlingFunction

public SlingFunction()
Construct a function that wraps nothing.

SlingFunction

public SlingFunction(SlingFunction[] source)
Construct a function that wraps the provided functions.

SlingFunction

public SlingFunction(SlingFunction source)
Construct a function that wraps the provided function.

SlingFunction

public SlingFunction(SlingFunction source0,
                     SlingFunction source1)
Construct a function that wraps the provided functions.
Method Detail

eval

public SlingFunction eval()
Creates a new version of an object, evaluating any variables in the object's subfunctions to non-variable values.

For example, an object of subclass Arithmetic might contain r*cos(theta). If r has a value of 2 and theta has a value of, say, 2*pi*t, then the object will evaluate to 2*cos(2*pi*t).


extrema

public Extrema extrema(int nPoint)
Return the extreme values of this function will reach when rendered with the given number of points.
Parameters:
int - the number of points to consider in rendering the function
Returns:
the extreme values of this function will reach when rendered with the given number of points

f

public abstract Point f(double t)
This is the function that all subclasses implement, so that each function class provides a two-dimensional function of time. Time, by definition, goes from 0 to 1 as a plot unfolds.
Parameters:
t - a number that represents how far along a plot is, and thus tells which point to return
Returns:
the value of the function at the given time

fresh

public SlingFunction fresh()
Creates a copy of the object and initializes the source array to an array of the right length, but leaves the elements of the array empty.

by Steve Metsker