QJCC homepage

biz.chitec.quarterback.util
Class QuickIntArray

java.lang.Object
  extended bybiz.chitec.quarterback.util.QuickIntArray
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class QuickIntArray
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

Storage for int values which provides certain basic operations with speed in mind. QuickIntArray can operate on lists of integer numbers. Namely, they can be stored sorted or unsorted, unique or multiple times. This storage is used especially in relation to database accesses and has at least one special method for this purpose.

This class has been created long before boxing or even the Collections framework became standard techniques. It has its advantages even these days, though, as it leaves all the fancy generic stuff and concentrates just on speed. And that can be archived best by using arrays of simple numeric data types instead of Lists of Object-based wrappers.

Version:
$Id: 368b1bf9a981c2507189d91aa069ac68edd43034 $
Author:
Dirk Hillbrecht 1997-2000, chitec/Dirk Hillbrecht 2000-2003, cantamen/Dirk Hillbrecht 2003-2004 Distributed under the terms of the GNU LGPL.
See Also:
Serialized Form

Field Summary
private static int DEFAULTGROWCHUNK
           
private  int growchunk
           
private  boolean singular
           
private  int size
           
private  boolean sorted
           
private  int[] xa
           
 
Constructor Summary
QuickIntArray()
          Creates empty storage, unsorted and without duplicate prevention.
QuickIntArray(boolean singularx, boolean sortedx)
          Creates empty storage.
QuickIntArray(boolean sgx, boolean srx, int szx, int grx, int[] xax)
          General constructor.
QuickIntArray(java.util.Collection list)
          Creates storage out of a List.
QuickIntArray(java.util.Collection list, boolean singularx, boolean sortedx)
          Creates storage out of a List.
QuickIntArray(int[] ii)
          Creates storage out of array of int values.
QuickIntArray(int[] ii, boolean singularx, boolean sortedx)
          Creates storage out of array of int values.
QuickIntArray(QuickIntArray x)
          Copy constructor.
 
Method Summary
 java.util.List asList()
          Transforms the storage into a ArrayList of Integer objects.
private  void checkAndEnlargeArray(int newlyneeded)
          Checks whether a new element can be added internally.
 void clear()
          Deletes all elements
 java.lang.Object clone()
          Clone method.
 boolean contains(int y)
          Checks whether the storage contains an integer or not.
static QuickIntArray createQuickIntArray(java.lang.Object orgx)
          Creates a QuickIntArray out of a multitude of input parameter types.
static QuickIntArray createQuickIntArrayOrNull(java.lang.Object orgx, boolean nullonempty)
          Creates a QuickIntArray out of a multitude of input parameter types and return null if input is null.
 void deleteAt(int idx)
          Deletes an element at a certain index position.
 int elementAt(int i)
          Deprecated. Use get(int) instead.
 int get(int i)
          Returns the element at the given index.
 int[] getContent()
          Return content.
 int[] getContentPart(int start, int length)
          Return part of the content in an efficient manner.
 int getGrowChunk()
          Gets the growChunk attribute of the QuickIntArray object
 int indexOf(int y)
          Returns the (first) index of the given integer in the storage.
 boolean insert(int y)
          Inserts given number.
 boolean insert(int[] y)
          Insert content of an int[]-array.
 void insert(QuickIntArray qia)
          Combines this storage with another one.
 QuickIntArray intersectWith(QuickIntArray other)
          Generates intersection of this and the other QuickIntArray.
 boolean isSingular()
           
 boolean isSorted()
           
 int length()
          Returns the length (=size) of the storage.
 QuickIntArray restWith(QuickIntArray other)
          Generates rest of this with respect to other.
 void setGrowChunk(int i)
          Sets the growChunk attribute of the QuickIntArray object
 java.util.List toList()
          Transforms the storage into a List of Integer objects.
 java.lang.String toString()
          generate output for debugging purposes.
 QuickIntArray uniteWith(QuickIntArray other)
          Generates union of this and the other QuickIntArray.
 java.lang.String whereClause(java.lang.String elemnam, java.lang.String concatenate, boolean concatafter)
          Creates an SQL-where-clause out of the storage's content, like "nr in (1,2,3)".
 java.lang.String whereNotClause(java.lang.String elemnam, java.lang.String concatenate, boolean concatafter)
          Creates an inverted SQL-where-clause out of the storage's content, like "nr not in (1,2,3)".
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULTGROWCHUNK

