Skip to content

Commit

Permalink
[GSCOLLECT-411] Implement SortedBag.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://gscollections.svn.services.gs.com/svnroot/gscollections-svn/trunk@430 d5c9223b-1aff-41ac-aadd-f810b4a99ac4
  • Loading branch information
Kristen O'Leary committed Oct 16, 2013
1 parent 2a21442 commit a28a7ee
Show file tree
Hide file tree
Showing 48 changed files with 5,648 additions and 664 deletions.
94 changes: 47 additions & 47 deletions collections-api/src/main/java/com/gs/collections/api/bag/Bag.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,53 +54,6 @@
public interface Bag<T>
extends RichIterable<T>
{
/**
* The size of the Bag when counting only distinct elements.
*/
int sizeDistinct();

/**
* The occurrences of a distinct item in the bag.
*/
int occurrencesOf(Object item);

/**
* For each distinct item, with the number of occurrences, execute the specified procedure.
*/
void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure);

/**
* Converts the Bag to a Map of the Item type to its count as an Integer.
*/
MutableMap<T, Integer> toMapOfItemToCount();

/**
* Returns a string representation of this bag. The string representation consists of a list of element-count mappings.
* The elements each appear once, in an order consistent with other methods like {@link #forEachWithOccurrences(ObjectIntProcedure)}
* and {@link #iterator()}. The element-count mappings are enclosed in braces (<tt>"{}"</tt>). Adjacent mappings are
* separated by the characters <tt>", "</tt> (comma and space). Each element-count mapping is rendered as the element
* followed by an equals sign (<tt>"="</tt>) followed by the number of ooccurrences. Elements and are converted to
* strings as by {@link String#valueOf(Object)}.
* <p/>
* The string representation is similar to {@link java.util.AbstractMap#toString()}, not {@link RichIterable#toString()}.
*
* @return a string representation of this bag
* @since 3.0
*/
String toStringOfItemToCount();

/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 3.0
*/
Bag<T> selectByOccurrences(IntPredicate predicate);

/**
* Convert the Bag to an ImmutableBag. If the bag is immutable, it returns itself.
*/
ImmutableBag<T> toImmutable();

/**
* Two bags<tt>b1</tt> and <tt>b2</tt> are equal if <tt>m1.toMapOfItemToCount().equals(m2.toMapOfItemToCount())</tt>.
*
Expand Down Expand Up @@ -154,4 +107,51 @@ public interface Bag<T>
<S> Bag<Pair<T, S>> zip(Iterable<S> that);

Bag<Pair<T, Integer>> zipWithIndex();

/**
* For each distinct item, with the number of occurrences, execute the specified procedure.
*/
void forEachWithOccurrences(ObjectIntProcedure<? super T> procedure);

/**
* The occurrences of a distinct item in the bag.
*/
int occurrencesOf(Object item);

/**
* Returns all elements of the bag that have a number of occurrences that satisfy the predicate.
*
* @since 3.0
*/
Bag<T> selectByOccurrences(IntPredicate predicate);

/**
* The size of the Bag when counting only distinct elements.
*/
int sizeDistinct();

/**
* Convert the Bag to an ImmutableBag. If the bag is immutable, it returns itself.
*/
ImmutableBag<T> toImmutable();

/**
* Converts the Bag to a Map of the Item type to its count as an Integer.
*/
MutableMap<T, Integer> toMapOfItemToCount();

