jess
Class ValueVector

java.lang.Object
  extended byjess.ValueVector
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
Fact, Funcall

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

A self-extending array that only holds Value objects. The ValueVector class is Jess's internal representation of a Lisp list, and therefore has a central role in programming with Jess in Java. The ValueVector class itself is used to represent generic lists, while specialized subclasses are used as function calls (Funcall and facts (Fact).

Working with ValueVector itself is simple. Its API is similar to the List interface. ValueVector is a self-extending array: when new elements are added, the ValueVector grows in size to accomodate them. Here is a bit of example Java code in which we create the Jess list (a b c).

ValueVector vv = new ValueVector();
vv.add("a");
vv.add("b");
vv.add("c");

// Prints "(a b c)"
System.out.println(vv.toStringWithParens());
 
To pass a ValueVector from Java to Jess, you should enclose it in a Value object of type RU.LIST.
Value myList = new Value(vv, RU.LIST);
ValueVector is not synchronized, so you must be careful of multithreading issues.

(C) 2005 Sandia National Laboratories

See Also:
Value, Serialized Form

Field Summary
protected  int m_ptr
          The current number of elements.
protected  Value[] m_v
          The elements themselves.
 
Constructor Summary
ValueVector()
          Construct a ValueVector of the default capacity (10).
ValueVector(int size)
          Construct a ValueVector of the given capacity.
 
Method Summary
 ValueVector add(boolean val)
          Add a new element to the end of this ValueVector.
 ValueVector add(double val)
          Add a new element to the end of this ValueVector.
 ValueVector add(int val)
          Add a new element to the end of this ValueVector.
 ValueVector add(long val)
          Add a new element to the end of this ValueVector.
 ValueVector add(java.lang.Object val)
          Add a new element to the end of this ValueVector.
 ValueVector add(java.lang.String val)
          Add a new element to the end of this ValueVector.
 ValueVector add(Value val)
          Add a new element to the end of this ValueVector.
 void addAll(java.util.Collection list)
          Add all the members of the collection to this ValueVector.
 ValueVector addAll(ValueVector vv)
          Appends all Values in the argument ValueVector.
 java.lang.Object clone()
          Create a shallow copy of this ValueVector.
 ValueVector cloneInto(ValueVector vv)
          Make the parameter into a copy of this ValueVector.
static void copy(ValueVector src, int srcPos, ValueVector dest, int destPos, int length)
          System.arraycopy DeLuxe for ValueVectors.
 boolean equals(java.lang.Object o)
          Compare this ValueVector to another object.
 Value get(int i)
          Returns the entry at position i in this ValueVector.
 ValueVector remove(int i)
          Remove the item at index i from the ValueVector; move all the higher-numbered elements down.
 ValueVector set(Value val, int i)
          Set the element at position i to the given value.
 ValueVector setLength(int i)
          Set the length of this ValueVector.
 int size()
          Returns the size of this ValueVector.
 java.lang.String toString()
          Return a String version of this ValueVector, without parentheses.
 java.lang.String toStringWithParens()
          Return a String version of this ValueVector, with parentheses around all ValueVectors.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

m_v

protected Value[] m_v
The elements themselves. Should never be null.


m_ptr

protected int m_ptr
The current number of elements. Should always be nonnegative.

Constructor Detail

ValueVector

public ValueVector()
Construct a ValueVector of the default capacity (10).


ValueVector

public ValueVector(int size)
Construct a ValueVector of the given capacity.

Parameters:
size - The number of Values this vector can hold at creation
Method Detail

size

public final int size()
Returns the size of this ValueVector.

Returns:
the size of this ValueVector

clone

public java.lang.Object clone()
Create a shallow copy of this ValueVector.

Returns:
the copy

cloneInto

public ValueVector cloneInto(ValueVector vv)
Make the parameter into a copy of this ValueVector.

Parameters:
vv - a ValueVector, whose contents are erased.
Returns:
the parameter

get

public Value get(int i)
          throws JessException
Returns the entry at position i in this ValueVector.

Parameters:
i - the 0-based index of the Value to fetch
Returns:
the Value
Throws:
JessException

setLength

