@SuppressWarnings("unchecked") private void injectListener(int version) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException { Field protocolsField = Protocol.LOGIN.TO_SERVER.getClass().getDeclaredField("protocols"); protocolsField.setAccessible(true); TIntObjectMap<?> protocols = (TIntObjectMap)protocolsField.get(Protocol.LOGIN.TO_SERVER); Object protocolData = protocols.get(version); Field packetMapField = protocolData.getClass().getDeclaredField("packetMap"); Field packetConstructorsField = protocolData.getClass().getDeclaredField("packetConstructors"); packetMapField.setAccessible(true); packetConstructorsField.setAccessible(true); TObjectIntMap packetMap = (TObjectIntMap)packetMapField.get(protocolData); TIntObjectMap packetConstructors = (TIntObjectMap)packetConstructorsField.get(protocolData); packetMap.remove(EncryptionResponse.class); packetConstructors.remove(0x01); packetMap.put( EncryptionResponsePacket.class, 0x01); packetConstructors.put( 0x01, EncryptionResponsePacket.class.getDeclaredConstructor() ); packetMapField.set(protocolData, packetMap); packetConstructorsField.set(protocolData, packetConstructors); protocolsField.set(Protocol.LOGIN.TO_SERVER, protocols); }
private static TObjectIntMap<String> buildAccessors(int accessFlags) { TObjectIntMap<String> map = new TObjectIntHashMap<>(); map.put("public", Modifier.isPublic(accessFlags) ? 1 : 0); map.put("protected", Modifier.isProtected(accessFlags) ? 1 : 0); map.put("private", Modifier.isPrivate(accessFlags) ? 1 : 0); map.put("final", Modifier.isFinal(accessFlags) ? 1 : 0); map.put("interface", Modifier.isInterface(accessFlags) ? 1 : 0); map.put("native", Modifier.isNative(accessFlags) ? 1 : 0); map.put("static", Modifier.isStatic(accessFlags) ? 1 : 0); map.put("strict", Modifier.isStrict(accessFlags) ? 1 : 0); map.put("synchronized", Modifier.isSynchronized(accessFlags) ? 1 : 0); map.put("transient", Modifier.isTransient(accessFlags) ? 1 : 0); map.put("volatile", Modifier.isVolatile(accessFlags) ? 1 : 0); map.put("abstract", Modifier.isAbstract(accessFlags) ? 1 : 0); return map; }
/** * Creates a new <code>TObjectIntCustomHashMap</code> that contains the entries * in the map passed to it. * * @param map the <tt>TObjectIntMap</tt> to be copied. */ public TObjectIntCustomHashMap( HashingStrategy<? super K> strategy, TObjectIntMap<? extends K> map ) { this( strategy, map.size(), 0.5f, map.getNoEntryValue() ); if ( map instanceof TObjectIntCustomHashMap ) { TObjectIntCustomHashMap hashmap = ( TObjectIntCustomHashMap ) map; this._loadFactor = hashmap._loadFactor; this.no_entry_value = hashmap.no_entry_value; this.strategy = hashmap.strategy; //noinspection RedundantCast if ( this.no_entry_value != ( int ) 0 ) { Arrays.fill( _values, this.no_entry_value ); } setUp( (int) Math.ceil( DEFAULT_CAPACITY / _loadFactor ) ); } putAll( map ); }
public void testArray() { char[] foo = new char[] { 'a', 'b', 'c' }; char[] bar = new char[] { 'a', 'b', 'c' }; assertFalse( foo.hashCode() == bar.hashCode() ); //noinspection ArrayEquals assertFalse( foo.equals( bar ) ); HashingStrategy<char[]> strategy = new ArrayHashingStrategy(); assertTrue( strategy.computeHashCode( foo ) == strategy.computeHashCode( bar ) ); assertTrue( strategy.equals( foo, bar ) ); TObjectIntMap<char[]> map = new TObjectIntCustomHashMap<char[]>( strategy ); map.put( foo, 12 ); assertTrue( map.containsKey( foo ) ); assertTrue( map.containsKey( bar ) ); assertEquals( 12, map.get( foo ) ); assertEquals( 12, map.get( bar ) ); Set<char[]> keys = map.keySet(); assertTrue( keys.contains( foo ) ); assertTrue( keys.contains( bar ) ); }
public void testAdjustToNoEntry() { TObjectIntMap<String> map = new TObjectIntHashMap<String>(); assertEquals( 0, map.getNoEntryValue() ); assertEquals( 0, map.get( "NotInThere" ) ); map.put( "Value", 1 ); assertEquals( 1, map.size() ); assertEquals( 1, map.get( "Value" ) ); assertTrue( map.containsKey( "Value" ) ); assertTrue( map.containsValue( 1 ) ); assertTrue( Arrays.equals( new int[] { 1 }, map.values() ) ); map.adjustValue( "Value", -1 ); assertEquals( 1, map.size() ); assertEquals( 0, map.get( "Value" ) ); assertTrue( map.containsKey( "Value" ) ); assertTrue( map.containsValue( 0 ) ); assertTrue( Arrays.equals( new int[] { 0 }, map.values() ) ); }
public void testContainsKey() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = new TObjectIntHashMap<String>(); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } for ( int i = 0; i < element_count; i++ ) { assertTrue( "Key should be present: " + keys[i] + ", map: " + map, map.containsKey( keys[i] ) ); } String key = "1138"; assertFalse( "Key should not be present: " + key + ", map: " + map, map.containsKey( key ) ); assertFalse( "Random object should not be present in map: " + map, map.containsKey( new Object() ) ); }
public void testContainsValue() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = new TObjectIntHashMap<String>(); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } for ( int i = 0; i < element_count; i++ ) { assertTrue( "Value should be present: " + vals[i] + ", map: " + map, map.containsValue( vals[i] ) ); } int val = 1138; assertFalse( "Key should not be present: " + val + ", map: " + map, map.containsValue( val ) ); }
public void testPutAllMap() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> control = new TObjectIntHashMap<String>(); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; control.put( keys[i], vals[i] ); } TObjectIntMap<String> map = new TObjectIntHashMap<String>(); Map<String, Integer> source = new HashMap<String, Integer>(); for ( int i = 0; i < element_count; i++ ) { source.put( keys[i], vals[i] ); } map.putAll( source ); assertEquals( control, map ); }
public void testClear() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = new TObjectIntHashMap<String>(); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); map.clear(); assertTrue( map.isEmpty() ); assertEquals( 0, map.size() ); assertEquals( map.getNoEntryValue(), map.get( keys[5] ) ); }
public void testIncrement() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = new TObjectIntHashMap<String>( element_count, 0.5f, Integer.MIN_VALUE ); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); assertFalse( map.increment( "non-existant" ) ); assertTrue( map.increment( "1" ) ); assertEquals( 2, map.get( "1" ) ); }
public void testTransformValues() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = new TObjectIntHashMap<String>( element_count, 0.5f, Integer.MIN_VALUE ); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); map.transformValues( new TIntFunction() { @Override public int execute( int value ) { return value * value; } } ); for ( int i = 0; i < element_count; i++ ) { int expected = vals[i] * vals[i]; assertEquals( expected, map.get( keys[i] ) ); } }
public void testValues() { int element_count = 20; String[] keys = new String[element_count]; Integer[] vals = new Integer[element_count]; TObjectIntMap<String> raw_map = new TObjectIntHashMap<String>( element_count, 0.5f, Integer.MIN_VALUE ); Map<String,Integer> map = TDecorators.wrap( raw_map ); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = Integer.valueOf( i + 1 ); map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); // No argument Collection<Integer> values_collection = map.values(); assertEquals( element_count, values_collection.size() ); List<Integer> values_list = new ArrayList<Integer>( values_collection ); for ( int i = 0; i < element_count; i++ ) { assertTrue( values_list.contains( vals[i] ) ); } }
@SuppressWarnings({"unchecked"}) public void testSerialize() throws Exception { Integer[] keys = {1138, 42, 86, 99, 101, 727, 117}; int[] vals = new int[keys.length]; TObjectIntMap<Integer> map = new TObjectIntHashMap<Integer>(); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( map ); ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() ); ObjectInputStream ois = new ObjectInputStream( bias ); TObjectIntMap<Integer> deserialized = (TObjectIntMap<Integer>) ois.readObject(); assertEquals( map, deserialized ); }
public void testGetMap() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> raw_map = new TObjectIntHashMap<String>(); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; raw_map.put( keys[i], vals[i] ); } //noinspection MismatchedQueryAndUpdateOfCollection TObjectIntMapDecorator<String> map = new TObjectIntMapDecorator<String>( raw_map ); assertEquals( raw_map, map.getMap() ); }
public void testPutAllMap() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> control = new TObjectIntHashMap<String>(); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; control.put( keys[i], vals[i] ); } TObjectIntMap<String> raw_map = new TObjectIntHashMap<String>(); Map<String,Integer> map = TDecorators.wrap( raw_map ); Map<String, Integer> source = new HashMap<String, Integer>(); for ( int i = 0; i < element_count; i++ ) { source.put( keys[i], vals[i] ); } map.putAll( source ); assertEquals( source, map ); assertEquals( control, raw_map ); }
@SuppressWarnings({"unchecked"}) public void testSerialize() throws Exception { Integer[] vals = {1138, 42, 86, 99, 101, 727, 117}; String[] keys = new String[vals.length]; TObjectIntMap<String> raw_map = new TObjectIntHashMap<String>(); Map<String,Integer> map = TDecorators.wrap( raw_map ); for ( int i = 0; i < keys.length; i++ ) { keys[i] = Integer.toString( vals[i] * 2 ); map.put( keys[i], vals[i] ); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( map ); ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() ); ObjectInputStream ois = new ObjectInputStream( bias ); Map<String,Integer> deserialized = ( Map<String,Integer> ) ois.readObject(); assertEquals( map, deserialized ); }
/** * Creates a new <code>TObjectIntHashMap</code> that contains the entries * in the map passed to it. * * @param map the <tt>TObjectIntMap</tt> to be copied. */ @SuppressWarnings("rawtypes") public TObjectIntHashMap( TObjectIntMap<? extends K> map ) { this( map.size(), 0.5f, map.getNoEntryValue() ); if ( map instanceof TObjectIntHashMap ) { TObjectIntHashMap hashmap = ( TObjectIntHashMap ) map; this._loadFactor = hashmap._loadFactor; this.no_entry_value = hashmap.no_entry_value; //noinspection RedundantCast if ( this.no_entry_value != ( int ) 0 ) { Arrays.fill( _values, this.no_entry_value ); } setUp( (int) Math.ceil( DEFAULT_CAPACITY / _loadFactor ) ); } putAll( map ); }
/** * Creates a new <code>TObjectIntCustomHashMap</code> that contains the entries * in the map passed to it. * * @param map the <tt>TObjectIntMap</tt> to be copied. */ @SuppressWarnings({ "rawtypes", "unchecked" }) public TObjectIntCustomHashMap( HashingStrategy<? super K> strategy, TObjectIntMap<? extends K> map ) { this( strategy, map.size(), 0.5f, map.getNoEntryValue() ); if ( map instanceof TObjectIntCustomHashMap ) { TObjectIntCustomHashMap hashmap = ( TObjectIntCustomHashMap ) map; this._loadFactor = hashmap._loadFactor; this.no_entry_value = hashmap.no_entry_value; this.strategy = hashmap.strategy; //noinspection RedundantCast if ( this.no_entry_value != ( int ) 0 ) { Arrays.fill( _values, this.no_entry_value ); } setUp( (int) Math.ceil( DEFAULT_CAPACITY / _loadFactor ) ); } putAll( map ); }
/** * Creates a new <code>TObjectIntCustomHashMap</code> that contains the entries * in the map passed to it. * * @param map the <tt>TObjectIntMap</tt> to be copied. */ public TObjectIntCustomHashMap( HashingStrategy<K> strategy, TObjectIntMap<K> map ) { this( strategy, map.size(), 0.5f, map.getNoEntryValue() ); if ( map instanceof TObjectIntCustomHashMap ) { TObjectIntCustomHashMap hashmap = ( TObjectIntCustomHashMap ) map; this._loadFactor = hashmap._loadFactor; this.no_entry_value = hashmap.no_entry_value; this.strategy = hashmap.strategy; //noinspection RedundantCast if ( this.no_entry_value != ( int ) 0 ) { Arrays.fill( _values, this.no_entry_value ); } setUp( (int) Math.ceil( DEFAULT_CAPACITY / _loadFactor ) ); } putAll( map ); }
public void testTPrimitiveHashMap() { TObjectIntMap<String> map = new TObjectIntHashMap<String>(); // Add 5, remove the first four, repeat String[] to_remove = new String[ 4 ]; int batch_index = 0; for( String s : Constants.STRING_OBJECTS ) { if ( batch_index < 4 ) { to_remove[ batch_index ] = s; } map.put( s, s.length() ); batch_index++; if ( batch_index == 5 ) { for( String s_remove : to_remove ) { map.remove( s_remove ); } batch_index = 0; } } }
public void testAdjustToNoEntry() { TObjectIntMap<String> map = TKolobokeObjectIntHashMap.withExpectedSize(10); assertEquals( 0, map.getNoEntryValue() ); assertEquals( 0, map.get( "NotInThere" ) ); map.put( "Value", 1 ); assertEquals( 1, map.size() ); assertEquals( 1, map.get( "Value" ) ); assertTrue( map.containsKey( "Value" ) ); assertTrue( map.containsValue( 1 ) ); assertTrue( Arrays.equals( new int[] { 1 }, map.values() ) ); map.adjustValue( "Value", -1 ); assertEquals( 1, map.size() ); assertEquals( 0, map.get( "Value" ) ); assertTrue( map.containsKey( "Value" ) ); assertTrue( map.containsValue( 0 ) ); assertTrue( Arrays.equals( new int[] { 0 }, map.values() ) ); }
public void testContainsKey() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = withExpectedSize(10); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } for ( int i = 0; i < element_count; i++ ) { assertTrue( "Key should be present: " + keys[i] + ", map: " + map, map.containsKey( keys[i] ) ); } String key = "1138"; assertFalse( "Key should not be present: " + key + ", map: " + map, map.containsKey( key ) ); assertFalse( "Random object should not be present in map: " + map, map.containsKey( new Object() ) ); }
public void testContainsValue() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = withExpectedSize(10); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } for ( int i = 0; i < element_count; i++ ) { assertTrue( "Value should be present: " + vals[i] + ", map: " + map, map.containsValue( vals[i] ) ); } int val = 1138; assertFalse( "Key should not be present: " + val + ", map: " + map, map.containsValue( val ) ); }
public void testPutAllMap() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> control = withExpectedSize(10); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; control.put( keys[i], vals[i] ); } TObjectIntMap<String> map = withExpectedSize(10); Map<String, Integer> source = new HashMap<String, Integer>(); for ( int i = 0; i < element_count; i++ ) { source.put( keys[i], vals[i] ); } map.putAll( source ); assertEquals( control, map ); }
public void testClear() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = withExpectedSize(10); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); map.clear(); assertTrue( map.isEmpty() ); assertEquals( 0, map.size() ); assertEquals( map.getNoEntryValue(), map.get( keys[5] ) ); }
public void testIteratorRemoval2() { int element_count = 10000; int remaining = element_count / 2; TObjectIntMap<String> map = withExpectedSizeAndNoEntryValue( element_count, Integer.MIN_VALUE ); for ( int pass = 0; pass < 10; pass++ ) { Random r = new Random(); for ( int i = 0; i <= element_count; i++ ) { map.put( String.valueOf( r.nextInt() ), i ); } TObjectIntIterator iterator = map.iterator(); while ( map.size() > remaining && iterator.hasNext() ) { iterator.advance(); iterator.remove(); } } }
public void testIncrement() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = withExpectedSizeAndNoEntryValue( element_count, Integer.MIN_VALUE ); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); assertFalse( map.increment( "non-existant" ) ); assertTrue( map.increment( "1" ) ); assertEquals( 2, map.get( "1" ) ); }
public void testAdjustValue() { TObjectIntMap<String> map = withExpectedSize(10); map.put( "one", 1 ); boolean changed = map.adjustValue( "one", 1 ); assertTrue( changed ); assertEquals( 2, map.get( "one" ) ); changed = map.adjustValue( "one", 5 ); assertTrue( changed ); assertEquals( 7, map.get( "one" ) ); changed = map.adjustValue( "one", -3 ); assertTrue( changed ); assertEquals( 4, map.get( "one" ) ); changed = map.adjustValue( "two", 1 ); assertFalse( changed ); assertFalse( map.containsKey( "two" ) ); }
public void testAdjustOrPutValue() { TObjectIntMap<String> map = withExpectedSize(10); map.put( "one", 1 ); long new_value = map.adjustOrPutValue( "one", 1, 100 ); assertEquals( 2, new_value ); assertEquals( 2, map.get( "one" ) ); new_value = map.adjustOrPutValue( "one", 5, 100 ); assertEquals( 7, new_value ); assertEquals( 7, map.get( "one" ) ); new_value = map.adjustOrPutValue( "one", -3, 100 ); assertEquals( 4, new_value ); assertEquals( 4, map.get( "one" ) ); new_value = map.adjustOrPutValue( "two", 1, 100 ); assertEquals( 100, new_value ); assertTrue( map.containsKey( "two" ) ); assertEquals( 100, map.get( "two" ) ); new_value = map.adjustOrPutValue( "two", 1, 100 ); assertEquals( 101, new_value ); assertEquals( 101, map.get( "two" ) ); }
public void testTransformValues() { int element_count = 20; String[] keys = new String[element_count]; int[] vals = new int[element_count]; TObjectIntMap<String> map = withExpectedSizeAndNoEntryValue( element_count, Integer.MIN_VALUE ); for ( int i = 0; i < element_count; i++ ) { keys[i] = Integer.toString( i + 1 ); vals[i] = i + 1; map.put( keys[i], vals[i] ); } assertEquals( element_count, map.size() ); map.transformValues( new TIntFunction() { public int execute( int value ) { return value * value; } } ); for ( int i = 0; i < element_count; i++ ) { int expected = vals[i] * vals[i]; assertEquals( expected, map.get( keys[i] ) ); } }
/** * Koloboke Compile doesn't support serialization yet */ @SuppressWarnings({"unchecked"}) public void ignoreTestSerialize() throws Exception { Integer[] keys = {1138, 42, 86, 99, 101, 727, 117}; int[] vals = new int[keys.length]; TObjectIntMap<Integer> map = withExpectedSize(10); for ( int i = 0; i < keys.length; i++ ) { vals[i] = keys[i] * 2; map.put( keys[i], vals[i] ); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( map ); ByteArrayInputStream bias = new ByteArrayInputStream( baos.toByteArray() ); ObjectInputStream ois = new ObjectInputStream( bias ); TObjectIntMap<Integer> deserialized = (TObjectIntMap<Integer>) ois.readObject(); assertEquals( map, deserialized ); }