net.algart.model3d.common.movement.model
Class SimpleItemSet

java.lang.Object
  extended by net.algart.model3d.common.movement.model.SimpleItemSet
All Implemented Interfaces:
ItemSet
Direct Known Subclasses:
GridItemSet

public class SimpleItemSet
extends java.lang.Object
implements ItemSet

Simplest item set.

This implementation of ItemSet interface stores the items in a built-in java.util.List, which is initially created in the constructor by some ItemListBuilder object. So, all basic methods of this object — size(), get(int), set(int, Item), add(Item), getAll(), getAll(Class), addAll(java.util.Collection), removeItems(int, int) — are implemented very simply via the corresponding methods of java.util.List interface. The only non-trivial methods are getPossibleInteractingIndexes(int[], int) and preprocess(): see comments to those methods for more details.

Please see comments fo ItemSet interface about multithreading usage.

AlgART Laboratory 2010

Since:
JDK 1.5
Version:
1.0
Author:
Daniel Alievsky

Constructor Summary
Constructor and Description
SimpleItemSet()
          Creates new instance of this class, storing items in java.util.ArrayList.
SimpleItemSet(ItemListBuilder itemListBuilder)
          Creates new instance of this class.
 
Method Summary
Modifier and Type Method and Description
 void add(Item newItem)
          Appends the specified item to the end of this item list.
 void addAll(java.util.Collection<? extends Item> newItems)
          Appends all items from the specified collection to the end of this item list, in the order that they are returned by the specified collection's iterator.
 Item get(int itemIndex)
          Returns the item with the given index (from 0 to ItemSet.size()−1).
 java.util.List<Item> getAll()
          Returns the list of all items of this item list.
