Back to index

wims  3.65+svn20090927
Function.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Mihai Preda.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 package org.javia.arity;
00018 
00037 abstract public class Function {
00038     private int cachedArity = -1;
00039 
00044     public int arity() {
00045         if (cachedArity == -1) {
00046             cachedArity = 0;
00047             for (int i = 0; i < 5; ++i) {
00048                 double args[] = new double[i];
00049                 try {
00050                     eval(args);
00051                     cachedArity = i;
00052                     break;
00053                 } catch (ArityException e) {
00054                     //ignore, continue
00055                 }
00056             }
00057         }
00058         return cachedArity;
00059     }
00060 
00065     public double eval() throws ArityException {
00066         throw new ArityException("Didn't expect 0 args");
00067     }
00068 
00069     public double eval(double x) throws ArityException {
00070         throw new ArityException("Didn't expect 1 args");
00071     }
00072 
00073     public double eval(double x, double y) throws ArityException {
00074         throw new ArityException("Didn't expect 2 args");
00075     }
00076 
00083     public double eval(double args[]) throws ArityException {
00084         switch (args.length) {
00085         case 0:
00086             return eval();
00087         case 1:
00088             return eval(args[0]);
00089         case 2:
00090             return eval(args[0], args[1]);
00091         }
00092         throw new ArityException("unexpected " + args.length + " args");
00093     }
00094 }