1 23 24 29 42 43 48 49 package com.sun.enterprise.util.collection; 50 51 import java.util.HashMap ; 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 55 56 59 60 65 public class SortedArrayListBucket 66 implements Bucket 67 { 68 69 protected ArrayList entries; 70 71 SortedArrayListBucket() { 72 entries = new ArrayList (); 73 } 74 75 public Object put(long searchKey, Object object) { 76 return put((int) searchKey, object); 77 } 78 79 public Object put(int searchKey, Object object) { 80 int low = 0, high = entries.size()-1, mid; 81 int entryKey = 0; 82 IntEntry entry = null; 83 while (low <= high) { 84 mid = (low + high) / 2; 85 entry = (IntEntry) entries.get(mid); 86 entryKey = entry.key; 87 if (entryKey == searchKey) { 88 Object oldObject = entry.object; 89 entry.object = object; 90 return oldObject; 91 } else if (searchKey < entryKey) { 92 high = mid - 1; 93 } else { 94 low = mid + 1; 95 } 96 } 97 98 entries.add(low, new IntEntry(searchKey, object)); 101 return null; 102 } 103 104 public Object get(long searchKey) { 105 return get((int) searchKey); 106 } 107 108 public Object get(int searchKey) { 109 int low = 0, high = entries.size()-1, mid; 110 int entryKey = 0; 111 IntEntry entry = null; 112 while (low <= high) { 113 mid = (low + high) / 2; 114 entry = (IntEntry) entries.get(mid); 115 entryKey = entry.key; 116 if (entryKey == searchKey) { 117 return entry.object; 118 } else if (searchKey < entryKey) { 119 high = mid - 1; 120 } else { 121 low = mid + 1; 122 } 123 } 124 return null; 125 } 126 127 public Object remove(long searchKey) { 128 return remove((int) searchKey); 129 } 130 131 public Object remove(int searchKey) { 132 int low = 0, high = entries.size()-1, mid; 133 int entryKey = 0; 134 IntEntry entry = null; 135 136 while (low <= high) { 137 mid = (low + high) / 2; 138 entry = (IntEntry) entries.get(mid); 139 entryKey = entry.key; 140 if (entryKey == searchKey) { 141 entries.remove(mid); 142 return entry.object; 144 } else if (searchKey < entryKey) { 145 high = mid - 1; 146 } else { 147 low = mid + 1; 148 } 149 } 150 return null; 151 } 152 153 public int size() { 154 return entries.size(); 155 } 156 157 public boolean containsKey(int searchKey) { 158 return (get((long) searchKey) != null); 159 } 160 161 public boolean containsKey(long searchKey) { 162 return (get(searchKey) != null); 163 } 164 165 public Iterator iterator() { 166 return new BucketIterator(entries, false); 167 } 168 169 public Iterator entryIterator() { 170 return new BucketIterator(entries, true); 171 } 172 173 private class BucketIterator 174 implements java.util.Iterator 175 { 176 ArrayList entries; 177 int index = 0; 178 boolean iterateEntry; 179 180 BucketIterator(ArrayList entries, boolean iterateEntry) { 181 this.entries = entries; 182 this.index = 0; 183 this.iterateEntry = iterateEntry; 184 } 185 186 public boolean hasNext() { 187 return (index < entries.size()); 188 } 189 190 public Object next() { 191 return (iterateEntry ? entries.get(index++) : ((IntEntry) entries.get(index++)).object); 192 } 193 194 public void remove() { 195 196 } 197 198 } 199 200 201 202 } 203 204 | Popular Tags |