annotate src/org/eclipse/jetty/io/StringCache.java @ 1022:3718afd99988

HttpHeaders uses StringCache
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 01 Nov 2016 01:04:46 -0600
parents 6be43ef1eb96
children 27f3dc761452
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 // to replace BufferCache
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 package org.eclipse.jetty.io;
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4
1022
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
5 import java.util.Map;
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.HashMap;
1022
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
7 import java.util.TreeMap;
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import org.eclipse.jetty.util.StringUtil;
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
1020
6be43ef1eb96 HttpHeaderValues uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1019
diff changeset
11 public final class StringCache {
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 private final Map<String,Integer> stringToOrdinal = new HashMap<String,Integer>();
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 // private final Map<Integer,String> ordinalToString = new HashMap<Integer,String>();
1022
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
14 private final TreeMap<String,String> stringMap = new TreeMap<String,String>();
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 public byte[] add(String value, int ordinal) {
1022
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
17 String valueLower = value.toLowerCase();
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
18 stringToOrdinal.put(valueLower, ordinal);
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 // ordinalToString.put(ordinal, value);
1022
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
20 stringMap.put(valueLower,value);
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return StringUtil.getBytes(value);
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 }
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public int getOrdinal(String value) {
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 Integer i = stringToOrdinal.get(value.toLowerCase());
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return i==null ? -1 : i;
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 public boolean contains(String value) {
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 return stringToOrdinal.containsKey(value.toLowerCase());
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
1022
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
32
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
33 public String getBest(String key) {
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
34 key = key.toLowerCase();
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
35 String rtn = stringMap.get(key);
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
36 if( rtn != null )
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
37 return rtn;
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
38 Map.Entry<String,String> floor = stringMap.floorEntry(key);
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
39 Map.Entry<String,String> ceiling = stringMap.ceilingEntry(key);
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
40 if( floor==null ) {
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
41 if( ceiling==null )
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
42 return null;
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
43 String ceilingKey = ceiling.getKey();
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
44 return key.charAt(0) == ceilingKey.charAt(0) ? ceiling.getValue() : null;
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
45 } else {
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
46 String floorKey = (String)floor.getKey();
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
47 if( ceiling==null )
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
48 return key.charAt(0) == floorKey.charAt(0) ? floor.getValue() : null;
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
49 String ceilingKey = ceiling.getKey();
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
50 int n = Math.min( key.length(), Math.min( floorKey.length(), ceilingKey.length() ) );
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
51 int i = 0;
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
52 while( ++i <= n && key.regionMatches(0,floorKey,0,i) && key.regionMatches(0,ceilingKey,0,i) );
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
53 return key.regionMatches(0,floorKey,0,i) ? floor.getValue() : key.regionMatches(0,ceilingKey,0,i) || i > 1 ? ceiling.getValue() : null;
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
54 }
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
55 }
3718afd99988 HttpHeaders uses StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents: 1020
diff changeset
56
1019
f126d30e04a4 start replacing BufferCache with StringCache
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }