Source Code Cross Referenced for ArrayHelper.java in  » Database-ORM » hibernate » org » hibernate » 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 ORM » hibernate » org.hibernate.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //$Id: ArrayHelper.java 7546 2005-07-19 18:17:15Z oneovthafew $
002:        package org.hibernate.util;
003:
004:        import java.lang.reflect.Array;
005:        import java.util.ArrayList;
006:        import java.util.Arrays;
007:        import java.util.Collection;
008:        import java.util.Iterator;
009:        import java.util.List;
010:
011:        import org.hibernate.LockMode;
012:        import org.hibernate.type.Type;
013:
014:        public final class ArrayHelper {
015:
016:            /*public static boolean contains(Object[] array, Object object) {
017:            	for ( int i=0; i<array.length; i++ ) {
018:            		if ( array[i].equals(object) ) return true;
019:            	}
020:            	return false;
021:            }*/
022:
023:            public static int indexOf(Object[] array, Object object) {
024:                for (int i = 0; i < array.length; i++) {
025:                    if (array[i].equals(object))
026:                        return i;
027:                }
028:                return -1;
029:            }
030:
031:            /*public static Object[] clone(Class elementClass, Object[] array) {
032:            	Object[] result = (Object[]) Array.newInstance( elementClass, array.length );
033:            	System.arraycopy(array, 0, result, 0, array.length);
034:            	return result;
035:            }*/
036:
037:            public static String[] toStringArray(Object[] objects) {
038:                int length = objects.length;
039:                String[] result = new String[length];
040:                for (int i = 0; i < length; i++) {
041:                    result[i] = objects[i].toString();
042:                }
043:                return result;
044:            }
045:
046:            public static String[] fillArray(String value, int length) {
047:                String[] result = new String[length];
048:                Arrays.fill(result, value);
049:                return result;
050:            }
051:
052:            public static int[] fillArray(int value, int length) {
053:                int[] result = new int[length];
054:                Arrays.fill(result, value);
055:                return result;
056:            }
057:
058:            public static LockMode[] fillArray(LockMode lockMode, int length) {
059:                LockMode[] array = new LockMode[length];
060:                Arrays.fill(array, lockMode);
061:                return array;
062:            }
063:
064:            public static String[] toStringArray(Collection coll) {
065:                return (String[]) coll.toArray(EMPTY_STRING_ARRAY);
066:            }
067:
068:            public static String[][] to2DStringArray(Collection coll) {
069:                return (String[][]) coll.toArray(new String[coll.size()][]);
070:            }
071:
072:            public static int[][] to2DIntArray(Collection coll) {
073:                return (int[][]) coll.toArray(new int[coll.size()][]);
074:            }
075:
076:            public static Type[] toTypeArray(Collection coll) {
077:                return (Type[]) coll.toArray(EMPTY_TYPE_ARRAY);
078:            }
079:
080:            public static int[] toIntArray(Collection coll) {
081:                Iterator iter = coll.iterator();
082:                int[] arr = new int[coll.size()];
083:                int i = 0;
084:                while (iter.hasNext()) {
085:                    arr[i++] = ((Integer) iter.next()).intValue();
086:                }
087:                return arr;
088:            }
089:
090:            public static boolean[] toBooleanArray(Collection coll) {
091:                Iterator iter = coll.iterator();
092:                boolean[] arr = new boolean[coll.size()];
093:                int i = 0;
094:                while (iter.hasNext()) {
095:                    arr[i++] = ((Boolean) iter.next()).booleanValue();
096:                }
097:                return arr;
098:            }
099:
100:            public static Object[] typecast(Object[] array, Object[] to) {
101:                return java.util.Arrays.asList(array).toArray(to);
102:            }
103:
104:            //Arrays.asList doesn't do primitive arrays
105:            public static List toList(Object array) {
106:                if (array instanceof  Object[])
107:                    return Arrays.asList((Object[]) array); //faster?
108:                int size = Array.getLength(array);
109:                ArrayList list = new ArrayList(size);
110:                for (int i = 0; i < size; i++) {
111:                    list.add(Array.get(array, i));
112:                }
113:                return list;
114:            }
115:
116:            public static String[] slice(String[] strings, int begin, int length) {
117:                String[] result = new String[length];
118:                for (int i = 0; i < length; i++) {
119:                    result[i] = strings[begin + i];
120:                }
121:                return result;
122:            }
123:
124:            public static Object[] slice(Object[] objects, int begin, int length) {
125:                Object[] result = new Object[length];
126:                for (int i = 0; i < length; i++) {
127:                    result[i] = objects[begin + i];
128:                }
129:                return result;
130:            }
131:
132:            public static List toList(Iterator iter) {
133:                List list = new ArrayList();
134:                while (iter.hasNext()) {
135:                    list.add(iter.next());
136:                }
137:                return list;
138:            }
139:
140:            public static String[] join(String[] x, String[] y) {
141:                String[] result = new String[x.length + y.length];
142:                for (int i = 0; i < x.length; i++)
143:                    result[i] = x[i];
144:                for (int i = 0; i < y.length; i++)
145:                    result[i + x.length] = y[i];
146:                return result;
147:            }
148:
149:            public static String[] join(String[] x, String[] y, boolean[] use) {
150:                String[] result = new String[x.length + countTrue(use)];
151:                for (int i = 0; i < x.length; i++)
152:                    result[i] = x[i];
153:                int k = x.length;
154:                for (int i = 0; i < y.length; i++) {
155:                    if (use[i])
156:                        result[k++] = y[i];
157:                }
158:                return result;
159:            }
160:
161:            public static int[] join(int[] x, int[] y) {
162:                int[] result = new int[x.length + y.length];
163:                for (int i = 0; i < x.length; i++)
164:                    result[i] = x[i];
165:                for (int i = 0; i < y.length; i++)
166:                    result[i + x.length] = y[i];
167:                return result;
168:            }
169:
170:            public static final boolean[] TRUE = { true };
171:            public static final boolean[] FALSE = { false };
172:
173:            private ArrayHelper() {
174:            }
175:
176:            public static String toString(Object[] array) {
177:                StringBuffer sb = new StringBuffer();
178:                sb.append("[");
179:                for (int i = 0; i < array.length; i++) {
180:                    sb.append(array[i]);
181:                    if (i < array.length - 1)
182:                        sb.append(",");
183:                }
184:                sb.append("]");
185:                return sb.toString();
186:            }
187:
188:            public static boolean isAllNegative(int[] array) {
189:                for (int i = 0; i < array.length; i++) {
190:                    if (array[i] >= 0)
191:                        return false;
192:                }
193:                return true;
194:            }
195:
196:            public static boolean isAllTrue(boolean[] array) {
197:                for (int i = 0; i < array.length; i++) {
198:                    if (!array[i])
199:                        return false;
200:                }
201:                return true;
202:            }
203:
204:            public static int countTrue(boolean[] array) {
205:                int result = 0;
206:                for (int i = 0; i < array.length; i++) {
207:                    if (array[i])
208:                        result++;
209:                }
210:                return result;
211:            }
212:
213:            /*public static int countFalse(boolean[] array) {
214:            	int result=0;
215:            	for ( int i=0; i<array.length; i++ ) {
216:            		if ( !array[i] ) result++;
217:            	}
218:            	return result;
219:            }*/
220:
221:            public static boolean isAllFalse(boolean[] array) {
222:                for (int i = 0; i < array.length; i++) {
223:                    if (array[i])
224:                        return false;
225:                }
226:                return true;
227:            }
228:
229:            public static void addAll(Collection collection, Object[] array) {
230:                for (int i = 0; i < array.length; i++) {
231:                    collection.add(array[i]);
232:                }
233:            }
234:
235:            public static final String[] EMPTY_STRING_ARRAY = {};
236:            public static final int[] EMPTY_INT_ARRAY = {};
237:            public static final boolean[] EMPTY_BOOLEAN_ARRAY = {};
238:            public static final Class[] EMPTY_CLASS_ARRAY = {};
239:            public static final Object[] EMPTY_OBJECT_ARRAY = {};
240:            public static final Type[] EMPTY_TYPE_ARRAY = {};
241:
242:            public static int[] getBatchSizes(int maxBatchSize) {
243:                int batchSize = maxBatchSize;
244:                int n = 1;
245:                while (batchSize > 1) {
246:                    batchSize = getNextBatchSize(batchSize);
247:                    n++;
248:                }
249:                int[] result = new int[n];
250:                batchSize = maxBatchSize;
251:                for (int i = 0; i < n; i++) {
252:                    result[i] = batchSize;
253:                    batchSize = getNextBatchSize(batchSize);
254:                }
255:                return result;
256:            }
257:
258:            private static int getNextBatchSize(int batchSize) {
259:                if (batchSize <= 10) {
260:                    return batchSize - 1; //allow 9,8,7,6,5,4,3,2,1
261:                } else if (batchSize / 2 < 10) {
262:                    return 10;
263:                } else {
264:                    return batchSize / 2;
265:                }
266:            }
267:
268:            private static int SEED = 23;
269:            private static int PRIME_NUMER = 37;
270:
271:            /**
272:             * calculate the array hash (only the first level)
273:             */
274:            public static int hash(Object[] array) {
275:                int length = array.length;
276:                int seed = SEED;
277:                for (int index = 0; index < length; index++) {
278:                    seed = hash(seed, array[index] == null ? 0 : array[index]
279:                            .hashCode());
280:                }
281:                return seed;
282:            }
283:
284:            /**
285:             * calculate the array hash (only the first level)
286:             */
287:            public static int hash(char[] array) {
288:                int length = array.length;
289:                int seed = SEED;
290:                for (int index = 0; index < length; index++) {
291:                    seed = hash(seed, (int) array[index]);
292:                }
293:                return seed;
294:            }
295:
296:            /**
297:             * calculate the array hash (only the first level)
298:             */
299:            public static int hash(byte[] bytes) {
300:                int length = bytes.length;
301:                int seed = SEED;
302:                for (int index = 0; index < length; index++) {
303:                    seed = hash(seed, (int) bytes[index]);
304:                }
305:                return seed;
306:            }
307:
308:            private static int hash(int seed, int i) {
309:                return PRIME_NUMER * seed + i;
310:            }
311:
312:            /**
313:             * Compare 2 arrays only at the first level
314:             */
315:            public static boolean isEquals(Object[] o1, Object[] o2) {
316:                if (o1 == o2)
317:                    return true;
318:                if (o1 == null || o2 == null)
319:                    return false;
320:                int length = o1.length;
321:                if (length != o2.length)
322:                    return false;
323:                for (int index = 0; index < length; index++) {
324:                    if (!o1[index].equals(o2[index]))
325:                        return false;
326:                }
327:                return true;
328:            }
329:
330:            /**
331:             * Compare 2 arrays only at the first level
332:             */
333:            public static boolean isEquals(char[] o1, char[] o2) {
334:                if (o1 == o2)
335:                    return true;
336:                if (o1 == null || o2 == null)
337:                    return false;
338:                int length = o1.length;
339:                if (length != o2.length)
340:                    return false;
341:                for (int index = 0; index < length; index++) {
342:                    if (!(o1[index] == o2[index]))
343:                        return false;
344:                }
345:                return true;
346:            }
347:
348:            /**
349:             * Compare 2 arrays only at the first level
350:             */
351:            public static boolean isEquals(byte[] b1, byte[] b2) {
352:                if (b1 == b2)
353:                    return true;
354:                if (b1 == null || b2 == null)
355:                    return false;
356:                int length = b1.length;
357:                if (length != b2.length)
358:                    return false;
359:                for (int index = 0; index < length; index++) {
360:                    if (!(b1[index] == b2[index]))
361:                        return false;
362:                }
363:                return true;
364:            }
365:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.