view src/org/eclipse/jetty/io/StringCache.java @ 1048:2b769da7f67d

remove Buffer
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 07 Nov 2016 23:15:42 -0700
parents 27f3dc761452
children
line wrap: on
line source

// to replace BufferCache

package org.eclipse.jetty.io;

import java.util.Map;
import java.util.HashMap;
import java.util.TreeMap;
import org.eclipse.jetty.util.StringUtil;


public final class StringCache {
	private final Map<String,Integer> stringToOrdinal = new HashMap<String,Integer>();
//	private final Map<Integer,String> ordinalToString = new HashMap<Integer,String>();
	private final TreeMap<String,String> stringMap = new TreeMap<String,String>();

	public byte[] add(String value, int ordinal) {
		String valueLower = value.toLowerCase();
		stringToOrdinal.put(valueLower, ordinal);
//		ordinalToString.put(ordinal, value);
		stringMap.put(valueLower,value);
		return StringUtil.getBytes(value);
	}

	public int getOrdinal(String value) {
		Integer i = stringToOrdinal.get(value.toLowerCase());
		return i==null ? -1 : i;
	}

	public boolean contains(String value) {
		return stringToOrdinal.containsKey(value.toLowerCase());
	}

	public String get(String value)
	{
		return stringMap.get(value.toLowerCase());
	}

	public String getBest(String key) {
		key = key.toLowerCase();
		String rtn = stringMap.get(key);
		if( rtn != null )
			return rtn;
		Map.Entry<String,String> floor = stringMap.floorEntry(key);
		Map.Entry<String,String> ceiling = stringMap.ceilingEntry(key);
		if( floor==null ) {
			if( ceiling==null )
				return null;
			String ceilingKey = ceiling.getKey();
			return key.charAt(0) == ceilingKey.charAt(0) ? ceiling.getValue() : null;
		} else {
			String floorKey = (String)floor.getKey();
			if( ceiling==null )
				return key.charAt(0) == floorKey.charAt(0) ? floor.getValue() : null;
			String ceilingKey = ceiling.getKey();
			int n = Math.min( key.length(), Math.min( floorKey.length(), ceilingKey.length() ) );
			int i = 0;
			while( ++i <= n && key.regionMatches(0,floorKey,0,i) && key.regionMatches(0,ceilingKey,0,i) );
			return key.regionMatches(0,floorKey,0,i) ? floor.getValue() : key.regionMatches(0,ceilingKey,0,i) || i > 1 ? ceiling.getValue() : null;
		}
	}

}