Skip to content

Commit

Permalink
Extract commonality from HashBag and HashBagWithHashingStrategy into …
Browse files Browse the repository at this point in the history
…AbstractHashBag.
  • Loading branch information
motlin authored and superhindupur committed Mar 29, 2016
1 parent 7ec64b4 commit d4ea738
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.map.MutableMap;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.Counter;
import org.eclipse.collections.impl.block.factory.primitive.IntToIntFunctions;
import org.eclipse.collections.impl.map.mutable.UnifiedMap;
Expand Down Expand Up @@ -70,6 +71,48 @@ public void addOccurrences(T item, int occurrences)
}
}

@Override
public boolean equals(Object other)
{
if (this == other)
{
return true;
}
if (!(other instanceof Bag))
{
return false;
}
final Bag<?> bag = (Bag<?>) other;
if (this.sizeDistinct() != bag.sizeDistinct())
{
return false;
}

return this.items.keyValuesView().allSatisfy(new Predicate<ObjectIntPair<T>>()
{
public boolean accept(ObjectIntPair<T> each)
{
return bag.occurrencesOf(each.getOne()) == each.getTwo();
}
});
}

@Override
public int hashCode()
{
final Counter counter = new Counter();
this.items.forEachKeyValue(new ObjectIntProcedure<T>()
{
public void value(T item, int count)
{
counter.add((item == null ? 0 : AbstractHashBag.this.computeHashCode(item)) ^ count);
}
});
return counter.getCount();
}

protected abstract int computeHashCode(T item);

public abstract MutableBag<T> selectByOccurrences(IntPredicate predicate);

@Override
Expand All @@ -88,9 +131,9 @@ public int occurrencesOf(Object item)
return this.items.get(item);
}

public void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure)
public void forEachWithOccurrences(ObjectIntProcedure<? super T> objectIntProcedure)
{
this.items.forEachKeyValue(procedure);
this.items.forEachKeyValue(objectIntProcedure);
}

public MutableMap<T, Integer> toMapOfItemToCount()
Expand All @@ -106,6 +149,13 @@ public void value(T item, int count)
return map;
}

public boolean add(T item)
{
this.items.updateValue(item, 0, IntToIntFunctions.increment());
this.size++;
return true;
}

public boolean remove(Object item)
{
int newValue = this.items.updateValue((T) item, 0, IntToIntFunctions.decrement());
Expand Down Expand Up @@ -133,11 +183,6 @@ public boolean isEmpty()
return this.items.isEmpty();
}

public int size()
{
return this.size;
}

public void each(final Procedure<? super T> procedure)
{
this.items.forEachKeyValue(new ObjectIntProcedure<T>()
Expand Down Expand Up @@ -299,6 +344,11 @@ public void value(Object each, int parameter)
return this.size != oldSize;
}

public int size()
{
return this.size;
}

@Override
public boolean contains(Object o)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@

import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.ObjectIntPredicate;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.Counter;
import org.eclipse.collections.impl.block.factory.primitive.IntToIntFunctions;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMap;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
Expand Down Expand Up @@ -92,43 +87,9 @@ public static <E> HashBag<E> newBagWith(E... elements)
}

@Override
public boolean equals(Object other)
protected int computeHashCode(T item)
{
if (this == other)
{
return true;
}
if (!(other instanceof Bag))
{
return false;
}
final Bag<?> bag = (Bag<?>) other;
if (this.sizeDistinct() != bag.sizeDistinct())
{
return false;
}

return this.items.keyValuesView().allSatisfy(new Predicate<ObjectIntPair<T>>()
{
public boolean accept(ObjectIntPair<T> each)
{
return bag.occurrencesOf(each.getOne()) == each.getTwo();
}
});
}

@Override
public int hashCode()
{
final Counter counter = new Counter();
this.items.forEachKeyValue(new ObjectIntProcedure<T>()
{
public void value(T item, int count)
{
counter.add((item == null ? 0 : item.hashCode()) ^ count);
}
});
return counter.getCount();
return item.hashCode();
}

@Override
Expand Down Expand Up @@ -198,13 +159,6 @@ public HashBag<T> with(T element1, T element2)
return this;
}

public boolean add(T item)
{
this.items.updateValue(item, 0, IntToIntFunctions.increment());
this.size++;
return true;
}

public HashBag<T> with(T element1, T element2, T element3)
{
this.add(element1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.predicate.primitive.ObjectIntPredicate;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.api.tuple.primitive.ObjectIntPair;
import org.eclipse.collections.impl.Counter;
import org.eclipse.collections.impl.bag.mutable.AbstractHashBag;
import org.eclipse.collections.impl.block.factory.primitive.IntToIntFunctions;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMapWithHashingStrategy;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;
Expand Down Expand Up @@ -102,43 +97,9 @@ public HashingStrategy<? super T> hashingStrategy()
}

@Override
public boolean equals(Object other)
protected int computeHashCode(T item)
{
if (this == other)
{
return true;
}
if (!(other instanceof Bag))
{
return false;
}
final Bag<?> bag = (Bag<?>) other;
if (this.sizeDistinct() != bag.sizeDistinct())
{
return false;
}

return this.items.keyValuesView().allSatisfy(new Predicate<ObjectIntPair<T>>()
{
public boolean accept(ObjectIntPair<T> each)
{
return bag.occurrencesOf(each.getOne()) == each.getTwo();
}
});
}

@Override
public int hashCode()
{
final Counter counter = new Counter();
this.items.forEachKeyValue(new ObjectIntProcedure<T>()
{
public void value(T item, int count)
{
counter.add((item == null ? 0 : HashBagWithHashingStrategy.this.hashingStrategy.computeHashCode(item)) ^ count);
}
});
return counter.getCount();
return this.hashingStrategy.computeHashCode(item);
}

@Override
Expand Down Expand Up @@ -187,11 +148,4 @@ public MutableBag<T> newEmpty()
{
return HashBagWithHashingStrategy.newBag(this.hashingStrategy);
}

public boolean add(T item)
{
this.items.updateValue(item, 0, IntToIntFunctions.increment());
this.size++;
return true;
}
}

0 comments on commit d4ea738

Please sign in to comment.