org.magicwerk.brownies.collections.Key1Collection 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: Key1Collection.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.function.IConsumer;
import org.magicwerk.brownies.collections.function.IFunction;
import org.magicwerk.brownies.collections.function.IPredicate;
/**
* Key1Collection implements a collection with 1 key.
* This key 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: Key1Collection.java 3124 2016-04-05 23:13:35Z origo $
*
* @see Key1List
* @param type of elements stored in the list
* @param type of key
*/
@SuppressWarnings("serial")
public class Key1Collection extends KeyCollectionImpl {
/**
* Builder to construct Key1Collection instances.
*/
public static class Builder extends BuilderImpl {
/**
* Default constructor.
*/
public Builder() {
this(null);
}
/**
* Private constructor used if extending Key1Collection.
*
* @param keyColl key collection
*/
Builder(Key1Collection keyColl) {
this.keyColl = keyColl;
initKeyMapBuilder(1);
}
// -- 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);
}
//-- 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,K> mapper) {
return (Builder) 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,K> 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,K> mapper) {
return (Builder) super.withUniqueKeyMap(1, mapper);
}
@Override
public Builder withOrderByKey1(boolean orderBy) {
return (Builder) super.withOrderByKey1(orderBy);
}
@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 collection itself, call a withOrderBy method for this.
*
* @param comparator comparator to use for sorting
* @return this (fluent interface)
*/
public Builder withKey1Sort(Comparator super K> comparator) {
return (Builder) super.withKeySort(1, comparator);
}
/**
* Set comparator to use for sorting the key map.
* Note that this does not automatically sort the collection 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 K> comparator, boolean sortNullsFirst) {
return (Builder) super.withKeySort(1, comparator, sortNullsFirst);
}
/**
* @return created collection
*/
@SuppressWarnings("unchecked")
public Key1Collection build() {
if (keyColl == null) {
keyColl = new Key1Collection();
}
build(keyColl, false);
init(keyColl);
return (Key1Collection) keyColl;
}
}
/**
* Protected constructor used by builder or derived collections.
*/
protected Key1Collection() {
}
/**
* @return builder to use in extending classes
*/
protected Builder getBuilder() {
return new Builder(this);
}
@Override
public Key1Collection copy() {
return (Key1Collection) super.copy();
}
@Override
public Key1Collection crop() {
return (Key1Collection) super.crop();
}
//-- Element methods
@Override
public GapList getAll(E elem) {
return super.getAll(elem);
}
@Override
public int getCount(E elem) {
return super.getCount(elem);
}
@Override
public GapList removeAll(E elem) {
return super.removeAll(elem);
}
@Override
public Set getDistinct() {
return super.getDistinct();
}
@Override
public E put(E elem) {
return super.put(elem);
}
@Override
public void invalidate(E elem) {
super.invalidate(elem);
}
//-- Key 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);
}
/**
* Checks whether an element with specified key exists.
*
* @param key key
* @return true if element with specified key exists, otherwise false
*/
public boolean containsKey1(K 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(K 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(K 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(K 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(K 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(K 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(K oldKey, K newKey, E elem) {
super.invalidateKey(1, oldKey, newKey, elem);
}
}