javolution.util.internal.table.SubTableImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javolution-core-java-msftbx Show documentation
Show all versions of javolution-core-java-msftbx Show documentation
Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.util.internal.table;
import javolution.util.function.Equality;
import javolution.util.service.TableService;
/**
* A view over a portion of a table.
*/
public class SubTableImpl extends TableView {
private static final long serialVersionUID = 0x600L; // Version.
/** Splits the specified table. */
@SuppressWarnings("unchecked")
public static TableService[] splitOf(TableService table,
int n, boolean updateable) {
if (updateable) table = new SharedTableImpl(table);
if (n < 1) throw new IllegalArgumentException("Invalid argument n: "
+ n);
TableService[] subTables = new TableService[n];
int minSize = table.size() / n;
int start = 0;
for (int i = 0; i < n - 1; i++) {
subTables[i] = new SubTableImpl(table, start, start + minSize);
start += minSize;
}
subTables[n - 1] = new SubTableImpl(table, start, table.size());
return subTables;
}
protected final int fromIndex;
protected int toIndex;
public SubTableImpl(TableService target, int from, int to) {
super(target);
if ((from < 0) || (to > target.size()) || (from > to)) throw new IndexOutOfBoundsException(
"fromIndex: " + from + ", toIndex: " + to + ", size(): "
+ target.size()); // As per List.subList contract.
fromIndex = from;
toIndex = to;
}
@Override
public boolean add(E element) {
target().add(toIndex++, element);
return true;
}
@Override
public void add(int index, E element) {
if ((index < 0) && (index > size())) indexError(index);
target().add(index + fromIndex, element);
toIndex++;
}
@Override
public void clear() {
for (int i = toIndex - 1; i >= fromIndex; i--) { // Better to do it from the end (less shift).
target().remove(i);
}
toIndex = fromIndex;
}
@Override
public Equality super E> comparator() {
return target().comparator();
}
@Override
public E get(int index) {
if ((index < 0) && (index >= size())) indexError(index);
return target().get(index + fromIndex);
}
@Override
public E remove(int index) {
if ((index < 0) && (index >= size())) indexError(index);
toIndex--;
return target().remove(index + fromIndex);
}
@Override
public E set(int index, E element) {
if ((index < 0) && (index >= size())) indexError(index);
return target().set(index + fromIndex, element);
}
@Override
public int size() {
return toIndex - fromIndex;
}
}