
org.eclipse.collections.impl.bag.mutable.AbstractHashBag Maven / Gradle / Ivy
/*
* Copyright (c) 2016 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.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.bag.mutable;
import java.util.Collection;
import java.util.Iterator;
import org.eclipse.collections.api.RichIterable;
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
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;
import org.eclipse.collections.impl.multimap.bag.HashBagMultimap;
public abstract class AbstractHashBag extends AbstractMutableBag
{
protected MutableObjectIntMap items;
protected int size;
@Override
public boolean addAll(Collection extends T> source)
{
if (source instanceof Bag)
{
return this.addAllBag((Bag) source);
}
return super.addAll(source);
}
protected boolean addAllBag(Bag extends T> source)
{
source.forEachWithOccurrences(new ObjectIntProcedure()
{
public void value(T each, int occurrences)
{
AbstractHashBag.this.addOccurrences(each, occurrences);
}
});
return source.notEmpty();
}
public void addOccurrences(T item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot add a negative number of occurrences");
}
if (occurrences > 0)
{
this.items.updateValue(item, 0, IntToIntFunctions.add(occurrences));
this.size += 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>()
{
public boolean accept(ObjectIntPair each)
{
return bag.occurrencesOf(each.getOne()) == each.getTwo();
}
});
}
@Override
public int hashCode()
{
final Counter counter = new Counter();
this.items.forEachKeyValue(new ObjectIntProcedure()
{
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 selectByOccurrences(IntPredicate predicate);
@Override
protected RichIterable getKeysView()
{
return this.items.keysView();
}
public int sizeDistinct()
{
return this.items.size();
}
public int occurrencesOf(Object item)
{
return this.items.get(item);
}
public void forEachWithOccurrences(ObjectIntProcedure super T> objectIntProcedure)
{
this.items.forEachKeyValue(objectIntProcedure);
}
public MutableMap toMapOfItemToCount()
{
final MutableMap map = UnifiedMap.newMap(this.items.size());
this.forEachWithOccurrences(new ObjectIntProcedure()
{
public void value(T item, int count)
{
map.put(item, 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());
if (newValue <= 0)
{
this.items.removeKey((T) item);
if (newValue == -1)
{
return false;
}
}
this.size--;
return true;
}
public void clear()
{
this.items.clear();
this.size = 0;
}
@Override
public boolean isEmpty()
{
return this.items.isEmpty();
}
public void each(final Procedure super T> procedure)
{
this.items.forEachKeyValue(new ObjectIntProcedure()
{
public void value(T key, int count)
{
for (int i = 0; i < count; i++)
{
procedure.value(key);
}
}
});
}
@Override
public void forEachWithIndex(final ObjectIntProcedure super T> objectIntProcedure)
{
final Counter index = new Counter();
this.items.forEachKeyValue(new ObjectIntProcedure()
{
public void value(T key, int count)
{
for (int i = 0; i < count; i++)
{
objectIntProcedure.value(key, index.getCount());
index.increment();
}
}
});
}
@Override
public void forEachWith(final Procedure2 super T, ? super P> procedure, final P parameter)
{
this.items.forEachKeyValue(new ObjectIntProcedure()
{
public void value(T key, int count)
{
for (int i = 0; i < count; i++)
{
procedure.value(key, parameter);
}
}
});
}
public Iterator iterator()
{
return new InternalIterator();
}
public boolean removeOccurrences(Object item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot remove a negative number of occurrences");
}
if (occurrences == 0)
{
return false;
}
int newValue = this.items.updateValue((T) item, 0, IntToIntFunctions.subtract(occurrences));
if (newValue <= 0)
{
this.size -= occurrences + newValue;
this.items.remove(item);
return newValue + occurrences != 0;
}
this.size -= occurrences;
return true;
}
public boolean setOccurrences(T item, int occurrences)
{
if (occurrences < 0)
{
throw new IllegalArgumentException("Cannot set a negative number of occurrences");
}
int originalOccurrences = this.items.get(item);
if (originalOccurrences == occurrences)
{
return false;
}
if (occurrences == 0)
{
this.items.remove(item);
}
else
{
this.items.put(item, occurrences);
}
this.size -= originalOccurrences - occurrences;
return true;
}
public boolean removeIf(Predicate super T> predicate)
{
boolean changed = false;
for (Iterator iterator = this.items.keySet().iterator(); iterator.hasNext(); )
{
T key = iterator.next();
if (predicate.accept(key))
{
this.size -= this.items.get(key);
iterator.remove();
changed = true;
}
}
return changed;
}
public boolean removeIfWith(Predicate2 super T, ? super P> predicate, P parameter)
{
boolean changed = false;
for (Iterator iterator = this.items.keySet().iterator(); iterator.hasNext(); )
{
T key = iterator.next();
if (predicate.accept(key, parameter))
{
this.size -= this.items.get(key);
iterator.remove();
changed = true;
}
}
return changed;
}
public boolean removeAllIterable(Iterable> iterable)
{
int oldSize = this.size;
if (iterable instanceof Bag)
{
Bag> source = (Bag>) iterable;
source.forEachWithOccurrences(new ObjectIntProcedure