net.sf.staccatocommons.lang
Class CollectionBuilder<A,B extends Collection<A>>

java.lang.Object
  extended by net.sf.staccatocommons.lang.CollectionBuilder<A,B>
All Implemented Interfaces:
Builder<B>

public class CollectionBuilder<A,B extends Collection<A>>
extends Object
implements Builder<B>

a Builder for Collections. With the exception of from(Collection, Applicable), all its factory methods grant to return CollectionBuilder that build Unmodifiable collections

Author:
flbulgarelli

Constructor Summary
CollectionBuilder(B list, net.sf.staccatocommons.defs.Applicable<B,B> wrapperFunction)
          Creates a new CollectionBuilder
 
Method Summary
 B build()
          Returns the built object.
static
<A> CollectionBuilder<A,Collection<A>>
from(Collection<A> collection)
          Answers a new CollectionBuilder that configures a Collection
static
<A> CollectionBuilder<A,List<A>>
listWith(A element)
          Answers a new CollectionBuilder that configures a List, with the given element already added.
static
<A> CollectionBuilder<A,Set<A>>
setWith(A element)
          Answers a new CollectionBuilder that configures a Set, with the given element already added
static
<A> CollectionBuilder<A,SortedSet<A>>
sortedSetWith(A element)
          Answers a new CollectionBuilder that configures a SortedSet , with the given element already added
static
<A> net.sf.staccatocommons.defs.Applicable<Collection<A>,Collection<A>>
toUnmodifiableCollection()
          Answers a constant function that returns an Unmodifiable view of its collection argument
static
<A> net.sf.staccatocommons.defs.Applicable<List<A>,List<A>>
toUnmodifiableList()
          Answers a constant function that returns an Unmodifiable view of its list argument
static
<A> net.sf.staccatocommons.defs.Applicable<Set<A>,Set<A>>
toUnmodifiableSet()
          Answers a constant function that returns an Unmodifiable view of its SortedSet argument
static
<A> net.sf.staccatocommons.defs.Applicable<SortedSet<A>,SortedSet<A>>
toUnmodifiableSortedSet()
          Answers a constant function that returns an Unmodifiable view of its SortedSet argument
 CollectionBuilder<A,B> unwrap()
          Disables wrapping, which means that a built collection will be returned just as it was created.
 CollectionBuilder<A,B> with(A element)
          Adds an element to the list
 CollectionBuilder<A,B> withIf(A element, net.sf.staccatocommons.defs.Evaluable<? super A> evaluable)
          Adds an element to the list if it satisfies the given condition
 CollectionBuilder<A,B> withWrapper(net.sf.staccatocommons.defs.Applicable<B,B> wrapperFunction)
          Sets the wrapper function, that is, the Applicable to apply to the resulting collection before returning it through build().
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CollectionBuilder

public CollectionBuilder(@NonNull
                         B list,
                         net.sf.staccatocommons.defs.Applicable<B,B> wrapperFunction)
Creates a new CollectionBuilder

Parameters:
list - the list to be configured by this builder
wrapperFunction -
Method Detail

with

@NonNull
public CollectionBuilder<A,B> with(A element)
Adds an element to the list

Parameters:
element -
Returns:
this builder

withIf

@NonNull
public CollectionBuilder<A,B> withIf(A element,
                                             @NonNull
                                             net.sf.staccatocommons.defs.Evaluable<? super A> evaluable)
Adds an element to the list if it satisfies the given condition

Parameters:
element -
evaluable -
Returns:
this

withWrapper

@NonNull
public CollectionBuilder<A,B> withWrapper(@NonNull
                                                  net.sf.staccatocommons.defs.Applicable<B,B> wrapperFunction)
Sets the wrapper function, that is, the Applicable to apply to the resulting collection before returning it through build(). By default, collections are wrapped with unmodifiable wrappers, but this behavior can be changed through this method.

Parameters:
wrapperFunction -
Returns:
this

unwrap

@NonNull
public CollectionBuilder<A,B> unwrap()
Disables wrapping, which means that a built collection will be returned just as it was created. As a consequence, the collection returned by build() will be modifiable.

Returns:
this

build

@NonNull
public B build()
                              throws BuilderAlreadyUsedException
Description copied from interface: Builder
Returns the built object.

Builders are not meant to be used more than once. So this method can only be invoked successfully one time. Subsequent invocations after a successful return will throw an BuilderAlreadyUsedException. However, implementors may relax this, and be reusable, that is being able to be used more than once; such implementors should document that feature.

Builders may impose restriction to the state of the object under construction. If such constraints are not met while invoking this method, a ObjectUnderConstructionException exception must be thrown.

Specified by:
build in interface Builder<B extends Collection<A>>
Returns:
The built object.
Throws:
BuilderAlreadyUsedException - If the building process has finished, but this method has already being invoked without throwing an exception.

from

@NonNull
public static <A> CollectionBuilder<A,Collection<A>> from(@NonNull
                                                                  Collection<A> collection)
Answers a new CollectionBuilder that configures a Collection

Type Parameters:
A -
Returns:
a new CollectionBuilder that builds a Collection

sortedSetWith

@NonNull
public static <A> CollectionBuilder<A,SortedSet<A>> sortedSetWith(A element)
Answers a new CollectionBuilder that configures a SortedSet , with the given element already added

Type Parameters:
A -
Returns:
a new CollectionBuilder that builds a SortedSet

setWith

@NonNull
public static <A> CollectionBuilder<A,Set<A>> setWith(A element)
Answers a new CollectionBuilder that configures a Set, with the given element already added

Type Parameters:
A -
Returns:
a new CollectionBuilder that builds a set

listWith

@NonNull
public static <A> CollectionBuilder<A,List<A>> listWith(A element)
Answers a new CollectionBuilder that configures a List, with the given element already added. The list built this builder grants to be Unmodifiable

Type Parameters:
A -
Returns:
a new CollectionBuilder that builds lists

toUnmodifiableSet

@Constant
public static <A> net.sf.staccatocommons.defs.Applicable<Set<A>,Set<A>> toUnmodifiableSet()
Answers a constant function that returns an Unmodifiable view of its SortedSet argument

Type Parameters:
A -
Returns:
a function

toUnmodifiableSortedSet

@Constant
public static <A> net.sf.staccatocommons.defs.Applicable<SortedSet<A>,SortedSet<A>> toUnmodifiableSortedSet()
Answers a constant function that returns an Unmodifiable view of its SortedSet argument

Type Parameters:
A -
Returns:
a function

toUnmodifiableList

@Constant
public static <A> net.sf.staccatocommons.defs.Applicable<List<A>,List<A>> toUnmodifiableList()
Answers a constant function that returns an Unmodifiable view of its list argument

Type Parameters:
A -
Returns:
a function

toUnmodifiableCollection

@Constant
public static <A> net.sf.staccatocommons.defs.Applicable<Collection<A>,Collection<A>> toUnmodifiableCollection()
Answers a constant function that returns an Unmodifiable view of its collection argument

Type Parameters:
A -
Returns:
a function


Copyright © 2010-2012 Staccatocommons. All Rights Reserved.