public ValueVector setLength(int i)
Set the length of this ValueVector. If necessary the storage will be extended. This can leave some of the elements in the ValueVector with null contents.

Parameters:
i - The new length (>= 0)
Returns:
this object, so methods can be chained.

set

public final ValueVector set(Value val,
                             int i)
                      throws JessException
Set the element at position i to the given value. The argument must be >= 0 and < the return value of size().

Parameters:
val - the new value
i - the index at which to place it.
Returns:
this object, so methods can be chained.
Throws:
JessException

add

public final ValueVector add(Value val)
Add a new element to the end of this ValueVector. The storage will be extended if necessary. This method returns the ValueVector object itself, so that add() calls can be chained together for convenience:
ValueVector vv = new ValueVector();
vv.add(new Value("a", RU.SYMBOL)).add(new Value("b", RU.SYMBOL)).add(new Value("c", RU.SYMBOL));

// Prints "(a b c)"
System.out.println(vv.toStringWithParens());
     

Parameters:
val - the value to add.
Returns:
this object, so methods can be chained.

add

public final ValueVector add(java.lang.String val)
                      throws JessException
Add a new element to the end of this ValueVector.

Parameters:
val - the value to add, interpreted as a symbol
Returns:
this object, so methods can be chained.
Throws:
JessException
See Also:
add(Value)

add

public final ValueVector add(int val)
                      throws JessException
Add a new element to the end of this ValueVector.

Parameters:
val - the value to add, interpreted as an RU.INTEGER
Returns:
this object, so methods can be chained.
Throws:
JessException
See Also:
add(Value)

add

public final ValueVector add(double val)
                      throws JessException
Add a new element to the end of this ValueVector.

Parameters:
val - the value to add, interpreted as an RU.FLOAT
Returns:
this object, so methods can be chained.
Throws:
JessException
See Also:
add(Value)

add

public final ValueVector add(boolean val)
Add a new element to the end of this ValueVector.

Parameters:
val - the value to add; will become one of the symbols TRUE or FALSE
Returns:
this object, so methods can be chained.
See Also:
add(Value)

add

public final ValueVector add(long val)
                      throws JessException
Add a new element to the end of this ValueVector.

Parameters:
val - the value to add, as an RU.LONG
Returns:
this object, so methods can be chained.
Throws:
JessException
See Also:
add(Value)

add

public final ValueVector add(java.lang.Object val)
Add a new element to the end of this ValueVector.

Parameters:
val - the value to add, as an RU.JAVA_OBJECT
Returns:
this object, so methods can be chained.
See Also:
add(Value)

remove

public final ValueVector remove(int i)
                         throws JessException
Remove the item at index i from the ValueVector; move all the higher-numbered elements down.

Returns:
this object, so methods can be chained.
Throws:
JessException

equals

public boolean equals(java.lang.Object o)
Compare this ValueVector to another object.

Parameters:
o - another object
Returns:
true if the object is a ValueVector of the same size containing Values that compare equal to the ones in this ValueVector.

copy

public static void copy(ValueVector src,
                        int srcPos,
                        ValueVector dest,
                        int destPos,
                        int length)
System.arraycopy DeLuxe for ValueVectors. Contributed by Thomas Barnekow.

Parameters:
src - the ValueVector to copy from
srcPos - the index to copy frmo
dest - the ValueVector to copy to
destPos - the index to copy to
length - the number of elements to copy

addAll

public ValueVector addAll(ValueVector vv)
Appends all Values in the argument ValueVector. Contributed by Thomas Barnekow.

Parameters:
vv - a ValueVector to copy elements from.

toString

public java.lang.String toString()
Return a String version of this ValueVector, without parentheses.

Returns:
the String

toStringWithParens

public java.lang.String toStringWithParens()
Return a String version of this ValueVector, with parentheses around all ValueVectors.

Returns:
the String

addAll

public void addAll(java.util.Collection list)
Add all the members of the collection to this ValueVector. All the objects in the collection must be Values.

Parameters:
list - a collection of jess.Value objects
Throws:
java.lang.ClassCastException - if any of the members of the collection are not jess.Value objects

© 2006 Sandia Corporation