All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.magicwerk.brownies.collections.Key2Collection Maven / Gradle / Ivy

Go to download

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).

There is a newer version: 0.9.16
Show newest version
/*
 * 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: Key2Collection.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;


/**
 * Key2Collection implements a collection 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: Key2Collection.java 3124 2016-04-05 23:13:35Z origo $
 *
 * @see Key1List
 * @param  type of elements stored in the list
 * @param  type of first key
 * @param  type of second key
 */
@SuppressWarnings("serial")
public class Key2Collection extends KeyCollectionImpl {

    /**
     * Builder to construct Key2Collection instances.
     */
    public static class Builder extends BuilderImpl {
        /**
         * Default constructor.
         */
        public Builder() {
        	this(null);
        }

        /**
         * Private constructor used if extending Key2Collection.
         *
         * @param keyColl	key collection
         */
        Builder(Key2Collection keyColl) {
        	this.keyColl = keyColl;
        	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 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 comparator) {
        	return (Builder) super.withElemSort(comparator);
        }

        @Override
        public Builder withElemSort(Comparator 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 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 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 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 list collection, call a withOrderBy method for this.
         *
         * @param comparator    comparator to use for sorting
         * @return              this (fluent interface)
         */
        public Builder withKey1Sort(Comparator 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 collection, 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 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 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 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 mapper) {
        	return (Builder) super.withUniqueKeyMap(2, mapper);
        }

        @Override
        public Builder withOrderByKey2(boolean orderBy) {
        	return (Builder) super.withOrderByKey2(orderBy);
        }

        @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 collection itself, call a withOrderBy method for this.
         *
         * @param comparator    comparator to use for sorting
         * @return              this (fluent interface)
         */
        public Builder withKey2Sort(Comparator comparator) {
        	return (Builder) super.withKeySort(2, 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 withKey2Sort(Comparator comparator, boolean sortNullsFirst) {
        	return (Builder) super.withKeySort(2, comparator, sortNullsFirst);
        }

        /**
         * @return created collection
         */
        @SuppressWarnings("unchecked")
		public Key2Collection build() {
        	if (keyColl == null) {
               	keyColl = new Key2Collection();
        	}
        	build(keyColl, false);
        	init(keyColl);
        	return (Key2Collection) keyColl;
        }
    }

    /**
     * Protected constructor used by builder or derived collections.
     */
    protected Key2Collection() {
    }

    /**
     * @return builder to use in extending classes
     */
    protected Builder getBuilder() {
    	return new Builder(this);
    }

    @Override
    public Key2Collection copy() {
        return (Key2Collection) super.copy();
    }

    @Override
    public Key2Collection crop() {
        return (Key2Collection) 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);
	}

    //-- 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);
    }

	/**
	 * 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 all elements with specified key.
	 * The returned list is immutable.
	 *
	 * @param key	key
	 * @return		all elements with specified key (never 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, 2, false); } /** * 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); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy