001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.util;
019:
020: import static java.lang.reflect.Array.newInstance;
021: import java.util.*;
022:
023: public class ArrayTools {
024: public static int[] intTranspose(Object[] a) {
025: int[] b = new int[a.length];
026: for (int i = a.length - 1; i >= 0; i--) {
027: b[i] = Integer.parseInt(String.valueOf(a[i]));
028: }
029: return b;
030: }
031:
032: public static long[] longTranspose(Object[] a) {
033: long[] b = new long[a.length];
034: for (int i = a.length - 1; i >= 0; i--) {
035: b[i] = Long.parseLong(String.valueOf(a[i]));
036: }
037: return b;
038: }
039:
040: public static char[] charTranspose(Object[] a) {
041: char[] b = new char[a.length];
042: for (int i = a.length - 1; i >= 0; i--) {
043: b[i] = String.valueOf(a[i]).charAt(0);
044: }
045: return b;
046: }
047:
048: public static float[] floatTranspose(Object[] a) {
049: float[] b = new float[a.length];
050: for (int i = a.length - 1; i >= 0; i--) {
051: b[i] = Float.parseFloat(String.valueOf(a[i]));
052: }
053: return b;
054: }
055:
056: public static short[] shortTranspose(Object[] a) {
057: short[] b = new short[a.length];
058: for (int i = a.length - 1; i >= 0; i--) {
059: b[i] = Short.parseShort(String.valueOf(a[i]));
060: }
061:
062: return b;
063: }
064:
065: public static String[] stringTranspose(Object[] a) {
066: String[] b = new String[a.length];
067: for (int i = a.length - 1; i >= 0; i--) {
068: b[i] = String.valueOf(a[i]);
069: }
070: return b;
071: }
072:
073: public static String[] stringTranspose(int[] a) {
074: String[] b = new String[a.length];
075: for (int i = a.length - 1; i >= 0; i--) {
076: b[i] = String.valueOf(a[i]);
077: }
078: return b;
079: }
080:
081: public static String[] stringTranspose(long[] a) {
082: String[] b = new String[a.length];
083: for (int i = a.length - 1; i >= 0; i--) {
084: b[i] = String.valueOf(a[i]);
085: }
086: return b;
087: }
088:
089: public static String[] stringTranspose(short[] a) {
090: String[] b = new String[a.length];
091: for (int i = a.length - 1; i >= 0; i--) {
092: b[i] = String.valueOf(a[i]);
093: }
094: return b;
095: }
096:
097: public static String[] stringTranspose(boolean[] a) {
098: String[] b = new String[a.length];
099: for (int i = a.length - 1; i >= 0; i--) {
100: b[i] = String.valueOf(a[i]);
101: }
102: return b;
103: }
104:
105: public static String[] stringTranspose(double[] a) {
106: String[] b = new String[a.length];
107: for (int i = a.length - 1; i >= 0; i--) {
108: b[i] = String.valueOf(a[i]);
109: }
110: return b;
111: }
112:
113: public static String[] stringTranspose(char[] a) {
114: String[] b = new String[a.length];
115: for (int i = a.length - 1; i >= 0; i--) {
116: b[i] = String.valueOf(a[i]);
117: }
118: return b;
119: }
120:
121: public static String[] stringTranspose(Collection col) {
122: String[] b = new String[col.size()];
123: int i = 0;
124: for (Object o : col) {
125: b[i++] = String.valueOf(o);
126: }
127: return b;
128: }
129:
130: public static List listTranspose(int[] a) {
131: List b = new ArrayList(a.length);
132: for (int i = a.length - 1; i >= 0; i--) {
133: b.add(i, a[i]);
134: }
135: return b;
136: }
137:
138: public static List listTranspose(char[] a) {
139: List b = new ArrayList(a.length);
140: for (int i = a.length - 1; i >= 0; i--) {
141: b.add(i, a[i]);
142: }
143: return b;
144: }
145:
146: public static List listTranspose(long[] a) {
147: List b = new ArrayList(a.length);
148: for (int i = a.length - 1; i >= 0; i--) {
149: b.add(i, a[i]);
150: }
151: return b;
152: }
153:
154: public static List listTranspose(boolean[] a) {
155: List b = new ArrayList(a.length);
156: for (int i = a.length - 1; i >= 0; i--) {
157: b.add(i, a[i]);
158: }
159: return b;
160: }
161:
162: public static List listTranspose(short[] a) {
163: List b = new ArrayList(a.length);
164: for (int i = a.length - 1; i >= 0; i--) {
165: b.add(i, a[i]);
166: }
167: return b;
168: }
169:
170: public static List listTranspose(double[] a) {
171: List b = new ArrayList(a.length);
172: for (int i = a.length - 1; i >= 0; i--) {
173: b.add(i, a[i]);
174: }
175: return b;
176: }
177:
178: public static List listTranspose(String[] a) {
179: if (a == null)
180: return new ArrayList(0);
181:
182: List b = new ArrayList(a.length);
183: for (int i = a.length - 1; i >= 0; i--) {
184: b.add(i, a[i]);
185: }
186: return b;
187: }
188:
189: public static Set setTranspose(String[] a) {
190: if (a == null)
191: return new HashSet(0);
192:
193: Set b = new HashSet(a.length * 2);
194: for (String c : a) {
195: b.add(c);
196: }
197: return b;
198: }
199:
200: public static <T> T[] collectionTranspose(Collection col,
201: Class<T> type) {
202: //noinspection unchecked
203: T[] a = (T[]) newInstance(type, col.size());
204:
205: int i = 0;
206: for (Object item : col) {
207: a[i++] = (T) item;
208: }
209:
210: return a;
211: }
212:
213: public static int findFirst(char c, char[] array) {
214: for (int i = 0; i < array.length; i++) {
215: if (array[i] == c)
216: return i;
217: }
218: return -1;
219: }
220:
221: public static int findLast(char c, char[] array) {
222: for (int i = array.length - 1; i >= 0; i--) {
223: if (array[i] == c)
224: return i;
225: }
226: return -1;
227: }
228:
229: }
|