Class WordBuilder<I>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<I>
-
- net.automatalib.word.WordBuilder<I>
-
- Type Parameters:
I
- symbol class.
- All Implemented Interfaces:
Iterable<I>
,Collection<I>
,List<I>
public final class WordBuilder<I> extends AbstractList<I>
A class for dynamically buildingWord
s.As
Word
s are - like strings - immutable objects, constructing them by subsequent invocations ofWord.concat(Word...)
etc. is highly inefficient. This class provides an efficient means of construction by operating on an internal storage during construction, only creating aWord
(and thus requiring to ensure immutability) when the methodtoWord()
(ortoWord(int, int)
) is invoked.Note that due to the specifics of the underlying word implementation, even after an invocation of
toWord()
the storage does not have to be duplicated unless it either is required due to capacity adjustment or a non-appending change (such assetSymbol(int, Object)
ortruncate(int)
) is made.Nearly all modification methods of this class return a
this
-reference, allowing constructs such asbuilder.append(foo).append(bar).append(baz)
.
-
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description WordBuilder()
Constructor.WordBuilder(int initialCapacity)
Constructor.WordBuilder(int capacity, I initSym, int count)
Constructor.WordBuilder(int capacity, Word<I> init)
Constructor.WordBuilder(I initSym, int count)
Constructor.WordBuilder(Word<I> init)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(I e)
WordBuilder<I>
append(I symbol)
Appends a symbol to the contents of the internal storage.WordBuilder<I>
append(I... symbols)
Appends several symbols to the contents of the internal storage.WordBuilder<I>
append(List<? extends I> symList)
WordBuilder<I>
append(Word<? extends I> word)
Appends a word to the contents of the internal storage.WordBuilder<I>
append(Word<? extends I>... words)
Appends several words to the contents of the internal storage.void
clear()
void
ensureAdditionalCapacity(int add)
Ensures that the internal storage has additionally the given capacity.void
ensureCapacity(int cap)
Ensures that the internal storage has in total the given capacity.I
get(int index)
I
getSymbol(int index)
Retrieves the symbol at the given index.WordBuilder<I>
repeatAppend(int num, I symbol)
Appendsnum
copies of a symbol to the contents of the internal storage.WordBuilder<I>
repeatAppend(int num, Word<I> word)
Appendsnum
copies of the given word to the contents of the initial storage.WordBuilder<I>
reverse()
Reverses the contents of the internal buffer.I
set(int index, I element)
WordBuilder<I>
setSymbol(int index, I symbol)
Sets the symbol at the given index.int
size()
Word<I>
toWord()
Creates a word from the contents of the internal storage.Word<I>
toWord(int fromIndex, int toIndex)
Creates a word from the given range of the contents of the internal storage.WordBuilder<I>
truncate(int truncLen)
Truncates the contents of the initial storage to the given length.-
Methods inherited from class java.util.AbstractList
add, addAll, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, remove, removeRange, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
-
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
-
-
-
Constructor Detail
-
WordBuilder
public WordBuilder()
Constructor. Initializes the builder with a default capacity.
-
WordBuilder
public WordBuilder(int initialCapacity)
Constructor. Initializes the builder with the specified initial capacity.- Parameters:
initialCapacity
- the initial capacity of the internal storage.
-
WordBuilder
public WordBuilder(I initSym, int count)
Constructor. Initializes the builder with a sequence ofcount
times the specified symbol. Note that this constructor runs in constant time ifinitSym
isnull
.- Parameters:
initSym
- the initial symbolcount
- the initial symbol count
-
WordBuilder
public WordBuilder(int capacity, I initSym, int count)
Constructor. Initializes the builder with a sequence ofcount
times the specified symbol, while allocating the specified initial capacity.- Parameters:
capacity
- the initial capacity of the internal storage.initSym
- the initial symbolcount
- the initial symbol count
-
WordBuilder
public WordBuilder(Word<I> init)
Constructor. Initializes the builder with a given word.- Parameters:
init
- the word to initialize the builder with.
-
-
Method Detail
-
append
public WordBuilder<I> append(I symbol)
Appends a symbol to the contents of the internal storage.- Parameters:
symbol
- the symbol to append- Returns:
this
-
append
public WordBuilder<I> append(List<? extends I> symList)
-
append
public WordBuilder<I> append(Word<? extends I> word)
Appends a word to the contents of the internal storage.- Parameters:
word
- the word to append.- Returns:
this
-
append
@SafeVarargs public final WordBuilder<I> append(Word<? extends I>... words)
Appends several words to the contents of the internal storage.- Parameters:
words
- the words to append- Returns:
this
-
append
@SafeVarargs public final WordBuilder<I> append(I... symbols)
Appends several symbols to the contents of the internal storage.- Parameters:
symbols
- the symbols to append- Returns:
this
-
ensureAdditionalCapacity
public void ensureAdditionalCapacity(int add)
Ensures that the internal storage has additionally the given capacity.- Parameters:
add
- the additional capacity to ensure
-
ensureCapacity
public void ensureCapacity(int cap)
Ensures that the internal storage has in total the given capacity.- Parameters:
cap
- the minimum capacity to ensure
-
repeatAppend
public WordBuilder<I> repeatAppend(int num, Word<I> word)
Appendsnum
copies of the given word to the contents of the initial storage.- Parameters:
num
- the number of copiesword
- the word- Returns:
this
-
repeatAppend
public WordBuilder<I> repeatAppend(int num, I symbol)
Appendsnum
copies of a symbol to the contents of the internal storage.- Parameters:
num
- the number of copiessymbol
- the symbol- Returns:
this
-
truncate
public WordBuilder<I> truncate(int truncLen)
Truncates the contents of the initial storage to the given length.- Parameters:
truncLen
- the length to truncate to- Returns:
this
-
toWord
public Word<I> toWord(int fromIndex, int toIndex)
Creates a word from the given range of the contents of the internal storage. Note that the storage management mechanisms of this class guarantee that the returned word will not change regardless of what further operations are invoked on thisWordBuilder
.- Parameters:
fromIndex
- the starting index, inclusivetoIndex
- the end index, exclusive- Returns:
- the word for the specified subrange
-
toWord
public Word<I> toWord()
Creates a word from the contents of the internal storage. Note that the storage management mechanisms of this class guarantee that the returned word will not change regardless of what further operations are performed on thisWordBuilder
.- Returns:
- the internal contents as a word
-
add
public boolean add(I e)
- Specified by:
add
in interfaceCollection<I>
- Specified by:
add
in interfaceList<I>
- Overrides:
add
in classAbstractList<I>
-
get
public I get(int index)
-
getSymbol
public I getSymbol(int index)
Retrieves the symbol at the given index.- Parameters:
index
- the index to retrieve- Returns:
- the symbol at the given index
-
setSymbol
public WordBuilder<I> setSymbol(int index, I symbol)
Sets the symbol at the given index. Note that this index must exist.- Parameters:
index
- the index to manipulatesymbol
- the symbol to set- Returns:
this
-
clear
public void clear()
- Specified by:
clear
in interfaceCollection<I>
- Specified by:
clear
in interfaceList<I>
- Overrides:
clear
in classAbstractList<I>
-
size
public int size()
- Specified by:
size
in interfaceCollection<I>
- Specified by:
size
in interfaceList<I>
- Specified by:
size
in classAbstractCollection<I>
-
reverse
public WordBuilder<I> reverse()
Reverses the contents of the internal buffer.- Returns:
this
-
-