404511 removed deprecated StringMap
This commit is contained in:
parent
5a0811b328
commit
deb7102e0e
|
@ -1,196 +0,0 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Map implementation Optimized for Strings keys..
|
||||
* This String Map has been optimized for mapping small sets of
|
||||
* Strings where the most frequently accessed Strings have been put to
|
||||
* the map first.
|
||||
*
|
||||
* It also has the benefit that it can look up entries by substring or
|
||||
* sections of char and byte arrays. This can prevent many String
|
||||
* objects from being created just to look up in the map.
|
||||
*
|
||||
* This map is NOT synchronized.
|
||||
* @deprecated Use {@link Trie}
|
||||
*/
|
||||
public class StringMap<O> extends AbstractMap<String,O>
|
||||
{
|
||||
private final TreeMap<Object, O> _map;
|
||||
|
||||
|
||||
public static final boolean CASE_INSENSTIVE=true;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
private final boolean _caseInsensitive;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Constructor.
|
||||
*/
|
||||
public StringMap()
|
||||
{
|
||||
this(false);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Constructor.
|
||||
* @param ignoreCase
|
||||
*/
|
||||
public StringMap(final boolean ignoreCase)
|
||||
{
|
||||
_caseInsensitive=ignoreCase;
|
||||
_map = new TreeMap<Object,O>(new Comparator<Object>()
|
||||
{
|
||||
@Override
|
||||
public int compare(Object o1, Object o2)
|
||||
{
|
||||
String s1=(o1 instanceof String)?(String)o1:null;
|
||||
ByteBuffer b1=(o1 instanceof ByteBuffer)?(ByteBuffer)o1:null;
|
||||
if (s1==null && b1==null)
|
||||
s1=o1.toString();
|
||||
String s2=(String)o2;
|
||||
|
||||
int n1 = s1==null?b1.remaining():s1.length();
|
||||
int n2 = s2.length();
|
||||
int min = Math.min(n1, n2);
|
||||
for (int i = 0; i < min; i++) {
|
||||
char c1 = s1==null?(char)b1.get(b1.position()+i):s1.charAt(i);
|
||||
char c2 = s2.charAt(i);
|
||||
if (c1 != c2) {
|
||||
if (ignoreCase)
|
||||
{
|
||||
c1 = Character.toUpperCase(c1);
|
||||
c2 = Character.toUpperCase(c2);
|
||||
if (c1 != c2) {
|
||||
c1 = Character.toLowerCase(c1);
|
||||
c2 = Character.toLowerCase(c2);
|
||||
if (c1 != c2) {
|
||||
// No overflow because of numeric promotion
|
||||
return c1 - c2;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
return c1 - c2;
|
||||
}
|
||||
}
|
||||
return n1 - n2;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public boolean isIgnoreCase()
|
||||
{
|
||||
return _caseInsensitive;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public O put(String key, O value)
|
||||
{
|
||||
return _map.put(key,value);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public O get(Object key)
|
||||
{
|
||||
return _map.get(key);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public O get(String key)
|
||||
{
|
||||
return _map.get(key);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public O get(String key,int offset,int length)
|
||||
{
|
||||
return _map.get(key.substring(offset,offset+length));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public O get(ByteBuffer buffer)
|
||||
{
|
||||
return _map.get(buffer);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public O remove(Object key)
|
||||
{
|
||||
return _map.remove(key);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public O remove(String key)
|
||||
{
|
||||
return _map.remove(key);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public Set<Map.Entry<String,O>> entrySet()
|
||||
{
|
||||
Object o=_map.entrySet();
|
||||
return Collections.unmodifiableSet((Set<Map.Entry<String,O>>)o);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public int size()
|
||||
{
|
||||
return _map.size();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public boolean isEmpty()
|
||||
{
|
||||
return _map.isEmpty();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public boolean containsKey(Object key)
|
||||
{
|
||||
return _map.containsKey(key);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
public void clear()
|
||||
{
|
||||
_map.clear();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,209 +0,0 @@
|
|||
//
|
||||
// ========================================================================
|
||||
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
|
||||
// ------------------------------------------------------------------------
|
||||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
//
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
//
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
//
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.util;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StringMapTest
|
||||
{
|
||||
StringMap<String> m0;
|
||||
StringMap<String> m1;
|
||||
StringMap<String> m5;
|
||||
StringMap<String> m5i;
|
||||
|
||||
/*
|
||||
* @see TestCase#setUp()
|
||||
*/
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception
|
||||
{
|
||||
m0=new StringMap<>();
|
||||
m1=new StringMap<>(false);
|
||||
m1.put("abc", "0");
|
||||
|
||||
m5=new StringMap<>(false);
|
||||
m5.put("a", "0");
|
||||
m5.put("ab", "1");
|
||||
m5.put("abc", "2");
|
||||
m5.put("abb", "3");
|
||||
m5.put("bbb", "4");
|
||||
|
||||
m5i=new StringMap<>(true);
|
||||
m5i.put("ab", "1");
|
||||
m5i.put("abc", "2");
|
||||
m5i.put("abb", "3");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSize()
|
||||
{
|
||||
Assert.assertEquals(0, m0.size());
|
||||
Assert.assertEquals(1, m1.size());
|
||||
Assert.assertEquals(5, m5.size());
|
||||
Assert.assertEquals(3, m5i.size());
|
||||
|
||||
m1.remove("abc");
|
||||
m5.remove("abc");
|
||||
m5.put("bbb","x");
|
||||
m5i.put("ABC", "x");
|
||||
Assert.assertEquals(0, m0.size());
|
||||
Assert.assertEquals(0, m1.size());
|
||||
Assert.assertEquals(4, m5.size());
|
||||
Assert.assertEquals(3, m5i.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmpty()
|
||||
{
|
||||
Assert.assertTrue(m0.isEmpty());
|
||||
Assert.assertFalse(m1.isEmpty());
|
||||
Assert.assertFalse(m5.isEmpty());
|
||||
Assert.assertFalse(m5i.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClear()
|
||||
{
|
||||
m0.clear();
|
||||
m1.clear();
|
||||
m5.clear();
|
||||
m5i.clear();
|
||||
Assert.assertTrue(m0.isEmpty());
|
||||
Assert.assertTrue(m1.isEmpty());
|
||||
Assert.assertTrue(m5.isEmpty());
|
||||
Assert.assertTrue(m5i.isEmpty());
|
||||
Assert.assertEquals(null, m1.get("abc"));
|
||||
Assert.assertEquals(null, m5.get("abc"));
|
||||
Assert.assertEquals(null, m5i.get("abc"));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Test for Object put(Object, Object)
|
||||
*/
|
||||
@Test
|
||||
public void testPutGet()
|
||||
{
|
||||
Assert.assertEquals("2", m5.get("abc"));
|
||||
Assert.assertEquals(null, m5.get("aBc"));
|
||||
Assert.assertEquals("2", m5i.get("abc"));
|
||||
Assert.assertEquals("2", m5i.get("aBc"));
|
||||
|
||||
m5.put("aBc", "x");
|
||||
m5i.put("AbC", "x");
|
||||
|
||||
StringBuilder buffer=new StringBuilder();
|
||||
buffer.append("aBc");
|
||||
Assert.assertEquals("2", m5.get("abc"));
|
||||
Assert.assertEquals("x", m5.get(buffer));
|
||||
Assert.assertEquals("x", m5i.get((Object)"abc"));
|
||||
Assert.assertEquals("x", m5i.get("aBc"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Test for Object remove(Object)
|
||||
*/
|
||||
@Test
|
||||
public void testRemove()
|
||||
{
|
||||
m0.remove("abc");
|
||||
m1.remove("abc");
|
||||
m5.remove("aBc");
|
||||
m5.remove("bbb");
|
||||
m5i.remove("aBc");
|
||||
|
||||
Assert.assertEquals(0, m0.size());
|
||||
Assert.assertEquals(0, m1.size());
|
||||
Assert.assertEquals(4, m5.size());
|
||||
Assert.assertEquals(2, m5i.size());
|
||||
|
||||
Assert.assertEquals("2", m5.get("abc"));
|
||||
Assert.assertEquals(null, m5.get("bbb"));
|
||||
Assert.assertEquals(null, m5i.get("AbC"));
|
||||
}
|
||||
|
||||
/*
|
||||
* Test for Set entrySet()
|
||||
*/
|
||||
@Test
|
||||
public void testEntrySet()
|
||||
{
|
||||
Set es0=m0.entrySet();
|
||||
Set es1=m1.entrySet();
|
||||
Set es5=m5.entrySet();
|
||||
Assert.assertEquals(0, es0.size());
|
||||
Assert.assertEquals(1, es1.size());
|
||||
Assert.assertEquals(5, es5.size());
|
||||
}
|
||||
|
||||
/*
|
||||
* Test for boolean containsKey(Object)
|
||||
*/
|
||||
@Test
|
||||
public void testContainsKey()
|
||||
{
|
||||
Assert.assertTrue(m5.containsKey("abc"));
|
||||
Assert.assertTrue(!m5.containsKey("aBc"));
|
||||
Assert.assertTrue(m5.containsKey("bbb"));
|
||||
Assert.assertTrue(!m5.containsKey("xyz"));
|
||||
|
||||
Assert.assertTrue(m5i.containsKey("abc"));
|
||||
Assert.assertTrue(m5i.containsKey("aBc"));
|
||||
Assert.assertTrue(m5i.containsKey("ABC"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToString()
|
||||
{
|
||||
Assert.assertEquals("{}", m0.toString());
|
||||
Assert.assertEquals("{abc=0}", m1.toString());
|
||||
Assert.assertTrue(m5.toString().indexOf("abc=2") > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreCase()
|
||||
{
|
||||
StringMap<String> map = new StringMap<>(true);
|
||||
map.put("POST","1");
|
||||
map.put("HEAD","2");
|
||||
map.put("PUT","3");
|
||||
map.put("OPTIONS","4");
|
||||
map.put("DELETE","5");
|
||||
map.put("TRACE","6");
|
||||
map.put("CONNECT","7");
|
||||
map.put("Upgrade","8");
|
||||
|
||||
Assert.assertEquals("1", map.get("POST"));
|
||||
Assert.assertEquals("1", map.get("pOST"));
|
||||
Assert.assertEquals("1", map.get("Post"));
|
||||
|
||||
Assert.assertEquals("8", map.get("UPGRADE"));
|
||||
Assert.assertEquals("8", map.get("Upgrade"));
|
||||
Assert.assertEquals("8", map.get("upgrade"));
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue