1 17 package org.apache.commons.collections.primitives; 18 19 35 public abstract class AbstractByteCollection implements ByteCollection { 36 public abstract ByteIterator iterator(); 37 public abstract int size(); 38 39 protected AbstractByteCollection() { } 40 41 42 public boolean add(byte element) { 43 throw new UnsupportedOperationException ("add(byte) is not supported."); 44 } 45 46 public boolean addAll(ByteCollection c) { 47 boolean modified = false; 48 for(ByteIterator iter = c.iterator(); iter.hasNext(); ) { 49 modified |= add(iter.next()); 50 } 51 return modified; 52 } 53 54 public void clear() { 55 for(ByteIterator iter = iterator(); iter.hasNext();) { 56 iter.next(); 57 iter.remove(); 58 } 59 } 60 61 public boolean contains(byte element) { 62 for(ByteIterator iter = iterator(); iter.hasNext();) { 63 if(iter.next() == element) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 public boolean containsAll(ByteCollection c) { 71 for(ByteIterator iter = c.iterator(); iter.hasNext();) { 72 if(!contains(iter.next())) { 73 return false; 74 } 75 } 76 return true; 77 } 78 79 public boolean isEmpty() { 80 return (0 == size()); 81 } 82 83 public boolean removeElement(byte element) { 84 for(ByteIterator iter = iterator(); iter.hasNext();) { 85 if(iter.next() == element) { 86 iter.remove(); 87 return true; 88 } 89 } 90 return false; 91 } 92 93 public boolean removeAll(ByteCollection c) { 94 boolean modified = false; 95 for(ByteIterator iter = c.iterator(); iter.hasNext(); ) { 96 modified |= removeElement(iter.next()); 97 } 98 return modified; 99 } 100 101 public boolean retainAll(ByteCollection c) { 102 boolean modified = false; 103 for(ByteIterator iter = iterator(); iter.hasNext();) { 104 if(!c.contains(iter.next())) { 105 iter.remove(); 106 modified = true; 107 } 108 } 109 return modified; 110 } 111 112 public byte[] toArray() { 113 byte[] array = new byte[size()]; 114 int i = 0; 115 for(ByteIterator iter = iterator(); iter.hasNext();) { 116 array[i] = iter.next(); 117 i++; 118 } 119 return array; 120 } 121 122 public byte[] toArray(byte[] a) { 123 if(a.length < size()) { 124 return toArray(); 125 } else { 126 int i = 0; 127 for(ByteIterator iter = iterator(); iter.hasNext();) { 128 a[i] = iter.next(); 129 i++; 130 } 131 return a; 132 } 133 } 134 } 135 | Popular Tags |