lp.struct
Class LpFunction

java.lang.Object
  extended by lp.struct.LpAbstractStructureUnit
      extended by lp.struct.LpFunction
All Implemented Interfaces:
LpStructureUnit

public class LpFunction
extends LpAbstractStructureUnit

Represents a function (symbol) in logic programming. Each function has a name and an arity, i.e. a positive integer that specifies the number of terms (see LpTerm) that it takes as arguments. LpFunctions are immutable.

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

Field Summary
private  int arity
          This function's arity.
private  String name
          This function's name.
private static Map<String,LpFunction> pool
          A pool of instances of this immutable class.
 
Fields inherited from class lp.struct.LpAbstractStructureUnit
TO_STRING_BUFFER
 
Constructor Summary
protected LpFunction(String name, int arity)
          Creates a new instance with the given name and arity.
 
Method Summary
 void accept(LpStructureUnitVisitor visitor)
          Accepts LpStructureUnitVisitor instance, i.e. calls visitor.visit(this).
 boolean equals(Object obj)
          Returns true if and only if obj is a LpFunction instance, its name is equal to this function's name as defined by String.equals(Object), its arity is equal to this function's arity
 int getArity()
          Returns this function's arity, the same that was given to the constructor.
static LpFunction getInstance(String name, int arity)
          Returns an instance of LpFunction with the given name and arity.
static LpFunction getInstance(String name, List<LpTerm> arguments)
          Returns an instance of LpFunction with the given name and arity implied by the size of the given argument list.
 String getName()
          Returns this function's name, the same that was given to the constructor.
 int hashCode()
          Overriden in order to maintain the general contract of Object.hashCode().
 String toString()
          Overrides the behaviour of LpAbstractStructureUnit.toString().
 
Methods inherited from class lp.struct.LpAbstractStructureUnit
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,LpFunction> pool
A pool of instances of this immutable class. The getInstance(String, int) method first looks here for the requested instances and if it is forced to create a new one, it is added to this pool.


name

private final String name
This function's name.


arity

private final int arity
This function's arity.

Constructor Detail

LpFunction

protected LpFunction(String name,
                     int arity)
Creates a new instance with the given name and arity. name is required to be a non-empty string and arity must be positive. Functions of arity 0 are actually constants, so LpConstant is to be used instead.

Parameters:
name - the name of the function
arity - the function's arity
Throws:
IllegalArgumentException - if name is null or name is an empty string or arity is not positive
Method Detail

getInstance

public static LpFunction getInstance(String name,
                                     int arity)
Returns an instance of LpFunction with the given name and arity. name is required to be a non-empty string and arity must be positive. Functions of arity 0 are actually constants, so LpConstant is to be used instead.

Parameters:
name - the name of the requested function
arity - the requested function's arity
Throws:
IllegalArgumentException - if name is null or name is an empty string or arity is not positive

getInstance

public static LpFunction getInstance(String name,
                                     List<LpTerm> arguments)
Returns an instance of LpFunction with the given name and arity implied by the size of the given argument list. name is required to be a non-empty string and arguments must have at least one member. Functions of arity 0 are actually constants, so LpConstant is to be used instead.

Parameters:
name - the name of the requested function
arguments - the sample argument list, its size is the function's arity
Throws:
IllegalArgumentException - if name is null or name is an empty string or arguments is null or arguments is empty

getName

public String getName()
Returns this function's name, the same that was given to the constructor.

Returns:
the name of this function

getArity

public int getArity()
Returns this function's arity, the same that was given to the constructor. Guaranteed to be positive.

Returns:
this function's arity

accept

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

Parameters:
visitor - the visitor to accept

equals

public boolean equals(Object obj)
Returns true if and only if
  1. obj is a LpFunction instance,
  2. its name is equal to this function's name as defined by String.equals(Object),
  3. its arity is equal to this function's arity

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:
String.equals(Object)

hashCode

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

Overrides:
hashCode in class Object
Returns:
the hash of this object
See Also:
String.hashCode()

toString

public String toString()
Overrides the behaviour of LpAbstractStructureUnit.toString(). Returns the normal form (i.e. the function's name) followed by a "/" and arity.

Overrides:
toString in class LpAbstractStructureUnit