com.browseengine.bobo.facets.data.TermValueList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bobo-browse Show documentation
Show all versions of bobo-browse Show documentation
Bobo is a Faceted Search implementation written purely in Java, an extension of Apache Lucene
The newest version!
package com.browseengine.bobo.facets.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
/**
* This class behaves as List with a few extensions:
*
* - Semi-immutable, e.g. once added, cannot be removed.
* - Assumes sequence of values added are in sorted order
* - {@link #indexOf(Object)} return value conforms to the contract of {@link Arrays#binarySearch(Object[], Object)}
* - {@link #seal()} is introduce to trim the List size, similar to {@link ArrayList#trimToSize()}, once it is called, no add should be performed.
*
*/
public abstract class TermValueList implements List {
protected abstract List> buildPrimitiveList(int capacity);
protected Class _type;
public abstract String format(Object o);
public abstract void seal();
protected List> _innerList;
protected TermValueList() {
_innerList = buildPrimitiveList(-1);
}
protected TermValueList(int capacity) {
_innerList = buildPrimitiveList(capacity);
}
/**
* The user of this method should not try to alter the content of the list,
* which may result in data inconsistency.
* And of the content can be accessed using the getRawValue(int) method.
* @return the inner list
*/
public List> getInnerList() {
return _innerList;
}
/**
* Add a new value to the list. It is important to add the values in sorted (ASC) order.
* Our algorithm uses binary searches and priority queues, both of which fails when the ordering is wrong.
*/
@Override
abstract public boolean add(String o);
@Override
public void add(int index, String element) {
throw new IllegalStateException("not supported");
}
@Override
public boolean addAll(Collection extends String> c) {
boolean ret = true;
for (String s : c) {
ret &= add(s);
}
return ret;
}
@Override
public boolean addAll(int index, Collection extends String> c) {
throw new IllegalStateException("not supported");
}
@Override
public void clear() {
_innerList.clear();
}
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
public abstract boolean containsWithType(T val);
@Override
public boolean containsAll(Collection> c) {
throw new IllegalStateException("not supported");
}
public Class getType() {
return _type;
}
@Override
public String get(int index) {
return format(_innerList.get(index));
}
@SuppressWarnings("unchecked")
public T getRawValue(int index) {
return (T) _innerList.get(index);
}
public Comparable> getComparableValue(int index) {
return (Comparable>) _innerList.get(index);
}
@Override
abstract public int indexOf(Object o);
public int indexOfWithOffset(Object value, int offset) {
throw new IllegalStateException("not supported");
}
public abstract int indexOfWithType(T o);
@Override
public boolean isEmpty() {
return _innerList.isEmpty();
}
@Override
public Iterator iterator() {
final Iterator> iter = _innerList.iterator();
return new Iterator() {
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public String next() {
return format(iter.next());
}
@Override
public void remove() {
iter.remove();
}
};
}
@Override
public int lastIndexOf(Object o) {
return indexOf(o);
}
@Override
public ListIterator listIterator() {
throw new IllegalStateException("not supported");
}
@Override
public ListIterator listIterator(int index) {
throw new IllegalStateException("not supported");
}
@Override
public boolean remove(Object o) {
throw new IllegalStateException("not supported");
}
@Override
public String remove(int index) {
throw new IllegalStateException("not supported");
}
@Override
public boolean removeAll(Collection> c) {
throw new IllegalStateException("not supported");
}
@Override
public boolean retainAll(Collection> c) {
throw new IllegalStateException("not supported");
}
@Override
public String set(int index, String element) {
throw new IllegalStateException("not supported");
}
@Override
public int size() {
return _innerList.size();
}
@Override
public List subList(int fromIndex, int toIndex) {
throw new IllegalStateException("not supported");
}
@Override
public Object[] toArray() {
Object[] array = _innerList.toArray();
Object[] retArray = new Object[array.length];
for (int i = 0; i < array.length; ++i) {
retArray[i] = format(array[i]);
}
return retArray;
}
@Override
public R[] toArray(R[] a) {
List l = subList(0, size());
return l.toArray(a);
}
public static void main(String[] args) {
int numIter = 20000;
TermIntList list = new TermIntList();
for (int i = 0; i < numIter; ++i) {
list.add(String.valueOf(i));
}
long start = System.currentTimeMillis();
List> rawList = list.getInnerList();
for (int i = 0; i < numIter; ++i) {
rawList.get(i);
}
long end = System.currentTimeMillis();
System.out.println("took: " + (end - start));
}
}