KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > ReverseFunction


1 // $Id: ReverseFunction.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // An example of a function: the reverse string list function.
4

5 /*
6  * Copyright 1997 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 /**
26  * An example of a function: the reverse string list function.
27  * This class is used only for testing and as an example.
28  * @version July 1999
29  * @author John D. Ramsdell
30  */

31 public class ReverseFunction
32 implements Function
33 {
34   private final static int nargs = 1;
35
36   /**
37    * Get the name of an function.
38    * @return the name for printing
39    */

40   public String JavaDoc getName() {
41     return "reverse";
42   }
43
44   /**
45    * Invoke the reverse function.
46    * @param args parameters to the function
47    * @param list a string list
48    * @return result of invocation appended to the list
49    * (if list is non-null, result must be a string list)
50    * @exception Exception if invocation failed
51    */

52   public Value invoke(Value[] args, StringList list)
53        throws Exception JavaDoc
54   {
55     if (args.length == nargs) {
56       if (StringList.isStringList(args[0])) {
57     StringList sl = (StringList)args[0];
58     for (; sl != null; sl = sl.getRest())
59       list = new StringList(sl.getString(), list);
60     return list;
61       }
62       else {
63     String JavaDoc msg = getName()
64       + ": the argument is not a string list";
65     throw new StringListCastException(msg);
66       }
67     }
68     else {
69       String JavaDoc msg = "Arg count error: " + getName() + " expecting " + nargs +
70     " but got " + args.length + " arguments";
71       throw new WrongArgCountException(msg);
72     }
73   }
74 }
75
Popular Tags