Class GenericField<E>
- java.lang.Object
-
- com.singularsys.extensions.field.implementations.AbstractComparativeField
-
- com.singularsys.extensions.field.implementations.GenericField<E>
-
- Type Parameters:
E
- Basic type field uses, e.g. Double or Complex.
- All Implemented Interfaces:
FieldI
,IntegerConvertor
,java.io.Serializable
- Direct Known Subclasses:
BigDecimalField
,BigIntegerField
,BigModulusField
,ExtDocsTest.StringField3
,GenericPowerField
,IntegerField
,ModulusField
public abstract class GenericField<E> extends AbstractComparativeField implements IntegerConvertor
Abstract-generic base class where the type-variable specifies the arguments and return type. Sub classes must implementE cast(Object l)
which converts arguments to the type-E, and also methodsE addG(E l, E r)
etc. The class provides implementations ofObject add(Object l,Object r)
which callscast(Object l)
on both arguments and then callsaddG
, if either argument converts to null then null is returned. It has null implementations for the logical operators.An examples for a Field using Strings:
public class StringField3 extends GenericField<String> { private static final long serialVersionUID = 330L; @Override public String cast(Object l) throws EvaluationException { if(l instanceof String) return ((String) l); return null; } // No need for casting in individual operations @Override public String addG(String l, String r) throws EvaluationException { return l + r; } // A single method for all comparison operations @Override public Integer cmpG(String l, String r) throws EvaluationException { return l.compareTo(r); } // other methods just return null @Override public String subG(String l, String r) { return null; } @Override public String negG(String l) { return null; } @Override public String mulG(String l, String r) { return null; } @Override public String divG(String l, String r) { return null; } @Override public String modG(String l, String r) { return null; } @Override public String powG(String l, String r) { return null; } // no need to implement comparison operations le(l,r) etc. // or logical operations and(l,r), or(l,r), not(l) }
- Author:
- Richard Morris
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description GenericField()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description java.lang.Object
add(java.lang.Object l, java.lang.Object r)
First callscast(Object)
on both arguments then call theaddG(E, E)
.abstract E
addG(E l, E r)
Adds two elements of type E.java.lang.Boolean
and(java.lang.Object l, java.lang.Object r)
Default implementation returns null.abstract E
cast(java.lang.Object l)
Convert the input to type E.java.lang.Integer
cmp(java.lang.Object l, java.lang.Object r)
Compare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.abstract java.lang.Integer
cmpG(E l, E r)
Compare two objects of the same type.abstract E
convertFromInt(java.lang.Integer l)
Attempt to convert argument from an integerjava.lang.Integer
convertToInt(java.lang.Object l)
Attempt to convert argument to an integerabstract java.lang.Integer
convertToIntE(E l)
If possible convert the argument to an Integer to enable integral powers.java.lang.Object
div(java.lang.Object l, java.lang.Object r)
Divides two members of the field.abstract E
divG(E l, E r)
Divides two elements of type E.abstract E
getOne()
Get the multiplicative identity for this field.abstract E
getZero()
Get the additive identity for this fieldjava.lang.Object
mod(java.lang.Object l, java.lang.Object r)
The modulus of two members of the field.abstract E
modG(E l, E r)
Modulus of two elements of type E.java.lang.Object
mul(java.lang.Object l, java.lang.Object r)
Multiplies two members of the field.abstract E
mulG(E l, E r)
Multiplies two elements of type E.java.lang.Object
neg(java.lang.Object l)
The negation -x of an element.abstract E
negG(E l)
Negates an elements of type E.java.lang.Boolean
not(java.lang.Object l)
Default implementation returns null.java.lang.Boolean
or(java.lang.Object l, java.lang.Object r)
Default implementation returns null.java.lang.Object
pow(java.lang.Object l, java.lang.Object r)
The power operator.abstract E
powG(E l, E r)
Raises l to the power of rjava.lang.Object
sub(java.lang.Object l, java.lang.Object r)
Subtract two members of the field.abstract E
subG(E l, E r)
Subtracts two elements of type E.
-
-
-
Method Detail
-
getOne
public abstract E getOne()
Description copied from interface:FieldI
Get the multiplicative identity for this field.
-
getZero
public abstract E getZero()
Description copied from interface:FieldI
Get the additive identity for this field
-
cast
public abstract E cast(java.lang.Object l) throws EvaluationException
Convert the input to type E.- Parameters:
l
-- Returns:
- l cast to type E if possible or null if casting is not possible
- Throws:
EvaluationException
-
addG
public abstract E addG(E l, E r) throws EvaluationException
Adds two elements of type E.- Parameters:
l
- lhs argumentr
- rhs argument- Returns:
- the sum
- Throws:
EvaluationException
- on error
-
subG
public abstract E subG(E l, E r) throws EvaluationException
Subtracts two elements of type E.- Parameters:
l
- lhs argumentr
- rhs argument- Returns:
- the difference
- Throws:
EvaluationException
- on error
-
negG
public abstract E negG(E l) throws EvaluationException
Negates an elements of type E.- Parameters:
l
- the argument- Returns:
- the negation
- Throws:
EvaluationException
- on error
-
mulG
public abstract E mulG(E l, E r) throws EvaluationException
Multiplies two elements of type E.- Parameters:
l
- lhs argumentr
- rhs argument- Returns:
- the product
- Throws:
EvaluationException
- on error
-
divG
public abstract E divG(E l, E r) throws EvaluationException
Divides two elements of type E.- Parameters:
l
- numeratorr
- denominator- Returns:
- the division
- Throws:
EvaluationException
- on error
-
modG
public abstract E modG(E l, E r) throws EvaluationException
Modulus of two elements of type E.- Parameters:
l
- lhs argumentr
- rhs argument- Returns:
- the modulus
- Throws:
EvaluationException
- on error
-
powG
public abstract E powG(E l, E r) throws EvaluationException
Raises l to the power of r- Parameters:
l
- lhs argumentr
- rhs argument- Returns:
- the power
- Throws:
EvaluationException
- on error
-
cmpG
public abstract java.lang.Integer cmpG(E l, E r) throws EvaluationException
Compare two objects of the same type.- Parameters:
l
- left argumentr
- right argument- Returns:
- -1, 0, 1 or null
- Throws:
EvaluationException
-
add
public java.lang.Object add(java.lang.Object l, java.lang.Object r) throws EvaluationException
First callscast(Object)
on both arguments then call theaddG(E, E)
. If the result of either cast are null returns null.- Specified by:
add
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
sub
public java.lang.Object sub(java.lang.Object l, java.lang.Object r) throws EvaluationException
Description copied from interface:FieldI
Subtract two members of the field.- Specified by:
sub
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
neg
public java.lang.Object neg(java.lang.Object l) throws EvaluationException
Description copied from interface:FieldI
The negation -x of an element.- Specified by:
neg
in interfaceFieldI
- Parameters:
l
- the argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
mul
public java.lang.Object mul(java.lang.Object l, java.lang.Object r) throws EvaluationException
Description copied from interface:FieldI
Multiplies two members of the field.- Specified by:
mul
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
div
public java.lang.Object div(java.lang.Object l, java.lang.Object r) throws EvaluationException
Description copied from interface:FieldI
Divides two members of the field.- Specified by:
div
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
mod
public java.lang.Object mod(java.lang.Object l, java.lang.Object r) throws EvaluationException
Description copied from interface:FieldI
The modulus of two members of the field.- Specified by:
mod
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
pow
public java.lang.Object pow(java.lang.Object l, java.lang.Object r) throws EvaluationException
Description copied from interface:FieldI
The power operator.- Specified by:
pow
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
cmp
public java.lang.Integer cmp(java.lang.Object l, java.lang.Object r) throws EvaluationException
Description copied from class:AbstractComparativeField
Compare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.- Specified by:
cmp
in classAbstractComparativeField
- Returns:
- -1, 0, 1 if l < r, l==r or l > r respectively, or null if comparison cannot be carried out or
- Throws:
EvaluationException
-
and
public java.lang.Boolean and(java.lang.Object l, java.lang.Object r) throws EvaluationException
Default implementation returns null.- Specified by:
and
in interfaceFieldI
- Parameters:
l
- left hand argument implimentationr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
or
public java.lang.Boolean or(java.lang.Object l, java.lang.Object r) throws EvaluationException
Default implementation returns null.- Specified by:
or
in interfaceFieldI
- Parameters:
l
- left hand argumentr
- right hand argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
not
public java.lang.Boolean not(java.lang.Object l) throws EvaluationException
Default implementation returns null.- Specified by:
not
in interfaceFieldI
- Parameters:
l
- argument- Returns:
- an object representing the result or null if it can not be evaluated.
- Throws:
EvaluationException
- on error
-
convertToIntE
public abstract java.lang.Integer convertToIntE(E l) throws EvaluationException
If possible convert the argument to an Integer to enable integral powers. If the argument cannot be converted null should be returned and thepowG(E,E)
method will be called.- Parameters:
l
- argument to convert.- Returns:
- an integer or null if argument cannot be converted
- Throws:
EvaluationException
-
convertToInt
public java.lang.Integer convertToInt(java.lang.Object l) throws EvaluationException
Description copied from interface:IntegerConvertor
Attempt to convert argument to an integer- Specified by:
convertToInt
in interfaceIntegerConvertor
- Parameters:
l
- value to convert- Returns:
- corresponding integer or null if it cannot be converted
- Throws:
EvaluationException
- on error
-
convertFromInt
public abstract E convertFromInt(java.lang.Integer l) throws EvaluationException
Description copied from interface:IntegerConvertor
Attempt to convert argument from an integer- Specified by:
convertFromInt
in interfaceIntegerConvertor
- Parameters:
l
- value to convert- Returns:
- corresponding value in this field or null if it cannot be converted
- Throws:
EvaluationException
- probably never
-
-