public Builder() { this(NaN, HashTreePMap.empty()); }
public static <K,V> PMap<K,V> map() { return HashTreePMap.empty(); } }
public static <K,V> PMap<K,V> map() { return HashTreePMap.empty(); } }
/** * @param <K> * @param <V> * @param key * @param value * @return empty().plus(key, value) */ public static <K,V> HashPMap<K,V> singleton(final K key, final V value) { return HashTreePMap.<K,V>empty().plus(key, value); }
/** * @param <K> * @param <V> * @param map * @return empty().plusAll(map) */ public static <K,V> HashPMap<K,V> from(final Map<? extends K, ? extends V> map) { return HashTreePMap.<K,V>empty().plusAll(map); } }
/** * @param <K> * @param <V> * @param key * @param value * @return empty().plus(key, value) */ public static <K,V> HashPMap<K,V> singleton(final K key, final V value) { return HashTreePMap.<K,V>empty().plus(key, value); }
/** * @param <K> * @param <V> * @param map * @return empty().plusAll(map) */ public static <K,V> HashPMap<K,V> from(final Map<? extends K, ? extends V> map) { return HashTreePMap.<K,V>empty().plusAll(map); } }
public void testSingleton() { UtilityTest.assertEqualsAndHash(HashTreePMap.empty().plus(10, "test"), HashTreePMap.singleton(10, "test")); }
public void testEmpty() { PMap<?,?> empty = HashTreePMap.empty(); UtilityTest.assertEqualsAndHash(new HashMap<Object,Object>(), empty); assertEquals(0, empty.size()); assertTrue(empty.isEmpty()); for(@SuppressWarnings("unused") Object e : empty.entrySet()) fail(); }
public void testSerializationForHashPMapWithEntrySet() { /* regression test because entry set cache is unserializable, but wasn't marked as transient */ final HashPMap<String, Long> initialHashPMap = HashTreePMap.<String, Long> empty() .plus("key001", 23L) .plus("key002", 29L) .plus("key003", 31L); initialHashPMap.entrySet(); serializeToByteArray(initialHashPMap); // used to throw an exception }
public void testSerializationForHashPMap() { final HashPMap<String, Long> initialHashPMap = HashTreePMap.<String, Long> empty() .plus("key001", 23L) .plus("key002", 29L) .plus("key003", 31L); final byte[] serializedThirdPHashPMap = serializeToByteArray(initialHashPMap); assertNotNull(serializedThirdPHashPMap); assertTrue(serializedThirdPHashPMap.length > 0); @SuppressWarnings("unchecked") final HashPMap<String, Long> deserializedHashPMap = (HashPMap<String, Long>) deserializeFromByteArray(serializedThirdPHashPMap, HashPMap.class); assertNotNull(deserializedHashPMap); assertEquals(Long.valueOf(23L), deserializedHashPMap.get("key001")); assertEquals(Long.valueOf(29L), deserializedHashPMap.get("key002")); assertEquals(Long.valueOf(31L), deserializedHashPMap.get("key003")); assertNotSame(initialHashPMap, deserializedHashPMap); assertEquals(initialHashPMap, deserializedHashPMap); }
/** * @param <K> * @param <V> * @param map * @return empty().plusAll(map) */ public static <K,V> HashPMap<K,V> from(final Map<? extends K, ? extends V> map) { return HashTreePMap.<K,V>empty().plusAll(map); } }
/** * @param <K> * @param <V> * @param map * @return empty().plusAll(map) */ public static <K, V> HashPMap<K, V> from(final Map<? extends K, ? extends V> map) { return HashTreePMap.<K, V>empty().plusAll(map); } }
/** * @param <K> * @param <V> * @param key * @param value * @return empty().plus(key, value) */ public static <K,V> HashPMap<K,V> singleton(final K key, final V value) { return HashTreePMap.<K,V>empty().plus(key, value); }
public void testSet() { PSet<String> a = HashTreePSet.empty(), b = HashTreePSet.empty(); assertEqualsAndHash("empty sets not equal",a,b); assertTrue("adding element should change empty set",!a.equals(a.plus("one"))); a = a.plus("one"); assertEqualsAndHash("adding duplicate should not change set",a,a.plus("one")); a = a.plus("two").plus(""); b = b.plus("two").plus("").plus("one").plus("two"); assertEqualsAndHash(a,b); b = b.plus("three"); assertTrue(!a.equals(b)); int n = b.size(); for(@SuppressWarnings("unused") String s : b) n--; assertTrue("iterator() should return size() elements", n==0); assertEqualsAndHash("adding a set to itself should do nothing",b,b.plusAll(b)); assertEqualsAndHash("adding a set to the empty set should be identity", b, MapPSet.from(HashTreePMap.<String,Object>empty(), b)); // bug Mike found: assertEqualsAndHash(HashTreePSet.from(Arrays.asList(2)), HashTreePSet.empty().plus(1).plus(2).minus(1)); }
/** * @param map Collection. * @param evt Event. * @return Cloned map. */ protected static <E> Map<E, Integer> addToMap(@Nullable Map<E, Integer> map, E evt) { HashPMap<E, Integer> res = map == null ? HashTreePMap.<E, Integer>empty() : (HashPMap<E, Integer>)map; Integer cnt = res.get(evt); return cnt != null ? res.minus(evt).plus(evt, cnt + 1) : res.plus(evt, 1); }
public void testMap() { PMap<Object,String> m = HashTreePMap.<Object,String>empty(); assertEquals(m.size(),0); m = m.plus(1,"One"); assertEquals(m.get(1),"One"); // last three have same hashCode: Object[] keys = {1,2,0,3,ConsPStack.empty()}; String[] values = {"one","two","","three","emptyList"}; for(int i=0;i<keys.length;i++) { m = m.plus(keys[i],values[i]); m = m.minus(keys[i]); m = m.plus(keys[i],values[i]); assertEquals(m.size(),i+1); assertEquals(m.get(keys[i]),values[i]); } for(int i=keys.length-1;i>=0;i--) { m = m.minus(keys[i]); assertEquals(m.size(),i); } }
public Events addEvent(UUID scopeId, UUID eventId, String name, Command... commands) { PMap<UUID, EventRecord> scope = events.get(scopeId); if (scope == null) { scope = HashTreePMap.empty(); } EventRecord r = new EventRecord(name, commands); scope = scope.plus(eventId, r); return new Events(this.events.plus(scopeId, scope)); }