KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: ReverseOperator.java,v 1.1.1.1 2000/09/26 11:20:34 ramsdell Exp $
2

3 // An example of an operator: the reverse command operator.
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 an operator: the reverse command operator.
27  * This class is used only for testing and as an example.
28  * @version November 1997
29  * @author John D. Ramsdell
30  */

31 public class ReverseOperator
32 implements Operator
33 {
34   /**
35    * Get the name of an operator.
36    * @return the name for printing
37    */

38   public String JavaDoc getName() {
39     return "reverse";
40   }
41
42   /**
43    * Execute the reverse operation. The operator simply prints
44    * the arguments in reverse order.
45    * @param args parameters to the operation
46    * @param out place to write messages
47    * @exception CommandFailedException if no args provided
48    */

49   public void exec(String JavaDoc[] args, java.io.PrintWriter JavaDoc out)
50        throws CommandFailedException
51   {
52     if (args.length > 0) {
53       out.print(args[args.length - 1]);
54       for (int i = args.length - 2; i >= 0; i--)
55     out.print(" " + args[i]);
56       out.println();
57     }
58     else
59       throw new CommandFailedException("Too few args to " + getName());
60   }
61 }
62
Popular Tags