<T extends Item>
java.util.List<T>
getAll(java.lang.Class<? extends T> requiredClass)
          Returns newly created modifiable list of all items of this item list, implementing the specified interface (or extending the specified class).
 int getPossibleInteractingIndexes(int[] result, int itemIndex)
          This implementation is based on a simple loop, which checks coordinates and interaction radii of all items and stores in the result only indexes of such items that the distance between centers (of the item #itemIndex and the other one) is not greater than the sum of interaction radii of that item pair.
 void preprocess()
          This implementation allocates several Java arrays and stores there coordinates of centers and interaction radii of all items, which have centers and interaction radii (i.e. implement HavingInteractionRadius).
 void removeItems(int fromIndex, int toIndex)
          Removes all items with indexes fromIndex<=k<toIndex and shifts any subsequent items to the left (subtracts toIndex-fromIndex from their indexes).
 void set(int itemIndex, Item newItem)
          Replaces the item with the given index (from 0 to ItemSet.size()−1) with the specified new one.
 int size()
          Returns the number of items in this item list.
 java.lang.String toString()
          Returns a brief string description of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SimpleItemSet

public SimpleItemSet(ItemListBuilder itemListBuilder)
Creates new instance of this class.

The itemListBuilder argument specifies the builder, that will be used for creating the built-in java.util.List instance, which will really store all items.

Parameters:
itemListBuilder - the builder, that will be used for creating the java.util.List instance.
Throws:
java.lang.NullPointerException - if itemListBuilder is null.

SimpleItemSet

public SimpleItemSet()
Creates new instance of this class, storing items in java.util.ArrayList. Equivalent to new SimpleItemSet(ItemListBuilder.DEFAULT).

Method Detail

size

public int size()
Description copied from interface: ItemSet
Returns the number of items in this item list.

Specified by:
size in interface ItemSet
Returns:
the number of items in this item list.

get

public Item get(int itemIndex)
Description copied from interface: ItemSet
Returns the item with the given index (from 0 to ItemSet.size()−1).

Specified by:
get in interface ItemSet
Parameters:
itemIndex - index of the item to get.
Returns:
the item with the given index.

set

public void set(int itemIndex,
                Item newItem)
Description copied from interface: ItemSet
Replaces the item with the given index (from 0 to ItemSet.size()−1) with the specified new one.

Specified by:
set in interface ItemSet
Parameters:
itemIndex - index of the item to replace.
newItem - new item to be stored at the specified index.

add

public void add(Item newItem)
Description copied from interface: ItemSet
Appends the specified item to the end of this item list. The added item must not be null.

Specified by:
add in interface ItemSet
Parameters:
newItem - new item to be added to this item list.

getAll

public java.util.List<Item> getAll()
Description copied from interface: ItemSet
Returns the list of all items of this item list. The order of items in the resulting list is identical to the order in this collection: result.get(k)==thisInstance.get(k).

The returned list can be either newly created list, or an immutable view (Collections.unmodifiableList) of the internal collection, stored by this object. In any case, you cannot modify the set of items via the result of this method. But the returned list contains references to items, not their clones; so changes in the returned items (via setXxx methods) will be reflected in this collection.

Specified by:
getAll in interface ItemSet
Returns:
newly created list of all items.

getAll

public <T extends Item> java.util.List<T> getAll(java.lang.Class<? extends T> requiredClass)
Description copied from interface: ItemSet
Returns newly created modifiable list of all items of this item list, implementing the specified interface (or extending the specified class). Usually the returned list is ArrayList.

The returned list is newly created, so changes in it do not affect to this object. But the returned list contains references to items, not their clones; so changes in the returned items (via setXxx methods) will be reflected in this collection.

Specified by:
getAll in interface ItemSet
Parameters:
requiredClass - the class of all returned items.
Returns:
newly created list of all items, implementing the specified interface (or extending class).

addAll

public void addAll(java.util.Collection<? extends Item> newItems)
Description copied from interface: ItemSet
Appends all items from the specified collection to the end of this item list, in the order that they are returned by the specified collection's iterator. The added items must not be null.

Note: if some of added items are null, then part of them can be still added before throwing NullPointerException. In other words, this method can be non-atomic regarding this failure.

Specified by:
addAll in interface ItemSet
Parameters:
newItems - new items to be added to this item list.

removeItems

public void removeItems(int fromIndex,
                        int toIndex)
Description copied from interface: ItemSet
Removes all items with indexes fromIndex<=k<toIndex and shifts any subsequent items to the left (subtracts toIndex-fromIndex from their indexes).

Specified by:
removeItems in interface ItemSet
Parameters:
fromIndex - starting index of removed items (inclusive).
toIndex - ending index of removed items (exclusive).

getPossibleInteractingIndexes

public int getPossibleInteractingIndexes(int[] result,
                                         int itemIndex)
This implementation is based on a simple loop, which checks coordinates and interaction radii of all items and stores in the result only indexes of such items that the distance between centers (of the item #itemIndex and the other one) is not greater than the sum of interaction radii of that item pair.

Specified by:
getPossibleInteractingIndexes in interface ItemSet
Parameters:
result - the indexes of "neighbours": items that can interact with the given item.
itemIndex - the index of the given item.
Returns:
the actual length K of the returned array: the required indexes are returned in the first K elements of the result.
Throws:
java.lang.NullPointerException - if result argument is null.
java.lang.IllegalStateException - if the length of result array is insufficient.
java.lang.IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).

preprocess

public void preprocess()
This implementation allocates several Java arrays and stores there coordinates of centers and interaction radii of all items, which have centers and interaction radii (i.e. implement HavingInteractionRadius). It optimizes the execution of getPossibleInteractingIndexes(int[], int) method: Java usually works faster with usual Java array, than with properties of objects available via getXxx method.

This method reallocates memory if and only if it is the first call of this method or the number of items size() was changed since the last call of it.

Specified by:
preprocess in interface ItemSet

toString

public java.lang.String toString()
Returns a brief string description of this object.

The result of this method may depend on implementation.

Overrides:
toString in class java.lang.Object
Returns:
a brief string description of this object.