
org.magicwerk.brownies.collections.Key2List Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of brownies-collections Show documentation
Show all versions of brownies-collections Show documentation
Brownies Collections complements the Java Collections Framework.
GapList combines the strengths of both ArrayList and LinkedList.
BigList is a list optimized for storing large number of elements.
There are specialized List implementations for all primitive data types (IntGapList, IntBigList, IntObjGapList, IntObjBigList).
The key collection classes offer support for keys and constraints for lists and collections
(KeyList, KeyCollection, KeySet, Key1List, Key1Collection, Key1Set, Key2List, Key2Collection, Key2Set).
/*
* Copyright 2013 by Thomas Mauch
*
* 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.
*
* $Id: Key2List.java 3124 2016-04-05 23:13:35Z origo $
*/
package org.magicwerk.brownies.collections;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import org.magicwerk.brownies.collections.KeyCollectionImpl.BuilderImpl;
import org.magicwerk.brownies.collections.function.IConsumer;
import org.magicwerk.brownies.collections.function.IFunction;
import org.magicwerk.brownies.collections.function.IPredicate;
/**
* Key2List implements a key list with 2 keys.
* These keys can be accessed fast.
* It can provide fast access to its elements like a Set.
* The elements allowed in the list can be constraint (null/duplicate values).
*
* @author Thomas Mauch
* @version $Id: Key2List.java 3124 2016-04-05 23:13:35Z origo $
*
* @param type of elements stored in the list
* @param type of first key
* @param type of second key
*/
@SuppressWarnings("serial")
public class Key2List extends KeyListImpl {
/**
* Builder to construct Key2List instances.
*/
public static class Builder extends BuilderImpl {
/**
* Default constructor.
*/
public Builder() {
this(null);
}
/**
* Private constructor used if extending Key2List.
*
* @param keyList key list
*/
Builder(Key2List keyList) {
this.keyList = keyList;
initKeyMapBuilder(2);
}
// -- Constraint
@Override
public Builder withNull(boolean allowNull) {
return (Builder) super.withNull(allowNull);
}
@Override
public Builder withConstraint(IPredicate constraint) {
return (Builder) super.withConstraint(constraint);
}
// -- Triggers
@Override
public Builder withBeforeInsertTrigger(IConsumer trigger) {
return (Builder) super.withBeforeInsertTrigger(trigger);
}
@Override
public Builder withAfterInsertTrigger(IConsumer trigger) {
return (Builder) super.withAfterInsertTrigger(trigger);
}
@Override
public Builder withBeforeDeleteTrigger(IConsumer trigger) {
return (Builder) super.withBeforeDeleteTrigger(trigger);
}
@Override
public Builder withAfterDeleteTrigger(IConsumer trigger) {
return (Builder) super.withAfterDeleteTrigger(trigger);
}
//-- Content
@Override
public Builder withCapacity(int capacity) {
return (Builder) super.withCapacity(capacity);
}
@Override
public Builder withContent(Collection extends E> elements) {
return (Builder) super.withContent(elements);
}
@Override
public Builder withContent(E... elements) {
return (Builder) super.withContent(elements);
}
@Override
public Builder withMaxSize(int maxSize) {
return (Builder) super.withMaxSize(maxSize);
}
@Override
public Builder withWindowSize(int maxSize) {
return (Builder) super.withWindowSize(maxSize);
}
@Override
public Builder withListBig(boolean bigList) {
return (Builder) super.withListBig(bigList);
}
//-- Element key
@Override
public Builder withElemSet() {
return (Builder) super.withElemSet();
}
@Override
public Builder withOrderByElem(boolean orderBy) {
return (Builder) super.withOrderByElem(orderBy);
}
@Override
public Builder withElemNull(boolean allowNull) {
return (Builder) super.withElemNull(allowNull);
}
@Override
public Builder withElemDuplicates(boolean allowDuplicates) {
return (Builder) super.withElemDuplicates(allowDuplicates);
}
@Override
public Builder withElemDuplicates(boolean allowDuplicates, boolean allowDuplicatesNull) {
return (Builder) super.withElemDuplicates(allowDuplicates, allowDuplicatesNull);
}
@Override
public Builder withElemSort(boolean sort) {
return (Builder) super.withElemSort(sort);
}
@Override
public Builder withElemSort(Comparator super E> comparator) {
return (Builder) super.withElemSort(comparator);
}
@Override
public Builder withElemSort(Comparator super E> comparator, boolean sortNullsFirst) {
return (Builder) super.withElemSort(comparator, sortNullsFirst);
}
@Override
public Builder withPrimaryElem() {
return (Builder) super.withPrimaryElem();
}
@Override
public Builder withUniqueElem() {
return (Builder) super.withUniqueElem();
}
// -- Key1
/**
* Add key map.
*
* @param mapper mapper to use
* @return this (fluent interface)
*/
public Builder withKey1Map(IFunction super E,K1> mapper) {
return (Builder) super.withKeyMap(1, mapper);
}
/**
* Specify this key to be a primary key.
* This is identical to calling
* withKey1Map(mapper), withKey1Null(false), and withKey1Duplicates(false).
*
* @param mapper mapper to use
* @return this (fluent interface)
*/
public Builder withPrimaryKey1Map(IFunction super E,K1> mapper) {
return (Builder) super.withPrimaryKeyMap(1, mapper);
}
/**
* Specify this key to be a unique key.
* This is identical to calling
* withKey1Map(mapper), withKey1Null(true), and withKey1Duplicates(false, true).
*
* @param mapper mapper to use
* @return this (fluent interface)
*/
public Builder withUniqueKey1Map(IFunction super E,K1> mapper) {
return (Builder) super.withUniqueKeyMap(1, mapper);
}
@Override
public Builder withOrderByKey1(boolean orderBy) {
return (Builder) super.withOrderByKey1(orderBy);
}
@Override
public Builder withOrderByKey1(Class> type) {
return (Builder) super.withOrderByKey1(type);
}
@Override
public Builder withKey1Null(boolean allowNull) {
return (Builder) super.withKey1Null(allowNull);
}
@Override
public Builder withKey1Duplicates(boolean allowDuplicates) {
return (Builder) super.withKey1Duplicates(allowDuplicates);
}
@Override
public Builder withKey1Duplicates(boolean allowDuplicates, boolean allowDuplicatesNull) {
return (Builder) super.withKey1Duplicates(allowDuplicates, allowDuplicatesNull);
}
@Override
public Builder withKey1Sort(boolean sort) {
return (Builder) super.withKey1Sort(sort);
}
/**
* Set comparator to use for sorting the key map.
* Note that this does not automatically sort the list itself, call a withOrderBy method for this.
*
* @param comparator comparator to use for sorting
* @return this (fluent interface)
*/
public Builder withKey1Sort(Comparator super K1> comparator) {
return (Builder) super.withKeySort(1, comparator);
}
/**
* Set comparator to use for sorting the key map.
* Note that this does not automatically sort the list itself, call a withOrderBy method for this.
*
* @param comparator comparator to use for sorting
* @param sortNullsFirst true if null will be sorted first, false for last
* @return this (fluent interface)
*/
public Builder withKey1Sort(Comparator super K1> comparator, boolean sortNullsFirst) {
return (Builder) super.withKeySort(1, comparator, sortNullsFirst);
}
// -- Key2
/**
* Add key map.
*
* @param mapper mapper to use
* @return this (fluent interface)
*/
public Builder withKey2Map(IFunction super E,K2> mapper) {
return (Builder) super.withKeyMap(2, mapper);
}
/**
* Specify this key to be a primary key.
* This is identical to calling
* withKey2Map(mapper), withKey2Null(false), and withKey2Duplicates(false).
*
* @param mapper mapper to use
* @return this (fluent interface)
*/
public Builder withPrimaryKey2Map(IFunction super E,K2> mapper) {
return (Builder) super.withPrimaryKeyMap(2, mapper);
}
/**
* Specify this key to be a unique key.
* This is identical to calling
* withKey2Map(mapper), withKey2Null(true), and withKey2Duplicates(false, true).
*
* @param mapper mapper to use
* @return this (fluent interface)
*/
public Builder withUniqueKey2Map(IFunction super E,K2> mapper) {
return (Builder) super.withUniqueKeyMap(2, mapper);
}
@Override
public Builder withOrderByKey2(boolean orderBy) {
return (Builder) super.withOrderByKey2(orderBy);
}
@Override
public Builder withOrderByKey2(Class> type) {
return (Builder) super.withOrderByKey2(type);
}
@Override
public Builder withKey2Null(boolean allowNull) {
return (Builder) super.withKey2Null(allowNull);
}
@Override
public Builder withKey2Duplicates(boolean allowDuplicates) {
return (Builder) super.withKey2Duplicates(allowDuplicates);
}
@Override
public Builder withKey2Duplicates(boolean allowDuplicates, boolean allowDuplicatesNull) {
return (Builder) super.withKey2Duplicates(allowDuplicates, allowDuplicatesNull);
}
@Override
public Builder withKey2Sort(boolean sort) {
return (Builder) super.withKey2Sort(sort);
}
/**
* Set comparator to use for sorting the key map.
* Note that this does not automatically sort the list itself, call a withOrderBy method for this.
*
* @param comparator comparator to use for sorting
* @return this (fluent interface)
*/
public Builder withKey2Sort(Comparator super K2> comparator) {
return (Builder) super.withKeySort(2, comparator);
}
/**
* Set comparator to use for sorting the key map.
* Note that this does not automatically sort the list itself, call a withOrderBy method for this.
*
* @param comparator comparator to use for sorting
* @param sortNullsFirst true if null will be sorted first, false for last
* @return this (fluent interface)
*/
public Builder withKey2Sort(Comparator super K2> comparator, boolean sortNullsFirst) {
return (Builder) super.withKeySort(2, comparator, sortNullsFirst);
}
/**
* @return created list
*/
public Key2List build() {
if (keyColl == null) {
keyColl = new KeyCollectionImpl();
}
build(keyColl, true);
Key2List list = new Key2List();
init(keyColl, list);
return list;
}
}
/**
* Protected constructor used by builder or derived collections.
*/
protected Key2List() {
}
/**
* @return builder to use in extending classes
*/
protected Builder getBuilder() {
return new Builder(this);
}
@Override
public Key2List copy() {
return (Key2List) super.copy();
}
@Override
public Key2List crop() {
return (Key2List) super.crop();
}
//-- Element methods
@Override
public IList getAll(E elem) {
return super.getAll(elem);
}
@Override
public int getCount(E elem) {
return super.getCount(elem);
}
@Override
public IList removeAll(E elem) {
return super.removeAll(elem);
}
@Override
public Set getDistinct() {
return super.getDistinct();
}
@Override
public E put(E elem) {
return super.put(elem);
}
//-- Key1 methods
/**
* Returns mapper for key map.
*
* @return mapper for key map
*/
public IFunction getKey1Mapper() {
return (IFunction) super.getKeyMapper(1);
}
/**
* Returns a map view to the key map.
* The collection can be modified through the map as long as the constraint are not violated.
* The collections returned by the methods entrySet(), keySet(), and values() are immutable however.
*
* @return map view to key map
* @throws IllegalArgumentException if the key map cannot be viewed as Map
*/
public Map asMap1() {
return new KeyCollectionAsMap(this, 1, false);
}
/**
* Returns index of first element in list with specified key.
*
* @param key key
* @return index of first element, -1 if no such element exists
*/
public int indexOfKey1(K1 key) {
return super.indexOfKey(1, key);
}
/**
* Checks whether an element with specified key exists.
*
* @param key key
* @return true if element with specified key exists, otherwise false
*/
public boolean containsKey1(K1 key) {
return super.containsKey(1, key);
}
/**
* Returns element with specified key.
* If there are several elements with the same key, the one added first will be returned.
*
* @param key key
* @return element with specified key or null
*/
public E getByKey1(K1 key) {
return super.getByKey(1, key);
}
/**
* Returns all elements with specified key.
*
* @param key key
* @return all elements with specified key (never null)
*/
public GapList getAllByKey1(K1 key) {
return super.getAllByKey(1, key);
}
/**
* Returns the number of elements with specified key.
*
* @param key key
* @return number of elements with specified key
*/
public int getCountByKey1(K1 key) {
return super.getCountByKey(1, key);
}
/**
* Removes element with specified key.
* If there are several elements with the same key, the one added first will be removed.
*
* @param key key
* @return element with specified key or null
*/
public E removeByKey1(K1 key) {
return super.removeByKey(1, key);
}
/**
* Removes all elements with specified key.
*
* @param key key
* @return removed elements with specified key (never null)
*/
public GapList removeAllByKey1(K1 key) {
return super.removeAllByKey(1, key);
}
/**
* Returns list containing all keys in element order.
*
* @return list containing all keys
*/
@SuppressWarnings("unchecked")
public GapList getAllKeys1() {
return (GapList) super.getAllKeys(1);
}
/**
* Returns all distinct keys in the same order as in the key map.
*
* @return distinct keys
*/
@SuppressWarnings("unchecked")
public Set getDistinctKeys1() {
return (Set) super.getDistinctKeys(1);
}
/**
* Adds or replaces element by key.
* If there is no such element, the element is added.
* If there is such an element, the element is replaced.
* So said simply, it is a shortcut for the following code:
*
* if (containsKey1(elem)) {
* removeByKey1(elem);
* }
* add(elem);
*
* However the method is atomic in the sense that all or none operations are executed.
* So if there is already such an element, but adding the new one fails due to a constraint violation,
* the old element remains in the list.
*
* @param elem element
* @return element which has been replaced or null otherwise
*/
public E putByKey1(E elem) {
return super.putByKey(1, elem);
}
/**
* Invalidate key value of element.
* You must call an invalidate method if an element's key value has changed after adding it to the collection.
*
* @param oldKey old key value
* @param newKey new key value
* @param elem element to invalidate (can be null if there are no duplicates with this key)
*/
public void invalidateKey1(K1 oldKey, K1 newKey, E elem) {
super.invalidateKey(1, oldKey, newKey, elem);
}
//-- Key2 methods
/**
* Returns mapper for key map.
*
* @return mapper for key map
*/
public IFunction getKey2Mapper() {
return (IFunction) super.getKeyMapper(2);
}
/**
* Returns a map view to the key map.
* The collection can be modified through the map as long as the constraint are not violated.
* The collections returned by the methods entrySet(), keySet(), and values() are immutable however.
*
* @return map view to key map
* @throws IllegalArgumentException if the key map cannot be viewed as Map
*/
public Map asMap2() {
return new KeyCollectionAsMap(this.keyColl, 2, false);
}
/**
* Returns index of first element in list with specified key.
*
* @param key key
* @return index of first element, -1 if no such element exists
*/
public int indexOfKey2(K2 key) {
return super.indexOfKey(2, key);
}
/**
* Checks whether an element with specified key exists.
*
* @param key key
* @return true if element with specified key exists, otherwise false
*/
public boolean containsKey2(K2 key) {
return super.containsKey(2, key);
}
/**
* Returns element with specified key.
* If there are several elements with the same key, the one added first will be returned.
*
* @param key key
* @return element with specified key or null
*/
public E getByKey2(K2 key) {
return super.getByKey(2, key);
}
/**
* Returns all elements with specified key.
*
* @param key key
* @return all elements with specified key (never null)
*/
public GapList getAllByKey2(K2 key) {
return super.getAllByKey(2, key);
}
/**
* Returns the number of elements with specified key.
*
* @param key key
* @return number of elements with specified key
*/
public int getCountByKey2(K2 key) {
return super.getCountByKey(2, key);
}
/**
* Removes element with specified key.
* If there are several elements with the same key, the one added first will be removed.
*
* @param key key
* @return element with specified key or null
*/
public E removeByKey2(K2 key) {
return super.removeByKey(2, key);
}
/**
* Removes all elements with specified key.
*
* @param key key
* @return removed elements with specified key (never null)
*/
public GapList removeAllByKey2(K2 key) {
return super.removeAllByKey(2, key);
}
/**
* Returns list containing all keys in element order.
*
* @return list containing all keys
*/
@SuppressWarnings("unchecked")
public GapList getAllKeys2() {
return (GapList) super.getAllKeys(2);
}
/**
* Returns all distinct keys in the same order as in the key map.
*
* @return distinct keys
*/
@SuppressWarnings("unchecked")
public Set getDistinctKeys2() {
return (Set) super.getDistinctKeys(2);
}
/**
* Adds or replaces element by key.
* If there is no such element, the element is added.
* If there is such an element, the element is replaced.
* So said simply, it is a shortcut for the following code:
*
* if (containsKey2(elem)) {
* removeByKey2(elem);
* }
* add(elem);
*
* However the method is atomic in the sense that all or none operations are executed.
* So if there is already such an element, but adding the new one fails due to a constraint violation,
* the old element remains in the list.
*
* @param elem element
* @return element which has been replaced or null otherwise
*/
public E putByKey2(E elem) {
return super.putByKey(2, elem);
}
/**
* Invalidate key value of element.
* You must call an invalidate method if an element's key value has changed after adding it to the collection.
*
* @param oldKey old key value
* @param newKey new key value
* @param elem element to invalidate (can be null if there are no duplicates with this key)
*/
public void invalidateKey2(K2 oldKey, K2 newKey, E elem) {
super.invalidateKey(2, oldKey, newKey, elem);
}
// --- ImmutableKey2List ---
@Override
public Key2List unmodifiableList() {
return new ImmutableKey2List(this);
}
protected Key2List(boolean copy, Key2List that) {
if (copy) {
doAssign(that);
}
}
/**
* An immutable version of a Key1List.
* Note that the client cannot change the list,
* but the content may change if the underlying list is changed.
*/
protected static class ImmutableKey2List extends Key2List {
/** UID for serialization */
private static final long serialVersionUID = -1352274047348922584L;
/**
* Private constructor used internally.
*
* @param that list to create an immutable view of
*/
protected ImmutableKey2List(Key2List that) {
super(true, that);
}
@Override
protected void doEnsureCapacity(int capacity) {
error();
}
@Override
protected boolean doAdd(int index, E elem) {
error();
return false;
}
@Override
protected E doSet(int index, E elem) {
error();
return null;
}
@Override
protected E doReSet(int index, E elem) {
error();
return null;
}
@Override
protected E doRemove(int index) {
error();
return null;
}
@Override
protected void doRemoveAll(int index, int len) {
error();
}
@Override
protected void doClear() {
error();
}
@Override
protected void doModify() {
error();
}
/**
* Throw exception if an attempt is made to change an immutable list.
*/
private void error() {
throw new UnsupportedOperationException("list is immutable");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy