1 package com.ctc.wstx.util;
2
3 import java.lang.reflect.Array;
4 import java.util.*;
5
6 import org.codehaus.stax2.ri.SingletonIterator;
7
8 public final class DataUtil
9 {
10 final static char[] EMPTY_CHAR_ARRAY = new char[0];
11
12 final static Long MAX_LONG = Long.valueOf(Long.MAX_VALUE);
13
14
15 private final static class EI implements Iterator<Object>
16 {
17 public final static Iterator<?> sInstance = new EI();
18
19 @SuppressWarnings("unchecked")
20 public static <T> Iterator<T> getInstance() { return (Iterator<T>) sInstance; }
21
22 @Override
23 public boolean hasNext() { return false; }
24
25 @Override
26 public Object next() {
27 throw new java.util.NoSuchElementException();
28 }
29
30 @Override
31 public void remove() {
32 throw new IllegalStateException();
33 }
34 }
35
36 private DataUtil() { }
37
38
43
44 public static char[] getEmptyCharArray() {
45 return EMPTY_CHAR_ARRAY;
46 }
47
48
49 public static Integer Integer(int i) {
50 return Integer.valueOf(i);
51 }
52
53
58
59 public static <T> Iterator<T> singletonIterator(T item) {
60
61
62 return SingletonIterator.create(item);
63 }
64
65 public static <T> Iterator<T> emptyIterator() {
66
67 return EI.getInstance();
68 }
69
70
75
76
83 public static <T> boolean anyValuesInCommon(Collection<T> c1, Collection<T> c2)
84 {
85
86 if (c1.size() > c2.size()) {
87 Collection<T> tmp = c1;
88 c1 = c2;
89 c2 = tmp;
90 }
91 Iterator<T> it = c1.iterator();
92 while (it.hasNext()) {
93 if (c2.contains(it.next())) {
94 return true;
95 }
96 }
97 return false;
98 }
99
100 final static String NO_TYPE = "Illegal to pass null; can not determine component type";
101
102 public static Object growArrayBy50Pct(Object arr)
103 {
104 if (arr == null) {
105 throw new IllegalArgumentException(NO_TYPE);
106 }
107 Object old = arr;
108 int len = Array.getLength(arr);
109 arr = Array.newInstance(arr.getClass().getComponentType(), len + (len >> 1));
110 System.arraycopy(old, 0, arr, 0, len);
111 return arr;
112 }
113
114
118 public static Object growArrayToAtLeast(Object arr, int minLen)
119 {
120 if (arr == null) {
121 throw new IllegalArgumentException(NO_TYPE);
122 }
123 Object old = arr;
124 int oldLen = Array.getLength(arr);
125 int newLen = oldLen + ((oldLen + 1) >> 1);
126 if (newLen < minLen) {
127 newLen = minLen;
128 }
129 arr = Array.newInstance(arr.getClass().getComponentType(), newLen);
130 System.arraycopy(old, 0, arr, 0, oldLen);
131 return arr;
132 }
133
134 public static String[] growArrayBy(String[] arr, int more)
135 {
136 if (arr == null) {
137 return new String[more];
138 }
139 return Arrays.copyOf(arr, arr.length + more);
140 }
141
142 public static int[] growArrayBy(int[] arr, int more)
143 {
144 if (arr == null) {
145 return new int[more];
146 }
147 return Arrays.copyOf(arr, arr.length + more);
148 }
149 }
150