me.dhf.dict.chain.ChainBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dhf-dict Show documentation
Show all versions of dhf-dict Show documentation
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);
}
}