org.nutz.plugins.dict.chain.ChainBuilder Maven / Gradle / Ivy
package org.nutz.plugins.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 - 2025 Weber Informatics LLC | Privacy Policy