001: /*
002: * @(#)reverse.java 1.3 05/01/14
003: *
004: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.lib;
010:
011: import java.util.*;
012: import java.lang.reflect.*;
013: import pnuts.lang.*;
014: import pnuts.lang.Runtime;
015:
016: /*
017: * function reverse(arrayOrList)
018: */
019: public class reverse extends PnutsFunction {
020:
021: public reverse() {
022: super ("reverse");
023: }
024:
025: public boolean defined(int nargs) {
026: return nargs == 1;
027: }
028:
029: protected Object exec(Object[] args, Context context) {
030: int nargs = args.length;
031: if (nargs != 1) {
032: undefined(args, context);
033: return null;
034: }
035: Object arg = args[0];
036: if (arg instanceof Object[]) {
037: Object[] a = (Object[]) arg;
038: int len = a.length;
039: for (int i = 0; i < len / 2; i++) {
040: Object t = a[i];
041: int j = len - i - 1;
042: a[i] = a[j];
043: a[j] = t;
044: }
045: } else if (arg instanceof int[]) {
046: int[] a = (int[]) arg;
047: int len = a.length;
048: for (int i = 0; i < len / 2; i++) {
049: int t = a[i];
050: int j = len - i - 1;
051: a[i] = a[j];
052: a[j] = t;
053: }
054: } else if (arg instanceof byte[]) {
055: byte[] a = (byte[]) arg;
056: int len = a.length;
057: for (int i = 0; i < len / 2; i++) {
058: byte t = a[i];
059: int j = len - i - 1;
060: a[i] = a[j];
061: a[j] = t;
062: }
063: } else if (arg instanceof char[]) {
064: char[] a = (char[]) arg;
065: int len = a.length;
066: for (int i = 0; i < len / 2; i++) {
067: char t = a[i];
068: int j = len - i - 1;
069: a[i] = a[j];
070: a[j] = t;
071: }
072: } else if (arg instanceof short[]) {
073: short[] a = (short[]) arg;
074: int len = a.length;
075: for (int i = 0; i < len / 2; i++) {
076: short t = a[i];
077: int j = len - i - 1;
078: a[i] = a[j];
079: a[j] = t;
080: }
081: } else if (arg instanceof float[]) {
082: float[] a = (float[]) arg;
083: int len = a.length;
084: for (int i = 0; i < len / 2; i++) {
085: float t = a[i];
086: int j = len - i - 1;
087: a[i] = a[j];
088: a[j] = t;
089: }
090: } else if (arg instanceof double[]) {
091: double[] a = (double[]) arg;
092: int len = a.length;
093: for (int i = 0; i < len / 2; i++) {
094: double t = a[i];
095: int j = len - i - 1;
096: a[i] = a[j];
097: a[j] = t;
098: }
099: } else if (arg instanceof long[]) {
100: long[] a = (long[]) arg;
101: int len = a.length;
102: for (int i = 0; i < len / 2; i++) {
103: long t = a[i];
104: int j = len - i - 1;
105: a[i] = a[j];
106: a[j] = t;
107: }
108: } else if (arg instanceof boolean[]) {
109: boolean[] a = (boolean[]) arg;
110: int len = a.length;
111: for (int i = 0; i < len / 2; i++) {
112: boolean t = a[i];
113: int j = len - i - 1;
114: a[i] = a[j];
115: a[j] = t;
116: }
117: } else if (arg instanceof List) {
118: Collections.reverse((List) arg);
119: } else if (arg instanceof Map) {
120: return reverseMap((Map) arg);
121: } else if (arg instanceof String) {
122: String str = (String) arg;
123: int len = str.length();
124: StringBuffer sbuf = new StringBuffer();
125: for (int i = 0; i < len; i++) {
126: sbuf.append(str.charAt(len - i - 1));
127: }
128: return sbuf.toString();
129: } else {
130: throw new IllegalArgumentException();
131: }
132: return arg;
133: }
134:
135: static Map reverseMap(Map map) {
136: Map output = new HashMap();
137: reverseMap(map, output);
138: return output;
139: }
140:
141: static void reverseMap(Map map, Map output) {
142: for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
143: Map.Entry entry = (Map.Entry) it.next();
144: Object key = entry.getKey();
145: Object value = entry.getValue();
146: List lst = (List) output.get(value);
147: if (lst == null) {
148: lst = new ArrayList();
149: output.put(value, lst);
150: }
151: lst.add(key);
152: }
153: }
154:
155: public String toString() {
156: return "function reverse(arrayOrListOrMap)";
157: }
158: }
|