private static final int DEFAULTGROWCHUNK
See Also:
Constant Field Values

xa

private int[] xa

size

private int size

growchunk

private int growchunk

singular

private boolean singular

sorted

private boolean sorted
Constructor Detail

QuickIntArray

public QuickIntArray(boolean sgx,
                     boolean srx,
                     int szx,
                     int grx,
                     int[] xax)
General constructor. Normally not used, declared public only for the GJSA parsers.

Parameters:
sgx - Sigular flag
srx - Sorted flag
szx - Number of currently used elements in int[] array
grx - growchunk
xax - "real" array. May be larger than QIA's size, in which case additional values do not count

QuickIntArray

public QuickIntArray()
Creates empty storage, unsorted and without duplicate prevention.


QuickIntArray

public QuickIntArray(boolean singularx,
                     boolean sortedx)
Creates empty storage.

Parameters:
singularx - Flag whether there must be no duplicate values
sortedx - Flag whether content is always sorted

QuickIntArray

public QuickIntArray(int[] ii,
                     boolean singularx,
                     boolean sortedx)
Creates storage out of array of int values. The array is copied. If singular or sorted is true, every element of the source in inserted on its own.

Parameters:
ii - Source values
singularx - Flag whether there must be no duplicate values
sortedx - Flag whether content is always sorted

QuickIntArray

public QuickIntArray(int[] ii)
Creates storage out of array of int values. The array is copied verbatim.

Parameters:
ii - Source values

QuickIntArray

public QuickIntArray(java.util.Collection list,
                     boolean singularx,
                     boolean sortedx)
Creates storage out of a List. The given List object may not contain anything but Number objects. Numbers not being Integers are converted by intValue().

Parameters:
list - Source values. Must be solely Number objects
singularx - Flag whether there must be no duplicate values
sortedx - Flag whether content is always sorted

QuickIntArray

public QuickIntArray(java.util.Collection list)
Creates storage out of a List. The given List object may not contain anything but Integer objects.

Parameters:
list - Description of the Parameter

QuickIntArray

public QuickIntArray(QuickIntArray x)
Copy constructor.

Parameters:
x - Description of the Parameter
Method Detail

isSingular

public boolean isSingular()

isSorted

public boolean isSorted()

setGrowChunk

public void setGrowChunk(int i)
Sets the growChunk attribute of the QuickIntArray object

Parameters:
i - The new growChunk value

getGrowChunk

public int getGrowChunk()
Gets the growChunk attribute of the QuickIntArray object

Returns:
The growChunk value

clone

public java.lang.Object clone()
Clone method.

Returns:
A copy of the current instance which is completely independent.

checkAndEnlargeArray

private void checkAndEnlargeArray(int newlyneeded)
Checks whether a new element can be added internally. If not, the internal storage is enlarged in a portion growchunk says.

Parameters:
newlyneeded - Description of the Parameter

clear

public void clear()
Deletes all elements


getContent

public int[] getContent()
Return content. Returns content in a fresh, new and properly fitting int[]-array.

Returns:
The content value

getContentPart

public int[] getContentPart(int start,
                            int length)
Return part of the content in an efficient manner. Returns from index start maximum length elements. Copying is done via System.arraycopy() and therefore efficient. If there are not enough elements in the QuickIntArray, not length elements, but accordingly less are copied. If start is too big at all, an array of length 0 is returned.

Parameters:
start - First index to copy.
length - Maximum number of elements to copy.
Returns:
New array with the requested elements, can be smaller than length and even of size 0 if start is near enough to QIA's size().

insert

public boolean insert(int y)
Inserts given number. Returns on if insertion was needed. No insertion takes place if value is already in an array with singular values. In this case, false is returned.

Parameters:
y - The value to insert
Returns:
True if the value has actually been inserted, false if not.

insert

public boolean insert(int[] y)
Insert content of an int[]-array. If not sorted or singluar, copy everything to the end.

