/* * Copyright 2014 Goldman Sachs. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.gs.collections.impl.map.strategy.immutable; import com.gs.collections.api.block.HashingStrategy; import com.gs.collections.api.map.ImmutableMap; import com.gs.collections.impl.block.factory.HashingStrategies; import com.gs.collections.impl.map.immutable.ImmutableMapTestCase; import com.gs.collections.impl.map.strategy.mutable.UnifiedMapWithHashingStrategy; import com.gs.collections.impl.math.IntegerSum; import com.gs.collections.impl.math.Sum; import com.gs.collections.impl.parallel.BatchIterable; import com.gs.collections.impl.test.SerializeTestHelper; import com.gs.collections.impl.test.Verify; import org.junit.Assert; import org.junit.Test; public class ImmutableUnifiedMapWithHashingStrategyTest extends ImmutableMapTestCase { //Not using the static factor method in order to have concrete types for test cases private static final HashingStrategy<Integer> HASHING_STRATEGY = HashingStrategies.nullSafeHashingStrategy(new HashingStrategy<Integer>() { public int computeHashCode(Integer object) { return object.hashCode(); } public boolean equals(Integer object1, Integer object2) { return object1.equals(object2); } }); @Override @Test public void equalsAndHashCode() { super.equalsAndHashCode(); ImmutableMap<Integer, String> deserialized = SerializeTestHelper.serializeDeserialize(this.classUnderTest()); Verify.assertInstanceOf(ImmutableUnifiedMapWithHashingStrategy.class, deserialized); } @Override protected ImmutableMap<Integer, String> classUnderTest() { return UnifiedMapWithHashingStrategy.newWithKeysValues(HASHING_STRATEGY, 1, "1", 2, "2", 3, "3", 4, "4").toImmutable(); } @Override protected int size() { return 4; } @Test @Override public void testToString() { Assert.assertEquals("{1=1, 2=2, 3=3, 4=4}", this.classUnderTest().toString()); } @Test public void getBatchCount() { BatchIterable<Integer> integerBatchIterable = (BatchIterable<Integer>) this.classUnderTest(); Assert.assertEquals(5, integerBatchIterable.getBatchCount(3)); } @Test public void batchForEach() { Sum sum = new IntegerSum(0); BatchIterable<String> integerBatchIterable = (BatchIterable<String>) this.classUnderTest(); integerBatchIterable.batchForEach(each -> sum.add(Integer.valueOf(each)), 0, 1); Assert.assertEquals(10, sum.getValue()); } }