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

me.dhf.dict.chain.ChainBuilder Maven / Gradle / Ivy

Go to download

Generate global dictionary JSON file and map for calling in the previous section or view

The newest version!
package me.dhf.dict.chain;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

final class ChainBuilder {

    private final LinkedList list;
    private final Map, E> uniqueClasses;

    public ChainBuilder() {
        this.list = new LinkedList();
        this.uniqueClasses = new HashMap, E>();
    }

    private void ensureUnique(final E e) {
        final E previous = this.uniqueClasses.remove(e.getClass());
        if (previous != null) {
            this.list.remove(previous);
        }
        this.uniqueClasses.put(e.getClass(), e);
    }

    public ChainBuilder addFirst(final E e) {
        if (e == null) {
            return this;
        }
        ensureUnique(e);
        this.list.addFirst(e);
        return this;
    }

    public ChainBuilder addLast(final E e) {
        if (e == null) {
            return this;
        }
        ensureUnique(e);
        this.list.addLast(e);
        return this;
    }

    public ChainBuilder addAllFirst(final Collection c) {
        if (c == null) {
            return this;
        }
        for (final E e: c) {
            addFirst(e);
        }
        return this;
    }

    @SuppressWarnings("unchecked")
	public ChainBuilder addAllFirst(final E... c) {
        if (c == null) {
            return this;
        }
        for (final E e: c) {
            addFirst(e);
        }
        return this;
    }

    public ChainBuilder addAllLast(final Collection c) {
        if (c == null) {
            return this;
        }
        for (final E e: c) {
            addLast(e);
        }
        return this;
    }
    
    @SuppressWarnings("unchecked")
    public ChainBuilder addAllLast(final E... c) {
        if (c == null) {
            return this;
        }
        for (final E e: c) {
            addLast(e);
        }
        return this;
    }

    public LinkedList build() {
        return new LinkedList(this.list);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy