/android/Android(LGame-0.3.2&LAE-1.1)/LAE-1.1(Canvas)/src/core/org/loon/framework/android/game/utils/CollectionUtils.java

http://loon-simple.googlecode.com/ · Java · 365 lines · 154 code · 30 blank · 181 comment · 17 complexity · 40bf065b5de61251dbd4e3cbcbcf6424 MD5 · raw file

  1. package org.loon.framework.android.game.utils;
  2. import java.lang.reflect.Array;
  3. import java.util.NoSuchElementException;
  4. /**
  5. *
  6. * Copyright 2008 - 2009
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  9. * use this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  17. * License for the specific language governing permissions and limitations under
  18. * the License.
  19. *
  20. * @project loonframework
  21. * @author chenpeng
  22. * @email?ceponline@yahoo.com.cn
  23. * @version 0.1.1
  24. */
  25. final public class CollectionUtils {
  26. final static public int INITIAL_CAPACITY = 10;
  27. protected CollectionUtils() {
  28. super();
  29. }
  30. /**
  31. * ??????????????????
  32. *
  33. * @param array
  34. * @param obj
  35. * @return
  36. */
  37. public static int indexOf(Object[] array, Object obj) {
  38. for (int i = 0; i < array.length; ++i) {
  39. if (obj == array[i]) {
  40. return i;
  41. }
  42. }
  43. throw new NoSuchElementException("" + obj);
  44. }
  45. /**
  46. * ????2????HashCode
  47. *
  48. * @param arrays
  49. * @return
  50. */
  51. public static int hashCode(int[][] arrays) {
  52. if (arrays == null) {
  53. return 0;
  54. }
  55. int result = 1;
  56. int h = arrays.length;
  57. int w = arrays[0].length;
  58. int value = 0;
  59. for (int i = 0; i < h; i++) {
  60. for (int j = 0; j < w; j++) {
  61. value = arrays[i][j];
  62. int elementHash = (value ^ (value >>> 32));
  63. result = 31 * result + elementHash;
  64. }
  65. }
  66. return result;
  67. }
  68. /**
  69. * ??????
  70. *
  71. * @param obj
  72. * @param i
  73. * @param flag
  74. * @return
  75. */
  76. public static Object expand(Object obj, int i, boolean flag) {
  77. int j = Array.getLength(obj);
  78. Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j
  79. + i);
  80. System.arraycopy(obj, 0, obj1, flag ? 0 : i, j);
  81. return obj1;
  82. }
  83. /**
  84. * ??????
  85. *
  86. * @param obj
  87. * @param size
  88. * @return
  89. */
  90. public static Object expand(Object obj, int size) {
  91. return expand(obj, size, true);
  92. }
  93. /**
  94. * ??????
  95. *
  96. * @param obj
  97. * @param size
  98. * @param flag
  99. * @param class1
  100. * @return
  101. */
  102. public static Object expand(Object obj, int size, boolean flag,
  103. Class<?> class1) {
  104. if (obj == null) {
  105. return Array.newInstance(class1, 1);
  106. } else {
  107. return expand(obj, size, flag);
  108. }
  109. }
  110. /**
  111. * ??????????
  112. *
  113. * @param obj
  114. * @param size
  115. * @return
  116. */
  117. public static Object cut(Object obj, int size) {
  118. int j;
  119. if ((j = Array.getLength(obj)) == 1) {
  120. return Array.newInstance(obj.getClass().getComponentType(), 0);
  121. }
  122. int k;
  123. if ((k = j - size - 1) > 0) {
  124. System.arraycopy(obj, size + 1, obj, size, k);
  125. }
  126. j--;
  127. Object obj1 = Array.newInstance(obj.getClass().getComponentType(), j);
  128. System.arraycopy(obj, 0, obj1, 0, j);
  129. return obj1;
  130. }
  131. /**
  132. * copy?????????
  133. *
  134. * @param src
  135. * @return
  136. */
  137. public static Object copyOf(Object src) {
  138. int srcLength = Array.getLength(src);
  139. Class<?> srcComponentType = src.getClass().getComponentType();
  140. Object dest = Array.newInstance(srcComponentType, srcLength);
  141. if (srcComponentType.isArray()) {
  142. for (int i = 0; i < Array.getLength(src); i++) {
  143. Array.set(dest, i, copyOf(Array.get(src, i)));
  144. }
  145. } else {
  146. System.arraycopy(src, 0, dest, 0, srcLength);
  147. }
  148. return dest;
  149. }
  150. /**
  151. * copy?????????
  152. *
  153. * @param obj
  154. * @return
  155. */
  156. public static int[][] copyOf(int[][] obj) {
  157. int size = obj.length;
  158. int[][] copy = new int[size][];
  159. for (int i = 0; i < size; i++) {
  160. int len = obj[i].length;
  161. int[] res = new int[len];
  162. System.arraycopy(obj[i], 0, res, 0, len);
  163. copy[i] = res;
  164. }
  165. return copy;
  166. }
  167. /**
  168. * copy?????????
  169. *
  170. * @param obj
  171. * @return
  172. */
  173. public static String[] copyOf(String[] obj) {
  174. return copyOf(obj, obj.length);
  175. }
  176. /**
  177. * copy?????????
  178. *
  179. * @param obj
  180. * @param newSize
  181. * @return
  182. */
  183. public static String[] copyOf(String[] obj, int newSize) {
  184. String tempArr[] = new String[newSize];
  185. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  186. return tempArr;
  187. }
  188. /**
  189. * copy?????????
  190. *
  191. * @param obj
  192. * @return
  193. */
  194. public static int[] copyOf(int[] obj) {
  195. return copyOf(obj, obj.length);
  196. }
  197. /**
  198. * copy?????????
  199. *
  200. * @param obj
  201. * @param newSize
  202. * @return
  203. */
  204. public static int[] copyOf(int[] obj, int newSize) {
  205. int tempArr[] = new int[newSize];
  206. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  207. return tempArr;
  208. }
  209. /**
  210. * copy?????????
  211. *
  212. * @param obj
  213. * @return
  214. */
  215. public static double[] copyOf(double[] obj) {
  216. return copyOf(obj, obj.length);
  217. }
  218. /**
  219. * copy?????????
  220. *
  221. * @param obj
  222. * @param newSize
  223. * @return
  224. */
  225. public static double[] copyOf(double[] obj, int newSize) {
  226. double tempArr[] = new double[newSize];
  227. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  228. return tempArr;
  229. }
  230. /**
  231. * copy?????????
  232. *
  233. * @param obj
  234. * @return
  235. */
  236. public static float[] copyOf(float[] obj) {
  237. return copyOf(obj, obj.length);
  238. }
  239. /**
  240. * copy?????????
  241. *
  242. * @param obj
  243. * @param newSize
  244. * @return
  245. */
  246. public static float[] copyOf(float[] obj, int newSize) {
  247. float tempArr[] = new float[newSize];
  248. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  249. return tempArr;
  250. }
  251. /**
  252. * copy?????????
  253. *
  254. * @param obj
  255. * @return
  256. */
  257. public static byte[] copyOf(byte[] obj) {
  258. return copyOf(obj, obj.length);
  259. }
  260. /**
  261. * copy?????????
  262. *
  263. * @param obj
  264. * @param newSize
  265. * @return
  266. */
  267. public static byte[] copyOf(byte[] obj, int newSize) {
  268. byte tempArr[] = new byte[newSize];
  269. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  270. return tempArr;
  271. }
  272. /**
  273. * copy?????????
  274. *
  275. * @param obj
  276. * @return
  277. */
  278. public static char[] copyOf(char[] obj) {
  279. return copyOf(obj, obj.length);
  280. }
  281. /**
  282. * copy?????????
  283. *
  284. * @param obj
  285. * @param newSize
  286. * @return
  287. */
  288. public static char[] copyOf(char[] obj, int newSize) {
  289. char tempArr[] = new char[newSize];
  290. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  291. return tempArr;
  292. }
  293. /**
  294. * copy?????????
  295. *
  296. * @param obj
  297. * @return
  298. */
  299. public static long[] copyOf(long[] obj) {
  300. return copyOf(obj, obj.length);
  301. }
  302. /**
  303. * copy?????????
  304. *
  305. * @param obj
  306. * @param newSize
  307. * @return
  308. */
  309. public static long[] copyOf(long[] obj, int newSize) {
  310. long tempArr[] = new long[newSize];
  311. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  312. return tempArr;
  313. }
  314. /**
  315. * copy?????????
  316. *
  317. * @param obj
  318. * @return
  319. */
  320. public static boolean[] copyOf(boolean[] obj) {
  321. return copyOf(obj, obj.length);
  322. }
  323. /**
  324. * copy?????????
  325. *
  326. * @param obj
  327. * @param newSize
  328. * @return
  329. */
  330. public static boolean[] copyOf(boolean[] obj, int newSize) {
  331. boolean tempArr[] = new boolean[newSize];
  332. System.arraycopy(obj, 0, tempArr, 0, Math.min(obj.length, newSize));
  333. return tempArr;
  334. }
  335. }