PageRenderTime 39ms CodeModel.GetById 17ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

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