lp.struct
Class LpConstant

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

public class LpConstant
extends LpAbstractStructureUnit
implements LpTerm

Represents a constant in logic programming. Constants are, together with function symbols and variables (see LpFunction and LpVariable) building units of terms (see LpTerm. Each constant has a unique non-empty name. LpConstants are immutable.

Version:
1.0.0
Author:
Martin Slota
See Also:
LpTerm

Field Summary
private  String name
          This constant's name.
private static Map<String,LpConstant> pool
          A pool of instances of this immutable class.
 
Fields inherited from class lp.struct.LpAbstractStructureUnit
TO_STRING_BUFFER
 
Constructor Summary
protected LpConstant(String name)
          Creates a new instance of LpConstant with the given name.
 
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 LpConstant instance, its name is equal to this constant's name as defined by String.equals(Object),
static LpConstant getInstance(String name)
          Returns an instance of LpConstant with the given name.
 String getName()
          Returns this constant's name, the same that was given to the constructor.
 int hashCode()
          Overriden in order to maintain the general contract of Object.hashCode().
 
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,LpConstant> pool
A pool of instances of this immutable class. The getInstance(String) 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 constant's name.

Constructor Detail

LpConstant

protected LpConstant(String name)
Creates a new instance of LpConstant with the given name.

Parameters:
name - the new constant's name
Throws:
IllegalArgumentException - if name is null or an empty string
Method Detail

getInstance

public static LpConstant getInstance(String name)
Returns an instance of LpConstant with the given name.

Parameters:
name - the requested constant's name
Throws:
IllegalArgumentException - if name is null or an empty string

getName

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

Returns:
the name of this contant

accept

public void accept(LpStructureUnitVisitor visitor)
Accepts LpStructureUnitVisitor instance, i.e. calls 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 LpConstant instance,
  2. its name is equal to this constant's name as defined by String.equals(Object),

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