Guest User

Untitled

a guest
Jul 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package org.vaadin.addons.lazyquerycontainer;
  2.  
  3. import java.util.AbstractList;
  4. import java.util.RandomAccess;
  5.  
  6. public class NaturalNumbersList extends AbstractList<Integer> implements
  7. RandomAccess, java.io.Serializable {
  8. private static final long serialVersionUID = -2764017481108945198L;
  9. private final int size;
  10.  
  11. public NaturalNumbersList(int size) {
  12. this.size = size;
  13. }
  14.  
  15. public int size() {
  16. return size;
  17. }
  18.  
  19. public Integer[] toArray() {
  20. Integer[] array = new Integer[size];
  21. for (int i=0; i<size; i++)
  22. array[i] = i;
  23. return array;
  24. }
  25.  
  26. @SuppressWarnings("unchecked")
  27. public <T> T[] toArray(T[] a) {
  28. if (a.length < size)
  29. a = (T[]) new Integer[size];
  30. for (int i=0; i<size; i++)
  31. a[i] = (T)Integer.valueOf(i);
  32. if (a.length > size)
  33. a[size] = null;
  34. return a;
  35. }
  36.  
  37. public Integer get(int index) {
  38. if (index<0 || index>=size)
  39. throw new IndexOutOfBoundsException();
  40. return index;
  41. }
  42.  
  43. public Integer set(int index, Integer element) {
  44. throw new UnsupportedOperationException();
  45. }
  46.  
  47. public int indexOf(Object o) {
  48. if (o == null)
  49. return -1;
  50. if (o instanceof Integer){
  51. int i = (Integer) o;
  52. if (i<0 || i>= size)
  53. return -1;
  54. return i;
  55. }
  56. return -1;
  57. }
  58.  
  59. public boolean contains(Object o) {
  60. return indexOf(o) != -1;
  61. }
  62. }
Add Comment
Please, Sign In to add comment