/src/com/google/appengine/datanucleus/PrimitiveArrays.java

http://datanucleus-appengine.googlecode.com/ · Java · 541 lines · 299 code · 76 blank · 166 comment · 36 complexity · 06f64b8718527a980e361f458edf4f48 MD5 · raw file

  1. /**********************************************************************
  2. Copyright (c) 2009 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. **********************************************************************/
  13. package com.google.appengine.datanucleus;
  14. import java.io.Serializable;
  15. import java.util.AbstractList;
  16. import java.util.Arrays;
  17. import java.util.Collection;
  18. import java.util.List;
  19. import java.util.ListIterator;
  20. import java.util.RandomAccess;
  21. /**
  22. * Static utility methods pertaining to arrays of Java primitives.
  23. *
  24. * @author DJ Lee
  25. * @author Michael Parker
  26. * @author Jared Levy
  27. */
  28. public final class PrimitiveArrays {
  29. private PrimitiveArrays() {}
  30. /*
  31. * In the following *Array classes, the overrides of contains(), indexOf(),
  32. * lastIndexOf(), equals(), hashCode(), and toString() are for performance
  33. * reasons only. The list generated by subList(), which doesn't have those
  34. * overrides, works correctly.
  35. *
  36. * TODO: Have subList return a class that overrides those methods.
  37. */
  38. private abstract static class PrimitiveArray<E> extends AbstractList<E>
  39. implements RandomAccess, Serializable {
  40. @Override public boolean contains(Object o) {
  41. return (o != null) && super.contains(o);
  42. }
  43. @Override public int indexOf(Object o) {
  44. return (o == null) ? -1 : super.indexOf(o);
  45. }
  46. @Override public int lastIndexOf(Object o) {
  47. return (o == null) ? -1 : super.lastIndexOf(o);
  48. }
  49. }
  50. // TODO: Enhance the to*Array methods to support concurrent collections whose
  51. // size changes while the method is running.
  52. /**
  53. * Converts a collection of {@code Short} instances into a new array of
  54. * primitive shorts.
  55. *
  56. * @param collection a collection of {@code Short} objects
  57. * @return an array containing the same shorts as {@code collection}, in the
  58. * same order, converted to primitives
  59. * @throws NullPointerException if {@code collection} or any of its elements
  60. * are null
  61. */
  62. public static short[] toShortArray(Collection<Short> collection) {
  63. int counter = 0;
  64. short[] array = new short[collection.size()];
  65. for (Short x : collection) {
  66. array[counter++] = x;
  67. }
  68. return array;
  69. }
  70. /**
  71. * Returns a fixed-size list backed by the specified array, similar to {@link
  72. * Arrays#asList}. The only additional restriction of the returned list is
  73. * that {@code null} cannot be assigned to any element via {@link
  74. * List#set(int,Object)} or {@link ListIterator#set}.
  75. *
  76. * @param backingArray the array to back the list
  77. * @return a list view of the array
  78. */
  79. public static List<Short> asList(short[] backingArray) {
  80. return new ShortArray(backingArray);
  81. }
  82. private static <T> T checkNotNull(T obj) {
  83. if (obj == null) {
  84. throw new NullPointerException();
  85. }
  86. return obj;
  87. }
  88. private static class ShortArray extends PrimitiveArray<Short> {
  89. final short[] array;
  90. ShortArray(short[] array) {
  91. this.array = checkNotNull(array);
  92. }
  93. @Override public Short get(int index) {
  94. return array[index];
  95. }
  96. @Override public int size() {
  97. return array.length;
  98. }
  99. @Override public Short set(int index, Short element) {
  100. Short oldValue = array[index];
  101. array[index] = element;
  102. return oldValue;
  103. }
  104. @Override public boolean equals(Object o) {
  105. if (this == o) {
  106. return true;
  107. } else if (o instanceof ShortArray) {
  108. ShortArray otherShortArray = (ShortArray) o;
  109. return Arrays.equals(array, otherShortArray.array);
  110. }
  111. return super.equals(o);
  112. }
  113. @Override public int hashCode() {
  114. return Arrays.hashCode(array);
  115. }
  116. @Override public String toString() {
  117. return Arrays.toString(array);
  118. }
  119. private static final long serialVersionUID = 0;
  120. }
  121. /**
  122. * Returns a fixed-size list backed by the specified array, similar to {@link
  123. * Arrays#asList}. The only additional restriction of the returned list is
  124. * that {@code null} cannot be assigned to any element via {@link
  125. * List#set(int,Object)} or {@link ListIterator#set}.
  126. *
  127. * @param backingArray the array to back the list
  128. * @return a list view of the array
  129. * @deprecated use {@link Ints#asList(int[])} (inline this method call)
  130. */
  131. @Deprecated public static List<Integer> asList(int[] backingArray) {
  132. return Ints.asList(backingArray);
  133. }
  134. /**
  135. * Converts a collection of {@code Double} instances into a new array of
  136. * primitive doubles.
  137. *
  138. * @param collection a collection of {@code Double} objects
  139. * @return an array containing the same doubles as {@code collection}, in the
  140. * same order, converted to primitives
  141. * @throws NullPointerException if {@code collection} or any of its elements
  142. * are null
  143. */
  144. public static double[] toDoubleArray(Collection<Double> collection) {
  145. int counter = 0;
  146. double[] array = new double[collection.size()];
  147. for (Double x : collection) {
  148. array[counter++] = x;
  149. }
  150. return array;
  151. }
  152. /**
  153. * Returns a fixed-size list backed by the specified array, similar to {@link
  154. * Arrays#asList}. The only additional restriction of the returned list is
  155. * that {@code null} cannot be assigned to any element via {@link
  156. * List#set(int,Object)} or {@link ListIterator#set}.
  157. *
  158. * @param backingArray the array to back the list
  159. * @return a list view of the array
  160. */
  161. public static List<Double> asList(double[] backingArray) {
  162. return new DoubleArray(backingArray);
  163. }
  164. private static class DoubleArray extends PrimitiveArray<Double> {
  165. final double[] array;
  166. DoubleArray(double[] array) {
  167. this.array = checkNotNull(array);
  168. }
  169. @Override public Double get(int index) {
  170. return array[index];
  171. }
  172. @Override public int size() {
  173. return array.length;
  174. }
  175. @Override public Double set(int index, Double element) {
  176. Double oldValue = array[index];
  177. array[index] = element;
  178. return oldValue;
  179. }
  180. @Override public boolean equals(Object o) {
  181. if (this == o) {
  182. return true;
  183. } else if (o instanceof DoubleArray) {
  184. DoubleArray otherDoubleArray = (DoubleArray) o;
  185. return Arrays.equals(array, otherDoubleArray.array);
  186. }
  187. return super.equals(o);
  188. }
  189. @Override public int hashCode() {
  190. return Arrays.hashCode(array);
  191. }
  192. @Override public String toString() {
  193. return Arrays.toString(array);
  194. }
  195. private static final long serialVersionUID = 0;
  196. }
  197. /**
  198. * Converts a collection of {@code Float} instances into a new array of
  199. * primitive floats.
  200. *
  201. * @param collection a collection of {@code float} objects
  202. * @return an array containing the same floats as {@code collection}, in the
  203. * same order, converted to primitives
  204. * @throws NullPointerException if {@code collection} or any of its elements
  205. * are null
  206. */
  207. public static float[] toFloatArray(Collection<Float> collection) {
  208. int counter = 0;
  209. float[] array = new float[collection.size()];
  210. for (Float x : collection) {
  211. array[counter++] = x;
  212. }
  213. return array;
  214. }
  215. /**
  216. * Returns a fixed-size list backed by the specified array, similar to {@link
  217. * Arrays#asList}. The only additional restriction of the returned list is
  218. * that {@code null} cannot be assigned to any element via {@link
  219. * List#set(int,Object)} or {@link ListIterator#set}.
  220. *
  221. * @param backingArray the array to back the list
  222. * @return a list view of the array
  223. */
  224. public static List<Float> asList(float[] backingArray) {
  225. return new FloatArray(backingArray);
  226. }
  227. private static class FloatArray extends PrimitiveArray<Float> {
  228. final float[] array;
  229. FloatArray(float[] array) {
  230. this.array = checkNotNull(array);
  231. }
  232. @Override public Float get(int index) {
  233. return array[index];
  234. }
  235. @Override public int size() {
  236. return array.length;
  237. }
  238. @Override public Float set(int index, Float element) {
  239. Float oldValue = array[index];
  240. array[index] = element;
  241. return oldValue;
  242. }
  243. @Override public boolean equals(Object o) {
  244. if (this == o) {
  245. return true;
  246. } else if (o instanceof FloatArray) {
  247. FloatArray otherFloatArray = (FloatArray) o;
  248. return Arrays.equals(array, otherFloatArray.array);
  249. }
  250. return super.equals(o);
  251. }
  252. @Override public int hashCode() {
  253. return Arrays.hashCode(array);
  254. }
  255. @Override public String toString() {
  256. return Arrays.toString(array);
  257. }
  258. private static final long serialVersionUID = 0;
  259. }
  260. /**
  261. * Returns a fixed-size list backed by the specified array, similar to {@link
  262. * Arrays#asList}. The only additional restriction of the returned list is
  263. * that {@code null} cannot be assigned to any element via {@link
  264. * List#set(int,Object)} or {@link ListIterator#set}.
  265. *
  266. * @param backingArray the array to back the list
  267. * @return a list view of the array
  268. * @deprecated use {@link Longs#asList(long[])} (inline this method call)
  269. */
  270. @Deprecated public static List<Long> asList(long[] backingArray) {
  271. return Longs.asList(backingArray);
  272. }
  273. /**
  274. * Converts a collection of {@code Character} instances into a new array of
  275. * primitive chars.
  276. *
  277. * @param collection a collection of {@code Character} objects
  278. * @return an array containing the same chars as {@code collection}, in the
  279. * same order, converted to primitives
  280. * @throws NullPointerException if {@code collection} or any of its elements
  281. * are null
  282. */
  283. public static char[] toCharArray(Collection<Character> collection) {
  284. int counter = 0;
  285. char[] array = new char[collection.size()];
  286. for (Character x : collection) {
  287. array[counter++] = x;
  288. }
  289. return array;
  290. }
  291. /**
  292. * Returns a fixed-size list backed by the specified array, similar to {@link
  293. * Arrays#asList}. The only additional restriction of the returned list is
  294. * that {@code null} cannot be assigned to any element via {@link
  295. * List#set(int,Object)} or {@link ListIterator#set}.
  296. *
  297. * @param backingArray the array to back the list
  298. * @return a list view of the array
  299. */
  300. public static List<Character> asList(char[] backingArray) {
  301. return new CharacterArray(backingArray);
  302. }
  303. private static class CharacterArray extends PrimitiveArray<Character> {
  304. final char[] array;
  305. CharacterArray(char[] array) {
  306. this.array = checkNotNull(array);
  307. }
  308. @Override public Character get(int index) {
  309. return array[index];
  310. }
  311. @Override public int size() {
  312. return array.length;
  313. }
  314. @Override public Character set(int index, Character element) {
  315. Character oldValue = array[index];
  316. array[index] = element;
  317. return oldValue;
  318. }
  319. @Override public boolean equals(Object o) {
  320. if (this == o) {
  321. return true;
  322. } else if (o instanceof CharacterArray) {
  323. CharacterArray otherCharArray = (CharacterArray) o;
  324. return Arrays.equals(array, otherCharArray.array);
  325. }
  326. return super.equals(o);
  327. }
  328. @Override public int hashCode() {
  329. return Arrays.hashCode(array);
  330. }
  331. @Override public String toString() {
  332. return Arrays.toString(array);
  333. }
  334. private static final long serialVersionUID = 0;
  335. }
  336. /**
  337. * Converts a collection of {@code Boolean} instances into a new array of
  338. * primitive booleans.
  339. *
  340. * @param collection a collection of {@code Booleans} objects
  341. * @return an array containing the same booleans as {@code collection}, in the
  342. * same order, converted to primitives
  343. * @throws NullPointerException if {@code collection} or any of its elements
  344. * are null
  345. */
  346. public static boolean[] toBooleanArray(Collection<Boolean> collection) {
  347. int counter = 0;
  348. boolean[] array = new boolean[collection.size()];
  349. for (Boolean x : collection) {
  350. array[counter++] = x;
  351. }
  352. return array;
  353. }
  354. /**
  355. * Returns a fixed-size list backed by the specified array, similar to {@link
  356. * Arrays#asList}. The only additional restriction of the returned list is
  357. * that {@code null} cannot be assigned to any element via {@link
  358. * List#set(int,Object)} or {@link ListIterator#set}.
  359. *
  360. * @param backingArray the array to back the list
  361. * @return a list view of the array
  362. */
  363. public static List<Boolean> asList(boolean[] backingArray) {
  364. return new BooleanArray(backingArray);
  365. }
  366. private static class BooleanArray extends PrimitiveArray<Boolean> {
  367. final boolean[] array;
  368. BooleanArray(boolean[] array) {
  369. this.array = checkNotNull(array);
  370. }
  371. @Override public Boolean get(int index) {
  372. return array[index];
  373. }
  374. @Override public int size() {
  375. return array.length;
  376. }
  377. @Override public Boolean set(int index, Boolean element) {
  378. Boolean oldValue = array[index];
  379. array[index] = element;
  380. return oldValue;
  381. }
  382. @Override public boolean equals(Object o) {
  383. if (this == o) {
  384. return true;
  385. } else if (o instanceof BooleanArray) {
  386. BooleanArray otherBoolArray = (BooleanArray) o;
  387. return Arrays.equals(array, otherBoolArray.array);
  388. }
  389. return super.equals(o);
  390. }
  391. @Override public int hashCode() {
  392. return Arrays.hashCode(array);
  393. }
  394. @Override public String toString() {
  395. return Arrays.toString(array);
  396. }
  397. private static final long serialVersionUID = 0;
  398. }
  399. /**
  400. * Converts a collection of {@code Byte} instances into a new array of
  401. * primitive bytes.
  402. *
  403. * @param collection a collection of {@code Byte} objects
  404. * @return an array containing the same bytes as {@code collection}, in the
  405. * same order, converted to primitives
  406. * @throws NullPointerException if {@code collection} or any of its elements
  407. * are null
  408. */
  409. public static byte[] toByteArray(Collection<Byte> collection) {
  410. int counter = 0;
  411. byte[] array = new byte[collection.size()];
  412. for (Byte x : collection) {
  413. array[counter++] = x;
  414. }
  415. return array;
  416. }
  417. /**
  418. * Returns a fixed-size list backed by the specified array, similar to {@link
  419. * Arrays#asList}. The only additional restriction of the returned list is
  420. * that {@code null} cannot be assigned to any element via {@link
  421. * List#set(int,Object)} or {@link ListIterator#set}.
  422. *
  423. * @param backingArray the array to back the list
  424. * @return a list view of the array
  425. */
  426. public static List<Byte> asList(byte[] backingArray) {
  427. return new ByteArray(backingArray);
  428. }
  429. private static class ByteArray extends PrimitiveArray<Byte> {
  430. final byte[] array;
  431. ByteArray(byte[] array) {
  432. this.array = checkNotNull(array);
  433. }
  434. @Override public Byte get(int index) {
  435. return array[index];
  436. }
  437. @Override public int size() {
  438. return array.length;
  439. }
  440. @Override public Byte set(int index, Byte element) {
  441. Byte oldValue = array[index];
  442. array[index] = element;
  443. return oldValue;
  444. }
  445. @Override public boolean equals(Object o) {
  446. if (this == o) {
  447. return true;
  448. } else if (o instanceof ByteArray) {
  449. ByteArray otherByteArray = (ByteArray) o;
  450. return Arrays.equals(array, otherByteArray.array);
  451. }
  452. return super.equals(o);
  453. }
  454. @Override public int hashCode() {
  455. return Arrays.hashCode(array);
  456. }
  457. @Override public String toString() {
  458. return Arrays.toString(array);
  459. }
  460. private static final long serialVersionUID = 0;
  461. }
  462. }