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

org.magicwerk.brownies.collections.KeyList 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: KeyList.java 2927 2015-09-04 13:07:01Z origo $
 */
package org.magicwerk.brownies.collections;

import java.util.Collection;
import java.util.Comparator;
import java.util.Set;

import org.magicwerk.brownies.collections.KeyCollectionImpl.BuilderImpl;
import org.magicwerk.brownies.collections.function.IConsumer;
import org.magicwerk.brownies.collections.function.IPredicate;


/**
 * KeyList implements a list.
 * 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: KeyList.java 2927 2015-09-04 13:07:01Z origo $
 *
 * @param  type of elements stored in the list
 */
@SuppressWarnings("serial")
public class KeyList extends KeyListImpl {

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

        /**
         * Private constructor used if extending KeyList.
         *
         * @param keyList	key list
         */
        Builder(KeyList keyList) {
        	this.keyList = keyList;
        	initKeyMapBuilder(0);
        }

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

        @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 withListType(Class type) {
        	return (Builder) super.withListType(type);
        }

        @Override
        public Builder withElemSet() {
        	return (Builder) super.withElemSet();
        }

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

        @Override
        public Builder withOrderByElem(Class type) {
        	return (Builder) super.withOrderByElem(type);
        }

        @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();
        }

        /**
         * Create collection with specified options.
         *
         * @return created collection
         */
        public KeyList build() {
        	if (keyColl == null) {
               	keyColl = new KeyCollectionImpl();
        	}
        	build(keyColl, true);
        	KeyList list = new KeyList();
        	init(keyColl, list);
        	return list;
        }
    }

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

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

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

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


	// --- ImmutableKey1List ---

	@Override
    public KeyList unmodifiableList() {
        return new ImmutableKeyList(this);
    }

    protected KeyList(boolean copy, KeyList 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 ImmutableKeyList extends KeyList {

        /** UID for serialization */
        private static final long serialVersionUID = -1352274047348922584L;

        /**
         * Private constructor used internally.
         *
         * @param that  list to create an immutable view of
         */
        protected ImmutableKeyList(KeyList 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 - 2024 Weber Informatics LLC | Privacy Policy