/**
* Returns a string representation of this bag. The string representation consists of a list of element-count mappings.
* The elements each appear once, in an order consistent with other methods like {@link #forEachWithOccurrences(ObjectIntProcedure)}
* and {@link #iterator()}. The element-count mappings are enclosed in braces (<tt>"{}"</tt>). Adjacent mappings are
* separated by the characters <tt>", "</tt> (comma and space). Each element-count mapping is rendered as the element
* followed by an equals sign (<tt>"="</tt>) followed by the number of ooccurrences. Elements and are converted to
* strings as by {@link String#valueOf(Object)}.
* <p/>
* The string representation is similar to {@link java.util.AbstractMap#toString()}, not {@link RichIterable#toString()}.
*
* @return a string representation of this bag
* @since 3.0
*/
String toStringOfItemToCount();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
public interface MutableBag<T>
extends Bag<T>, MutableCollection<T>
{
MutableBag<T> selectByOccurrences(IntPredicate predicate);

ImmutableBag<T> toImmutable();

MutableBag<T> with(T element);

MutableBag<T> without(T element);
Expand All @@ -62,46 +66,30 @@ public interface MutableBag<T>

MutableBag<T> newEmpty();

void addOccurrences(T item, int occurrences);

boolean removeOccurrences(Object item, int occurrences);

MutableBag<T> selectByOccurrences(IntPredicate predicate);
<P> MutableBag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter);

MutableBag<T> select(Predicate<? super T> predicate);
<P> MutableBag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter);

<P> MutableBag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter);
<P, V> MutableBag<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter);

MutableBag<T> reject(Predicate<? super T> predicate);
MutableBag<T> asUnmodifiable();

<P> MutableBag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter);
MutableBag<T> asSynchronized();

PartitionMutableBag<T> partition(Predicate<? super T> predicate);

<S> MutableBag<S> selectInstancesOf(Class<S> clazz);

<V> MutableBag<V> collect(Function<? super T, ? extends V> function);

MutableBooleanBag collectBoolean(BooleanFunction<? super T> booleanFunction);

MutableByteBag collectByte(ByteFunction<? super T> byteFunction);

MutableCharBag collectChar(CharFunction<? super T> charFunction);

MutableDoubleBag collectDouble(DoubleFunction<? super T> doubleFunction);

MutableFloatBag collectFloat(FloatFunction<? super T> floatFunction);

MutableIntBag collectInt(IntFunction<? super T> intFunction);

MutableLongBag collectLong(LongFunction<? super T> longFunction);

MutableShortBag collectShort(ShortFunction<? super T> shortFunction);

<P, V> MutableBag<V> collectWith(Function2<? super T, ? super P, ? extends V> function, P parameter);

<V> MutableBag<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function);

<V> MutableBag<V> flatCollect(Function<? super T, ? extends Iterable<V>> function);

<V> MutableBagMultimap<V, T> groupBy(Function<? super T, ? extends V> function);
Expand All @@ -112,9 +100,21 @@ public interface MutableBag<T>

MutableBag<Pair<T, Integer>> zipWithIndex();

ImmutableBag<T> toImmutable();
MutableBag<T> select(Predicate<? super T> predicate);

MutableBag<T> asUnmodifiable();
MutableBag<T> reject(Predicate<? super T> predicate);

MutableBag<T> asSynchronized();
<S> MutableBag<S> selectInstancesOf(Class<S> clazz);

MutableByteBag collectByte(ByteFunction<? super T> byteFunction);

MutableCharBag collectChar(CharFunction<? super T> charFunction);

MutableIntBag collectInt(IntFunction<? super T> intFunction);

<V> MutableBag<V> collectIf(Predicate<? super T> predicate, Function<? super T, ? extends V> function);

void addOccurrences(T item, int occurrences);

boolean removeOccurrences(Object item, int occurrences);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2013 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.api.bag.sorted;

import com.gs.collections.api.bag.ImmutableBag;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.multimap.sortedbag.ImmutableSortedBagMultimap;
import com.gs.collections.api.partition.bag.sorted.PartitionImmutableSortedBag;
import net.jcip.annotations.Immutable;

