Skip to content

Commit

Permalink
Merge pull request #1200 from motlin/map-toString-test
Browse files Browse the repository at this point in the history
Implement missing implementations of toString(), fixing an issue mentioned in #1196. Also optimize some implementations of toString() that were delegating to iterators.
  • Loading branch information
donraab committed Mar 4, 2022
2 parents affd2c7 + ddce4f9 commit 3163e7b
Show file tree
Hide file tree
Showing 40 changed files with 632 additions and 248 deletions.
2 changes: 1 addition & 1 deletion checkstyle-configuration.xml
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@

<module name="AvoidStaticImport">
<property name="excludes"
value="org.junit.Assert.*,org.junit.Assume.*,org.hamcrest.CoreMatchers.*,org.hamcrest.Matchers.*,org.eclipse.collections.impl.test.Verify.*,org.eclipse.collections.impl.factory.Iterables.*,org.eclipse.collections.test.IterableTestCase.*" />
value="org.junit.Assert.*,org.junit.Assume.*,org.hamcrest.CoreMatchers.*,org.hamcrest.Matchers.*,org.hamcrest.MatcherAssert.*,org.eclipse.collections.impl.test.Verify.*,org.eclipse.collections.impl.factory.Iterables.*,org.eclipse.collections.test.IterableTestCase.*" />
</module>

<module name="MissingDeprecated" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -728,7 +728,7 @@ public void remove()
}
}

private class KeySet implements Set<K>, Serializable
protected class KeySet implements Set<K>, Serializable
{
@Override
public boolean equals(Object obj)
Expand Down Expand Up @@ -847,7 +847,7 @@ public Iterator<K> iterator()
@Override
public String toString()
{
return Iterate.makeString(this, "[", ", ", "]");
return AbstractMutableBiMap.this.delegate.keySet().toString();
}

protected Object writeReplace()
Expand All @@ -858,7 +858,7 @@ protected Object writeReplace()
}
}

private class ValuesCollection implements Collection<V>
protected class ValuesCollection implements Collection<V>
{
@Override
public int size()
Expand Down Expand Up @@ -965,11 +965,11 @@ public Iterator<V> iterator()
@Override
public String toString()
{
return Iterate.makeString(this, "[", ", ", "]");
return AbstractMutableBiMap.this.delegate.values().toString();
}
}

private class EntrySet implements Set<Map.Entry<K, V>>
protected class EntrySet implements Set<Map.Entry<K, V>>
{
@Override
public boolean equals(Object obj)
Expand All @@ -991,6 +991,12 @@ public int hashCode()
return AbstractMutableBiMap.this.hashCode();
}

@Override
public String toString()
{
return AbstractMutableBiMap.this.delegate.entrySet().toString();
}

@Override
public int size()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bimap.ImmutableBiMap;
import org.eclipse.collections.api.bimap.MutableBiMap;
import org.eclipse.collections.api.block.function.Function;
Expand All @@ -36,7 +35,6 @@
import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable;
import org.eclipse.collections.impl.map.mutable.SynchronizedBiMapSerializationProxy;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;
import org.eclipse.collections.impl.utility.LazyIterate;

public class SynchronizedBiMap<K, V> extends AbstractSynchronizedMapIterable<K, V> implements MutableBiMap<K, V>, Serializable
{
Expand Down Expand Up @@ -269,18 +267,6 @@ public MutableBiMap<V, K> flipUniqueValues()
}
}

@Override
public RichIterable<K> keysView()
{
return LazyIterate.adapt(this.keySet());
}

@Override
public RichIterable<V> valuesView()
{
return LazyIterate.adapt(this.values());
}

@Override
public ImmutableBiMap<K, V> toImmutable()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -37,7 +37,6 @@
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.collection.AbstractSynchronizedRichIterable;
import org.eclipse.collections.impl.tuple.AbstractImmutableEntry;
import org.eclipse.collections.impl.utility.Iterate;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
Expand Down Expand Up @@ -350,15 +349,22 @@ public <K1, V1, V2> MutableMapIterable<K1, V2> aggregateBy(
}
}

@Override
public RichIterable<K> keysView()
{
return LazyIterate.adapt(this.keySet());
}

@Override
public RichIterable<V> valuesView()
{
return LazyIterate.adapt(this.values());
}

@Override
public RichIterable<Pair<K, V>> keyValuesView()
{
synchronized (this.lock)
{
Set<Entry<K, V>> entries = this.getDelegate().entrySet();
Iterable<Pair<K, V>> pairs = Iterate.collect(entries, AbstractImmutableEntry.getPairFunction());
return LazyIterate.adapt(pairs);
}
return LazyIterate.adapt(this.entrySet()).collect(AbstractImmutableEntry.getPairFunction());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand All @@ -16,7 +16,6 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.bag.primitive.MutableBooleanBag;
import org.eclipse.collections.api.bag.primitive.MutableByteBag;
Expand Down Expand Up @@ -54,7 +53,6 @@
import org.eclipse.collections.impl.list.fixed.ArrayAdapter;
import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
* A synchronized view of a {@link MutableMap}. It is imperative that the user manually synchronize on the collection when iterating over it using the
Expand Down Expand Up @@ -489,18 +487,6 @@ public Set<Entry<K, V>> entrySet()
}
}

@Override
public RichIterable<K> keysView()
{
return LazyIterate.adapt(this.keySet());
}