Parameters:
y - Array of int values to insert
Returns:
True if any of the values has been inserted, false otherwise.

insert

public void insert(QuickIntArray qia)
Combines this storage with another one. Not optimized for the moment.

Parameters:
qia - Array of values to insert.

deleteAt

public void deleteAt(int idx)
Deletes an element at a certain index position. Does not shrink the array.

Parameters:
idx - Index of element to delete. If out of bounds, nothing happens.

contains

public boolean contains(int y)
Checks whether the storage contains an integer or not. If the QuickIntArray is sorted, binary search is performed. Otherwise, we must search linear.

Parameters:
y - Value to look for
Returns:
Flag indicating whether QuickIntArray contains the given value at least once or not

indexOf

public int indexOf(int y)
Returns the (first) index of the given integer in the storage.

Parameters:
y - Description of the Parameter
Returns:
Description of the Return Value

elementAt

public int elementAt(int i)
Deprecated. Use get(int) instead.

Returns the element at the given index.

Parameters:
i - Description of the Parameter
Returns:
Description of the Return Value

get

public int get(int i)
Returns the element at the given index.

Parameters:
i - Description of the Parameter
Returns:
Description of the Return Value

length

public int length()
Returns the length (=size) of the storage.

Returns:
Description of the Return Value

toList

public java.util.List toList()
Transforms the storage into a List of Integer objects.

Returns:
Content as List of Integer objects

asList

public java.util.List asList()
Transforms the storage into a ArrayList of Integer objects.

Returns:
Content as ArrayList of Integer objects

whereClause

public java.lang.String whereClause(java.lang.String elemnam,
                                    java.lang.String concatenate,
                                    boolean concatafter)
Creates an SQL-where-clause out of the storage's content, like "nr in (1,2,3)". Note that in case of an empty storage, an empty string is returned. This might not be the wanted behaviour.

Parameters:
elemnam - Name of the element to substitute in the query
concatenate - concatenation literal, usually " and" or " or"
concatafter - Flag whether the concatenation literal should be placed before or after the content
Returns:
String being a chunk of SQL

whereNotClause

public java.lang.String whereNotClause(java.lang.String elemnam,
                                       java.lang.String concatenate,
                                       boolean concatafter)
Creates an inverted SQL-where-clause out of the storage's content, like "nr not in (1,2,3)".

Parameters:
elemnam - Description of the Parameter
concatenate - Description of the Parameter
concatafter - Description of the Parameter
Returns:
Description of the Return Value

toString

public java.lang.String toString()
generate output for debugging purposes.

Returns:
Description of the Return Value

uniteWith

public QuickIntArray uniteWith(QuickIntArray other)
Generates union of this and the other QuickIntArray. Note that this method works in situ.

Parameters:
other - Description of the Parameter
Returns:
Description of the Return Value

intersectWith

public QuickIntArray intersectWith(QuickIntArray other)
Generates intersection of this and the other QuickIntArray. The method works in situ and keeps only those elements which are also available in other.

Parameters:
other - Description of the Parameter
Returns:
Description of the Return Value

restWith

public QuickIntArray restWith(QuickIntArray other)
Generates rest of this with respect to other. The method works in situ and keeps only those elements which are not available in other.

Parameters:
other - Description of the Parameter
Returns:
Description of the Return Value

createQuickIntArray

public static QuickIntArray createQuickIntArray(java.lang.Object orgx)
Creates a QuickIntArray out of a multitude of input parameter types. The following argument types are detected and converted as follows:

createQuickIntArrayOrNull

public static QuickIntArray createQuickIntArrayOrNull(java.lang.Object orgx,
                                                      boolean nullonempty)
Creates a QuickIntArray out of a multitude of input parameter types and return null if input is null. If nullonempty is true, null is also returned if the resulting QuickIntArray is of size 0. If the parameter is false, the empty QuickIntArray is retured.

Parameters:
orgx - Some data to be represented as a QuickIntArray
nullonempty - If true, return also null if the QuickIntArray is empty
Returns:
The resulting QuickIntArray or null if the input was null and potentially if the resultung QuickIntArray is empty
See Also:
createQuickIntArray(Object)

QJCC homepage