/**
* ImmutableSortedBag is the non-modifiable equivalent interface to {@link MutableSortedBag}.
*
* @since 4.2
*/
@Immutable
public interface ImmutableSortedBag<T>
extends ImmutableBag<T>, SortedBag<T>
{
ImmutableSortedBag<T> newWith(T element);

ImmutableSortedBag<T> newWithout(T element);

ImmutableSortedBag<T> newWithAll(Iterable<? extends T> elements);

ImmutableSortedBag<T> newWithoutAll(Iterable<? extends T> elements);

ImmutableSortedBag<T> selectByOccurrences(IntPredicate predicate);

ImmutableSortedBag<T> select(Predicate<? super T> predicate);

ImmutableSortedBag<T> reject(Predicate<? super T> predicate);

PartitionImmutableSortedBag<T> partition(Predicate<? super T> predicate);

<S> ImmutableSortedBag<S> selectInstancesOf(Class<S> clazz);

<V> ImmutableSortedBagMultimap<V, T> groupBy(Function<? super T, ? extends V> function);

<V> ImmutableSortedBagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function);

//Not yet supported

//<S> ImmutableBag<Pair<T, S>> zip(Iterable<S> that);

//ImmutableBag<Pair<T, Integer>> zipWithIndex();

// <K, V> SortedMapIterable<K, V> aggregateInPlaceBy(
// Function<? super T, ? extends K> groupBy,
// Function0<? extends V> zeroValueFactory,
// Procedure2<? super V, ? super T> mutatingAggregator);
//
// <K, V> SortedMapIterable<K, V> aggregateBy(
// Function<? super T, ? extends K> groupBy,
// Function0<? extends V> zeroValueFactory,
// Function2<? super V, ? super T, ? extends V> nonMutatingAggregator);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 2013 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.api.bag.sorted;

import com.gs.collections.api.bag.MutableBag;
import com.gs.collections.api.block.function.Function;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.predicate.Predicate2;
import com.gs.collections.api.block.predicate.primitive.IntPredicate;
import com.gs.collections.api.multimap.sortedbag.MutableSortedBagMultimap;
import com.gs.collections.api.partition.bag.sorted.PartitionMutableSortedBag;

/**
* @since 4.2
*/
public interface MutableSortedBag<T>
extends SortedBag<T>, MutableBag<T>, Cloneable
{
MutableSortedBag<T> selectByOccurrences(IntPredicate predicate);

MutableSortedBag<T> with(T element);

MutableSortedBag<T> without(T element);

MutableSortedBag<T> withAll(Iterable<? extends T> elements);

MutableSortedBag<T> withoutAll(Iterable<? extends T> elements);

MutableSortedBag<T> newEmpty();

<P> MutableSortedBag<T> selectWith(Predicate2<? super T, ? super P> predicate, P parameter);

<P> MutableSortedBag<T> rejectWith(Predicate2<? super T, ? super P> predicate, P parameter);

/**
* Returns an unmodifable view of the set. The returned set will be <tt>Serializable</tt> if this set is <tt>Serializable</tt>.
*
* @return an unmodifiable view of this set
*/
MutableSortedBag<T> asUnmodifiable();

MutableSortedBag<T> asSynchronized();

MutableSortedBag<T> reject(Predicate<? super T> predicate);

<V> MutableSortedBagMultimap<V, T> groupBy(Function<? super T, ? extends V> function);

MutableSortedBag<T> select(Predicate<? super T> predicate);

PartitionMutableSortedBag<T> partition(Predicate<? super T> predicate);

<S> MutableSortedBag<S> selectInstancesOf(Class<S> clazz);

<V> MutableSortedBagMultimap<V, T> groupByEach(Function<? super T, ? extends Iterable<V>> function);

// Not yet supported
// <S> MutableBag<Pair<T, S>> zip(Iterable<S> that);
//
// MutableBag<Pair<T, Integer>> zipWithIndex();
//
// <K, V> SortedMapIterable<K, V> aggregateInPlaceBy(
// Function<? super T, ? extends K> groupBy,
// Function0<? extends V> zeroValueFactory,
// Procedure2<? super V, ? super T> mutatingAggregator);
//
// <K, V> SortedMapIterable<K, V> aggregateBy(
// Function<? super T, ? extends K> groupBy,
// Function0<? extends V> zeroValueFactory,
// Function2<? super V, ? super T, ? extends V> nonMutatingAggregator);
// MutableSortedMap<T, Integer> toMapOfItemToCount();
}
Loading

0 comments on commit a28a7ee

Please sign in to comment.