My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* fnPad: a functional programming style calculator
* Copyright © 2001-2010 Hank Dolben, org.dolben.fn.Pad.LICENSE
*/
package org.dolben.fn;

import java.util.List;

/**
* a derivation interface for fnPad functions written in Java
* </p><p>
* A derived class
* <ul>
* <il>has a constructor that specifies the number of paramaters</il>
* <il>implements evaluate() with no parameters</il>
* <il>uses getDouble() or getNumber() to get its arguments</il>
* </ul>
*/
public abstract class Function extends Symbol {

private int _params; // the number of parameters
private List<Object> _args; // the arguments during a "call"

/**
* sets the Symbol name to the (derived) class name
* and saves the number of parameters
*
* @param params the number of parameters to the derived class "function"
*/
public Function( int params ) {
setName(this.getClass().getName());
_params = params;
}

/**
* implements Symbol's evaluate,
* checks that there are the correct number of arguments,
* saves the list, and calls the derived class's evaluate()
*
* @param arguments a list of arguments
*
* @return the result of the derived class's evaluation
*
* @throws Exception when there is an incorrect number of arguments
*/
public Object evaluate( List<Evaluable> arguments ) throws Exception {
if ( arguments.size() != _params ) {
throw new Exception(
"number of arguments of '"+this+"' is not equal to "+_params
);
}
_args = evaluateArguments(arguments);
return evaluate();
}

/**
* evaluates the derived class's function
*
* @return the result of the evaluation
*
* @throws Exception
*/
protected abstract Object evaluate( ) throws Exception;

/**
* gets the double value of an argument
*
* @param index which argument (starts at zero)
*
* @throws Exception when the argument is not a Number
*/
protected double getDouble( int index ) throws Exception {
return getNumber(index).doubleValue();
}

/**
* gets an argument which is a Number
*
* @param index which argument (starts at zero)
*
* @return the argument Number
*
* @throws Exception when the argument is not a Number
*/
protected Number getNumber( int index ) throws Exception {
Object object = _args.get(_params-index-1);
if ( !(object instanceof Number) ) {
throw new Exception(
"'"+object+"' is not a number, argument of '"+this+"'"
);
}
return (Number)object;
}

/**
* gets an argument which is a Long
*
* @param index which argument (starts at zero)
*
* @return the argument Long
*
* @throws Exception when the argument is not a Long
*/
protected long getLong( int index ) throws Exception {
Object object = _args.get(_params-index-1);
if ( !(object instanceof Long) ) {
throw new Exception(
"'"+object+"' is not an integer, argument of '"+this+"'"
);
}
return ((Long)object).longValue();
}

}

Change log

r17 by hdolben on Mar 14, 2010   Diff
use copyright symbol
Go to: 
Project members, sign in to write a code review

Older revisions

r12 by hdolben on Jan 9, 2010   Diff
merged branches/hank-01
r8 by hdolben on Jan 9, 2010   Diff
merged branches/hank-01
r2 by hdolben on Jan 9, 2010   Diff
initial import
All revisions of this file

File info

Size: 3362 bytes, 114 lines
Powered by Google Project Hosting