@Override
public RichIterable<V> valuesView()
{
return LazyIterate.adapt(this.values());
}

@Override
public MutableMap<K, V> asUnmodifiable()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -42,6 +42,7 @@
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.AppendStringProcedure;
import org.eclipse.collections.impl.block.procedure.MapCollectProcedure;
import org.eclipse.collections.impl.list.mutable.FastList;
import org.eclipse.collections.impl.parallel.BatchIterable;
Expand Down Expand Up @@ -2346,7 +2347,12 @@ public int hashCode()
@Override
public String toString()
{
return Iterate.makeString(this, "[", ", ", "]");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('[');
Procedure<K> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", ");
this.forEach(appendStringProcedure);
stringBuilder.append(']');
return stringBuilder.toString();
}

@Override
Expand Down Expand Up @@ -2897,6 +2903,17 @@ public int hashCode()
{
return UnifiedMap.this.hashCode();
}

@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('[');
Procedure<Entry<K, V>> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", ");
this.forEach(appendStringProcedure);
stringBuilder.append(']');
return stringBuilder.toString();
}
}

protected class EntrySetIterator extends PositionalIterator<Entry<K, V>>
Expand Down Expand Up @@ -3228,7 +3245,12 @@ private void chainedAddToList(Object[] chain, FastList<V> replace)
@Override
public String toString()
{
return Iterate.makeString(this, "[", ", ", "]");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('[');
Procedure<V> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", ");
this.forEach(appendStringProcedure);
stringBuilder.append(']');
return stringBuilder.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand All @@ -18,7 +18,6 @@
import java.util.SortedMap;

import org.eclipse.collections.api.LazyIterable;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.function.primitive.BooleanFunction;
Expand Down Expand Up @@ -63,7 +62,6 @@
import org.eclipse.collections.impl.map.AbstractSynchronizedMapIterable;
import org.eclipse.collections.impl.map.mutable.SynchronizedMapSerializationProxy;
import org.eclipse.collections.impl.set.mutable.SynchronizedMutableSet;
import org.eclipse.collections.impl.utility.LazyIterate;

/**
* A synchronized view of a SortedMap.
Expand Down Expand Up @@ -507,18 +505,6 @@ public <R> MutableSortedMap<K, R> collectValues(Function2<? super K, ? super V,
}
}

@Override
public RichIterable<K> keysView()
{
return LazyIterate.adapt(this.keySet());
}

@Override
public RichIterable<V> valuesView()
{
return LazyIterate.adapt(this.values());
}

@Override
public MutableSortedMap<K, V> asUnmodifiable()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -39,6 +39,7 @@
import org.eclipse.collections.api.tuple.Pair;
import org.eclipse.collections.impl.block.factory.Functions;
import org.eclipse.collections.impl.block.factory.Predicates;
import org.eclipse.collections.impl.block.procedure.AppendStringProcedure;
import org.eclipse.collections.impl.block.procedure.MapCollectProcedure;
import org.eclipse.collections.impl.factory.HashingStrategyMaps;
import org.eclipse.collections.impl.list.mutable.FastList;
Expand Down Expand Up @@ -1959,7 +1960,12 @@ public int hashCode()
@Override
public String toString()
{
return Iterate.makeString(this, "[", ", ", "]");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('[');
Procedure<K> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", ");
this.forEach(appendStringProcedure);
stringBuilder.append(']');
return stringBuilder.toString();
}

@Override
Expand Down Expand Up @@ -2513,6 +2519,17 @@ public int hashCode()
{
return UnifiedMapWithHashingStrategy.this.hashCode();
}

@Override
public String toString()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('[');
Procedure<Entry<K, V>> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", ");
this.forEach(appendStringProcedure);
stringBuilder.append(']');
return stringBuilder.toString();
}
}

protected class EntrySetIterator extends PositionalIterator<Entry<K, V>>
Expand Down Expand Up @@ -2852,7 +2869,12 @@ private void chainedAddToList(Object[] chain, FastList<V> replace)
@Override
public String toString()
{
return Iterate.makeString(this, "[", ", ", "]");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append('[');
Procedure<V> appendStringProcedure = new AppendStringProcedure<>(stringBuilder, ", ");
this.forEach(appendStringProcedure);
stringBuilder.append(']');
return stringBuilder.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs.
* Copyright (c) 2022 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -86,6 +86,7 @@
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
import org.eclipse.collections.impl.multimap.list.FastListMultimap;
import org.eclipse.collections.impl.multimap.set.UnifiedSetMultimap;
import org.eclipse.collections.impl.parallel.BatchIterable;
import org.eclipse.collections.impl.utility.internal.DefaultSpeciesNewStrategy;
import org.eclipse.collections.impl.utility.internal.IterableIterate;
import org.eclipse.collections.impl.utility.internal.RandomAccessListIterate;
Expand Down Expand Up @@ -130,6 +131,10 @@ public static <T> void forEach(Iterable<T> iterable, Procedure<? super T> proced
{
((InternalIterable<T>) iterable).forEach(procedure);
}
else if (iterable instanceof BatchIterable)
{
((BatchIterable<T>) iterable).forEach(procedure);
}
else if (iterable instanceof ArrayList)
{
ArrayListIterate.forEach((ArrayList<T>) iterable, procedure);
Expand Down

0 comments on commit 3163e7b

Please sign in to comment.