lp.struct
Class LpCompoundTerm

java.lang.Object
  extended by lp.struct.LpAbstractStructureUnit
      extended by lp.struct.LpCompoundTerm
All Implemented Interfaces:
LpStructureUnit, LpTerm

public class LpCompoundTerm
extends LpAbstractStructureUnit
implements LpTerm

This class represents a compound term in logic programming. Such a term is composed of a function symbol (represented by a LpFunction instance) and its arguments—terms represented by LpTerm instances. The number of arguments must match the function's arity (returned by LpFunction.getArity()). This class is immutable as long as the argument list used to create it is not changed later.

Version:
1.0.0
Author:
Martin Slota
See Also:
LpFunction, LpTerm

Field Summary
private  List<LpTerm> arguments
          The function's arguments.
private  LpFunction function
          The top-most function symbol that is used in this compound term.
private static LpBuffer keyBuilder
          Used to create construct keys in pool.
private static Map<String,LpCompoundTerm> pool
          A pool of instances of this immutable class.
 
Fields inherited from class lp.struct.LpAbstractStructureUnit
TO_STRING_BUFFER
 
Constructor Summary
private LpCompoundTerm(LpFunction function, List<LpTerm> arguments)
          Creates a new instance with the given values as members.
 
Method Summary
 void accept(LpStructureUnitVisitor visitor)
          Accepts LpStructureUnitVisitor instance, i.e. calls its visitor.visit(this).
 boolean equals(Object obj)
          Returns true if and only if obj is a LpCompoundTerm instance, its function symbol is equal to this term's function symbol as defined by LpFunction.equals(Object), its argument list is equal to this term's argument list
 List<LpTerm> getArguments()
          Returns the list of arguments of this compound term.
 LpFunction getFunction()
          Returns the function symbol of this compound term, the same that was given to the constructor.
static LpCompoundTerm getInstance(LpFunction function, List<LpTerm> arguments)
          Returns an instance of LpCompoundTerm with the given function symbol and argument list.
 int hashCode()
          Overriden in order to maintain the general contract of Object.hashCode().
private static void checkArity(LpFunction function, List<LpTerm> arguments)
          Checks whether function.getArity() equals arguments.size().
 
Methods inherited from class lp.struct.LpAbstractStructureUnit
toString, toUnmodifiableList, toUnmodifiableSet
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

pool

private static final Map<String,LpCompoundTerm> pool
A pool of instances of this immutable class. The getInstance(LpFunction, List) method first looks here for the requested instances and if it is forced to create a new one, it is added to this pool.


keyBuilder

private static final LpBuffer keyBuilder
Used to create construct keys in pool.


function

private final LpFunction function
The top-most function symbol that is used in this compound term.


arguments

private final List<LpTerm> arguments
The function's arguments.

Constructor Detail

LpCompoundTerm

private LpCompoundTerm(LpFunction function,
                       List<LpTerm> arguments)
Creates a new instance with the given values as members. Arguments of the constructor are not copied (especially the arguments List).

Parameters:
function - the top-most function symbol used in this compound term
arguments - terms that will become arguments of the function
Throws:
IllegalArgumentException - if function is null or the number of arguments doesn't match the function's arity, i.e. if this condition doesn't hold:
(arguments == null && function.getArity() == 0) || arguments.size() == function.getArity()
Method Detail

getInstance

public static LpCompoundTerm getInstance(LpFunction function,
                                         List<LpTerm> arguments)
Returns an instance of LpCompoundTerm with the given function symbol and argument list.

Parameters:
function - the requested term's function symbol
arguments - the argument list of the requested LpCompoundTerm instance
Throws:
IllegalArgumentException - if function is null or the number of args doesn't match the function's arity, i.e. this condition doesn't hold:
(args == null && function.getArity() == 0) || args.size() == function.getArity()

checkArity

private static void checkArity(LpFunction function,
                               List<LpTerm> arguments)
Checks whether function.getArity() equals arguments.size(). If it doesn't, throws an IllegalArgumentException.

Parameters:
function - a function, its arity must match the length of the argument list
arguments - an argument list, its length must match the function's arity
Throws:
IllegalArgumentException - if function.getArity() is not the same as arguments.size()

getFunction

public LpFunction getFunction()
Returns the function symbol of this compound term, the same that was given to the constructor.

Returns:
the LpFunction instance given in the constructor

getArguments

public List<LpTerm> getArguments()
Returns the list of arguments of this compound term. Doesn't return null, even if it was given to the constructor of this instance (in that case an empty list is returned).

Returns:
an unmodifiable version of the argument list from the constructor

accept

public void accept(LpStructureUnitVisitor visitor)
Accepts LpStructureUnitVisitor instance, i.e. calls its visitor.visit(this).

Specified by:
accept in interface LpStructureUnit
Parameters:
visitor - the visitor to accept

equals

public boolean equals(Object obj)
Returns true if and only if
  1. obj is a LpCompoundTerm instance,
  2. its function symbol is equal to this term's function symbol as defined by LpFunction.equals(Object),
  3. its argument list is equal to this term's argument list

Specified by:
equals in interface LpTerm
Overrides:
equals in class Object
Parameters:
obj - the object to compare with
Returns:
true if this object is equal to obj according to the description above, and false otherwise
See Also:
LpFunction.equals(Object), List.equals(Object), LpTerm.equals(Object)

hashCode

public int hashCode()
Overriden in order to maintain the general contract of Object.hashCode().

Specified by:
hashCode in interface LpTerm
Overrides:
hashCode in class Object
Returns:
the hash of this object
See Also:
LpFunction.hashCode(), List.hashCode(), LpTerm.equals(Object)