1 17 18 package org.apache.tomcat.util.collections; 19 20 import org.apache.tomcat.util.buf.MessageBytes; 21 22 24 43 public class MultiMap { 44 45 protected Field[] fields; 46 protected int count; 48 49 52 public MultiMap(int initial_size) { 53 fields=new Field[initial_size]; 54 } 55 56 59 public void recycle() { 60 for (int i = 0; i < count; i++) { 61 fields[i].recycle(); 62 } 63 count = 0; 64 } 65 66 69 72 public int size() { 73 return count; 74 } 75 76 82 public MessageBytes getName(int n) { 83 return fields[n].name; 85 } 86 87 91 public MessageBytes getValue(int n) { 92 return fields[n].value; 93 } 94 95 97 public int find( String name, int starting ) { 98 104 for (int i = starting; i < count; i++) { 106 if (fields[i].name.equals(name)) { 107 return i; 108 } 109 } 110 return -1; 111 } 112 113 115 public int findIgnoreCase( String name, int starting ) { 116 122 for (int i = starting; i < count; i++) { 124 if (fields[i].name.equalsIgnoreCase(name)) { 125 return i; 126 } 127 } 128 return -1; 129 } 130 131 141 public void remove( int i ) { 142 Field mh = fields[i]; 144 mh.recycle(); 146 147 fields[i] = fields[count - 1]; 148 fields[count - 1] = mh; 149 count--; 150 } 151 152 154 public int addField() { 155 int len = fields.length; 156 int pos=count; 157 if (count >= len) { 158 Field tmp[] = new Field[pos * 2]; 160 System.arraycopy(fields, 0, tmp, 0, len); 161 fields = tmp; 162 } 163 if (fields[pos] == null) { 164 fields[pos] = new Field(); 165 } 166 count++; 167 return pos; 168 } 169 170 public MessageBytes get( String name) { 171 for (int i = 0; i < count; i++) { 172 if (fields[i].name.equals(name)) { 173 return fields[i].value; 174 } 175 } 176 return null; 177 } 178 179 public int findFirst( String name ) { 180 for (int i = 0; i < count; i++) { 181 if (fields[i].name.equals(name)) { 182 return i; 183 } 184 } 185 return -1; 186 } 187 188 public int findNext( int startPos ) { 189 int next= fields[startPos].nextPos; 190 if( next != MultiMap.NEED_NEXT ) { 191 return next; 192 } 193 194 MessageBytes name=fields[startPos].name; 196 for (int i = startPos; i < count; i++) { 197 if (fields[i].name.equals(name)) { 198 fields[startPos].nextPos=i; 200 return i; 201 } 202 } 203 fields[startPos].nextPos= MultiMap.LAST; 204 return -1; 205 } 206 207 static final int NEED_NEXT=-2; 209 static final int LAST=-1; 210 211 final class Field { 213 MessageBytes name; 214 MessageBytes value; 215 216 218 int nextPos; 221 222 int hash; 224 Field nextSameHash; 225 226 Field() { 227 nextPos=MultiMap.NEED_NEXT; 228 } 229 230 void recycle() { 231 name.recycle(); 232 value.recycle(); 233 nextPos=MultiMap.NEED_NEXT; 234 } 235 } 236 } 237 | Popular Tags |