Close #918 - use case insensitive lookup for Yaml locations

This commit is contained in:
md_5
2014-03-10 11:04:28 +11:00
parent 003a1973d4
commit 4a7f8015e5
6 changed files with 10 additions and 3 deletions

View File

@@ -0,0 +1,21 @@
package net.md_5.bungee.util;
import gnu.trove.strategy.HashingStrategy;
class CaseInsensitiveHashingStrategy implements HashingStrategy
{
static final CaseInsensitiveHashingStrategy INSTANCE = new CaseInsensitiveHashingStrategy();
@Override
public int computeHashCode(Object object)
{
return ( (String) object ).toLowerCase().hashCode();
}
@Override
public boolean equals(Object o1, Object o2)
{
return o1.equals( o2 ) || ( o1 instanceof String && o2 instanceof String && ( (String) o1 ).toLowerCase().equals( ( (String) o2 ).toLowerCase() ) );
}
}

View File

@@ -0,0 +1,18 @@
package net.md_5.bungee.util;
import gnu.trove.map.hash.TCustomHashMap;
import java.util.Map;
public class CaseInsensitiveMap<V> extends TCustomHashMap<String, V>
{
public CaseInsensitiveMap()
{
super( CaseInsensitiveHashingStrategy.INSTANCE );
}
public CaseInsensitiveMap(Map<? extends String, ? extends V> map)
{
super( CaseInsensitiveHashingStrategy.INSTANCE, map );
}
}

View File

@@ -0,0 +1,18 @@
package net.md_5.bungee.util;
import gnu.trove.set.hash.TCustomHashSet;
import java.util.Collection;
public class CaseInsensitiveSet extends TCustomHashSet<String>
{
public CaseInsensitiveSet()
{
super( CaseInsensitiveHashingStrategy.INSTANCE );
}
public CaseInsensitiveSet(Collection<? extends String> collection)
{
super( CaseInsensitiveHashingStrategy.INSTANCE, collection );
}
}

View File

@@ -0,0 +1,34 @@
package net.md_5.bungee.util;
import org.junit.Test;
import org.junit.Assert;
public class CaseInsensitiveTest
{
@Test
public void testMaps()
{
Object obj = new Object();
CaseInsensitiveMap<Object> map = new CaseInsensitiveMap<>();
map.put( "FOO", obj );
Assert.assertTrue( map.contains( "foo" ) ); // Assert that contains is case insensitive
Assert.assertTrue( map.entrySet().iterator().next().getKey().equals( "FOO" ) ); // Assert that case is preserved
// Assert that remove is case insensitive
map.remove( "FoO" );
Assert.assertFalse( map.contains( "foo" ) );
}
@Test
public void testSets()
{
CaseInsensitiveSet set = new CaseInsensitiveSet();
set.add( "FOO" );
Assert.assertTrue( set.contains( "foo" ) ); // Assert that contains is case insensitive
set.remove( "FoO" );
Assert.assertFalse( set.contains( "foo" ) ); // Assert that remove is case insensitive
}
}