Source Code Cross Referenced for ArrayUtil.java in  » Database-Client » SQLMinus » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database Client » SQLMinus » util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package util;
002:
003:        import java.util.ArrayList;
004:        import java.util.Arrays;
005:        import java.util.ArrayList;
006:        import java.util.List;
007:        import java.util.Enumeration;
008:        import java.util.StringTokenizer;
009:
010:        /** contains static methods that operate on arrays or return arrays.
011:         * @author Rahul Kumar June 2001.
012:         * $Author: rahul_kumar $    $Id: ArrayUtil.java,v 1.1 2004/01/01 06:49:54 rahul_kumar Exp rahul $
013:         */
014:        public class ArrayUtil {
015:
016:            /** prepend a string to each element in an array
017:             */
018:            public static String[] prependString(String[] sarr, String text) {
019:                int len = sarr.length;
020:                for (int i = 0; i < len; i++) {
021:                    sarr[i] = text + sarr[i];
022:                }
023:                return sarr;
024:            }
025:
026:            public static List prependString(List sarr, String text) {
027:                int len = sarr.size();
028:                List v = new ArrayList(len);
029:                for (int i = 0; i < len; i++) {
030:                    v.add(text + sarr.get(i).toString());
031:                }
032:                return v;
033:            }
034:
035:            /** append a string to each element in an array
036:             */
037:            public static String[] appendString(String[] sarr, String text) {
038:                int len = sarr.length;
039:                for (int i = 0; i < len; i++) {
040:                    sarr[i] += text;
041:                }
042:                return sarr;
043:            }
044:
045:            /** enclose each element in an array with given strings.
046:             * Useful when surrounding a string with tags
047:             */
048:            public static String[] enclose(String[] sarr, String before,
049:                    String after) {
050:                int len = sarr.length;
051:                for (int i = 0; i < len; i++) {
052:                    sarr[i] = before + sarr[i] + after;
053:                }
054:                return sarr;
055:            }
056:
057:            public static String enclose(String sarr, String before,
058:                    String after) {
059:                return (before + sarr + after);
060:            }
061:
062:            /** remove zero length entries from the given array - principally created for
063:             * string arrays coming from JSP's. For other rare cases, i am also trimming the string 
064:             * and then checking its length.
065:             */
066:            public static String[] trim(String[] sarr) {
067:                int len = sarr.length;
068:                ArrayList alist = new ArrayList();
069:                for (int i = 0; i < len; i++) {
070:                    if (sarr[i].length() == 0 || sarr[i].trim().length() == 0)
071:                        continue;
072:                    alist.add(sarr[i]);
073:                }
074:
075:                // Return list as an array of strings
076:                String[] ret = new String[alist.size()];
077:                alist.toArray(ret);
078:                return ret;
079:            }
080:
081:            /** joins values of a string array with the given separator
082:             * into a string and returns the string.
083:             * Does not delimit the last value, obviously.
084:             */
085:            public static String join(String[] sarr, char csep) {
086:                int len = sarr.length;
087:                StringBuffer sbuf = new StringBuffer(len * 10);
088:                for (int i = 0; i < len; i++) {
089:                    if (i == len - 1)
090:                        sbuf.append(sarr[i]);
091:                    else
092:                        sbuf.append(sarr[i]).append(csep);
093:                }
094:                return sbuf.toString();
095:            }
096:
097:            public static String join(Object[] sarr, char csep) {
098:                int len = sarr.length;
099:                StringBuffer sbuf = new StringBuffer(len * 10);
100:                for (int i = 0; i < len; i++) {
101:                    if (i == len - 1)
102:                        sbuf.append(sarr[i].toString());
103:                    else
104:                        sbuf.append(sarr[i].toString()).append(csep);
105:                }
106:                return sbuf.toString();
107:            }
108:
109:            /** joins an arraylist. this should go into a new ListUtil
110:             */
111:            public static String join(List sarr, char csep) {
112:                String[] sa = toStringArray(sarr);
113:                return join(sa, csep);
114:            }
115:
116:            // i had earlier put this in BTSLUtil but then it vanished from there.
117:            // it may be safer here, although it doesnt take an array
118:            /** splits a given string on given character separator returning
119:             * an array of strings.
120:             * THis would be more efficient than using string tokenizer which
121:             * takes a String not char as argument.
122:             * @author Rahul Kumar June 17, 2001
123:             * How to deal with consecutive delimiters - i am returning blank
124:             * strings.
125:             */
126:            public static String[] split(String st, char sep) {
127:
128:                ArrayList alist = new ArrayList();
129:
130:                int len = st.length();
131:                int pos = 0;
132:                int fin = 0;
133:
134:                // while not end of string, and you can find a match
135:                while (pos < len && (fin = st.indexOf(sep, pos)) != -1) {
136:                    alist.add(st.substring(pos, fin));
137:                    pos = fin + 1;
138:                }
139:                // Push remainder if it's not empty
140:                String remainder = st.substring(pos);
141:                if (remainder.length() != 0) {
142:                    alist.add(remainder);
143:                }
144:
145:                // Return list as an array of strings
146:                String[] ret = new String[alist.size()];
147:                alist.toArray(ret);
148:                return ret;
149:            } // end of split
150:
151:            /** THIS WONT WORK AS EXPECTED SINCE ssep actually means each
152:             * character in ssep is a delim
153:             */
154:            public static String[] split(String st, String ssep) {
155:                StringTokenizer stok = new StringTokenizer(ssep);
156:                ArrayList v = new ArrayList(16);
157:                while (stok.hasMoreTokens()) {
158:                    v.add(stok.nextToken());
159:                }
160:                String[] sarr = new String[v.size()];
161:                v.toArray(sarr);
162:                return (String[]) sarr;
163:            }
164:
165:            /** returns the value at an index, a negative index denotes
166:             * indexes taken from end backwards (-1 means last but one),
167:             * An index higher than array size returns highest element,
168:             * A negative index larger than size will return element at 0.
169:             */
170:            public static Object get(Object a[], int index) {
171:                if (index > a.length - 1)
172:                    index = a.length - 1;
173:                if (index < 0) {
174:                    index = a.length + index;
175:                    if (index < 0)
176:                        index = 0;
177:                }
178:                return a[index];
179:            }
180:
181:            /** identical to get (for those used to ArrayList class methods).
182:             */
183:            public static Object elementAt(Object a[], int index) {
184:                return get(a, index);
185:            }
186:
187:            /** sequential search of array returning index if found, else -1.
188:             * Return values start from 0. 
189:             * For repeated searches on a large array, please use Arrays.sort() once
190:             * and then use binarySearch. However, that would work only if index
191:             * does not matter.
192:             * */
193:            public static int indexOf(Object a[], Object key) {
194:                for (int i = 0; i < a.length; i++) {
195:                    if (a[i].equals(key))
196:                        return i;
197:                }
198:                return -1;
199:            }
200:
201:            /** searches an array of a String that starts with the given key 
202:             * returning its index.
203:             */
204:            public static int indexOfStartsWith(String a[], String key) {
205:                for (int i = 0; i < a.length; i++) {
206:                    if (a[i].startsWith(key))
207:                        return i;
208:                }
209:                return -1;
210:            }
211:
212:            /** searches an array of Strings that ends with the given key 
213:             * returning its index.
214:             * e.g. EMPID, ['MGREMPID','NAME']
215:             */
216:            public static int indexOfEndsWith(String a[], String key) {
217:                for (int i = 0; i < a.length; i++) {
218:                    if (a[i].endsWith(key))
219:                        return i;
220:                }
221:                return -1;
222:            }
223:
224:            /** checks whether the key, ends with a string in the array given.
225:             * This is a reverse of the above, returning its index.
226:             * e.g. MGREMPID, ['EMPID','NAME']
227:             * RK added on 20031226 20:54:41
228:             */
229:            public static int indexOfEndsWith(String key, String a[]) {
230:                for (int i = 0; i < a.length; i++) {
231:                    if (key.endsWith(a[i]))
232:                        return i;
233:                }
234:                return -1;
235:            }
236:
237:            /** searches an array of Strings that includes the given key 
238:             * returning its index.
239:             */
240:            public static int indexOfIndexOf(String a[], String key) {
241:                for (int i = 0; i < a.length; i++) {
242:                    if (a[i].indexOf(key) != -1)
243:                        return i;
244:                }
245:                return -1;
246:            }
247:
248:            public static int indexOf(int a[], int key) {
249:                for (int i = 0; i < a.length; i++) {
250:                    if (a[i] == key)
251:                        return i;
252:                }
253:                return -1;
254:            }
255:
256:            /** for those bad days when you have to deal with an enumeration and you
257:             * sorely need to treat is as an array or Collection.
258:             */
259:            public static Object[] toArray(Enumeration e) {
260:                List v = new ArrayList();
261:                while (e.hasMoreElements()) {
262:                    v.add(e.nextElement());
263:                }
264:                return v.toArray();
265:            }
266:
267:            /** If you want to put the value into an array of some particular
268:             * type, pass that as a parameter. On the lines of ArrayList.toArray(Object[])
269:             */
270:            public static Object[] toArray(Enumeration e, Object[] a) {
271:                List v = new ArrayList();
272:                while (e.hasMoreElements()) {
273:                    v.add(e.nextElement());
274:                }
275:                return v.toArray(a);
276:            }
277:
278:            public static Object[] toSortedArray(Enumeration e) {
279:                Object[] result = (Object[]) ArrayUtil.toArray(e);
280:                Arrays.sort(result);
281:                return result;
282:            }
283:
284:            /** If you want to put the value into an array of some particular
285:             * type, pass that as a parameter. On the lines of ArrayList.toArray(Object[])
286:             */
287:            public static Object[] toSortedArray(Enumeration e, Object[] a) {
288:                Object[] result = (Object[]) ArrayUtil.toArray(e, a);
289:                Arrays.sort(result);
290:                return result;
291:            }
292:
293:            public static String[] toStringArray(List l) {
294:                String arr[] = new String[l.size()];
295:                return (String[]) l.toArray(arr);
296:            }
297:
298:            /** find a regex in the array and return its index, else -1. 
299:             * pattern should be enclosed in // with possible modifiers at end
300:             * e.g. /pattern/ OR /pattern/i  .
301:             */
302:            public static int indexOfRegex(String s[], String pattern) {
303:                for (int i = 0; i < s.length; i++) {
304:                    if (PerlWrapper.isMatchingRE(pattern, s[i]))
305:                        return i;
306:                }
307:                return -1;
308:            }
309:
310:        } // end of class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.