01: /*
02: * @(#)rangeEnum.java 1.2 04/12/06
03: *
04: * Copyright (c) 2001-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.util;
10:
11: import pnuts.lang.*;
12: import java.util.Enumeration;
13:
14: /*
15: * function rangeEnum(start, end {, step})
16: */
17: public class rangeEnum extends PnutsFunction {
18:
19: public rangeEnum() {
20: super ("rangeEnum");
21: }
22:
23: public boolean defined(int nargs) {
24: return nargs == 2 || nargs == 3;
25: }
26:
27: protected Object exec(Object[] args, Context context) {
28: int nargs = args.length;
29: if (nargs == 2 || nargs == 3) {
30: final int start = ((Integer) args[0]).intValue();
31: final int end = ((Integer) args[1]).intValue();
32: int step;
33: if (nargs == 3) {
34: step = ((Integer) args[2]).intValue();
35: } else {
36: if (start > end) {
37: step = -1;
38: } else {
39: step = 1;
40: }
41: }
42: final int fstep = step;
43: class Enum implements Enumeration {
44: int pos = start;
45: boolean increase = (fstep > 0);
46:
47: public boolean hasMoreElements() {
48: if (increase) {
49: return pos <= end;
50: } else {
51: return pos >= end;
52: }
53: }
54:
55: public Object nextElement() {
56: Object ret = new Integer(pos);
57: pos += fstep;
58: return ret;
59: }
60: }
61: return new Enum();
62:
63: } else {
64: undefined(args, context);
65: return null;
66: }
67: }
68:
69: public String toString() {
70: return "function rangeEnum(start, end {, step})";
71: }
72: }
|