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