1 19 package bak.pcj; 20 21 import bak.pcj.util.Exceptions; 22 23 39 public abstract class AbstractShortCollection implements ShortCollection { 40 41 42 protected AbstractShortCollection() { } 43 44 50 public boolean add(short v) 51 { Exceptions.unsupported("add"); return false; } 52 53 public boolean addAll(ShortCollection c) { 54 ShortIterator i = c.iterator(); boolean result = false; 56 while (i.hasNext()) 57 result = result | add(i.next()); 58 return result; 59 } 60 61 public void clear() { 62 ShortIterator i = iterator(); 63 while (i.hasNext()) { 64 i.next(); 65 i.remove(); 66 } 67 } 68 69 public boolean contains(short v) { 70 ShortIterator i = iterator(); 71 while (i.hasNext()) 72 if (i.next() == v) 73 return true; 74 return false; 75 } 76 77 public boolean containsAll(ShortCollection c) { 78 ShortIterator i = c.iterator(); while (i.hasNext()) 80 if (!contains(i.next())) 81 return false; 82 return true; 83 } 84 85 public boolean isEmpty() 86 { return size() == 0; } 87 88 public boolean remove(short v) { 89 ShortIterator i = iterator(); 90 boolean result = false; 91 while (i.hasNext()) { 92 if (i.next() == v) { 93 i.remove(); 94 result = true; 95 break; 96 } 97 } 98 return result; 99 } 100 101 public boolean removeAll(ShortCollection c) { 102 if (c == null) 103 Exceptions.nullArgument("collection"); 104 ShortIterator i = iterator(); 105 boolean result = false; 106 while (i.hasNext()) { 107 if (c.contains(i.next())) { 108 i.remove(); 109 result = true; 110 } 111 } 112 return result; 113 } 114 115 public boolean retainAll(ShortCollection c) { 116 if (c == null) 117 Exceptions.nullArgument("collection"); 118 ShortIterator i = iterator(); 119 boolean result = false; 120 while (i.hasNext()) { 121 if (!c.contains(i.next())) { 122 i.remove(); 123 result = true; 124 } 125 } 126 return result; 127 } 128 129 public int size() { 130 ShortIterator i = iterator(); 131 int size = 0; 132 while (i.hasNext()) { 133 i.next(); 134 size++; 135 } 136 return size; 137 } 138 139 public short[] toArray() { 140 return toArray(null); 141 } 142 143 public short[] toArray(short[] a) { 144 int size = size(); 145 if (a == null || a.length < size) 146 a = new short[size]; 147 ShortIterator i = iterator(); 148 int index = 0; 149 while (i.hasNext()) { 150 a[index] = i.next(); 151 index++; 152 } 153 return a; 154 } 155 156 161 public void trimToSize() 162 { } 163 164 169 public String toString() { 170 StringBuffer s = new StringBuffer (); 171 s.append('['); 172 ShortIterator i = iterator(); 173 while (i.hasNext()) { 174 if (s.length() > 1) 175 s.append(','); 176 s.append(bak.pcj.util.Display.display(i.next())); 177 } 178 s.append(']'); 179 return s.toString(); 180 } 181 182 } | Popular Tags |