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.Objectadd(java.lang.Object l, java.lang.Object r)First callscast(Object)on both arguments then call theaddG(E, E).abstract EaddG(E l, E r)Adds two elements of type E.java.lang.Booleanand(java.lang.Object l, java.lang.Object r)Default implementation returns null.abstract Ecast(java.lang.Object l)Convert the input to type E.java.lang.Integercmp(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.IntegercmpG(E l, E r)Compare two objects of the same type.abstract EconvertFromInt(java.lang.Integer l)Attempt to convert argument from an integerjava.lang.IntegerconvertToInt(java.lang.Object l)Attempt to convert argument to an integerabstract java.lang.IntegerconvertToIntE(E l)If possible convert the argument to an Integer to enable integral powers.java.lang.Objectdiv(java.lang.Object l, java.lang.Object r)Divides two members of the field.abstract EdivG(E l, E r)Divides two elements of type E.abstract EgetOne()Get the multiplicative identity for this field.abstract EgetZero()Get the additive identity for this fieldjava.lang.Objectmod(java.lang.Object l, java.lang.Object r)The modulus of two members of the field.abstract EmodG(E l, E r)Modulus of two elements of type E.java.lang.Objectmul(java.lang.Object l, java.lang.Object r)Multiplies two members of the field.abstract EmulG(E l, E r)Multiplies two elements of type E.java.lang.Objectneg(java.lang.Object l)The negation -x of an element.abstract EnegG(E l)Negates an elements of type E.java.lang.Booleannot(java.lang.Object l)Default implementation returns null.java.lang.Booleanor(java.lang.Object l, java.lang.Object r)Default implementation returns null.java.lang.Objectpow(java.lang.Object l, java.lang.Object r)The power operator.abstract EpowG(E l, E r)Raises l to the power of rjava.lang.Objectsub(java.lang.Object l, java.lang.Object r)Subtract two members of the field.abstract EsubG(E l, E r)Subtracts two elements of type E.
-
-
-
Method Detail
-
getOne
public abstract E getOne()
Description copied from interface:FieldIGet the multiplicative identity for this field.
-
getZero
public abstract E getZero()
Description copied from interface:FieldIGet 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 EvaluationExceptionFirst callscast(Object)on both arguments then call theaddG(E, E). If the result of either cast are null returns null.- Specified by:
addin 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 EvaluationExceptionDescription copied from interface:FieldISubtract two members of the field.- Specified by:
subin 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 EvaluationExceptionDescription copied from interface:FieldIThe negation -x of an element.- Specified by:
negin 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 EvaluationExceptionDescription copied from interface:FieldIMultiplies two members of the field.- Specified by:
mulin 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 EvaluationExceptionDescription copied from interface:FieldIDivides two members of the field.- Specified by:
divin 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 EvaluationExceptionDescription copied from interface:FieldIThe modulus of two members of the field.- Specified by:
modin 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 EvaluationExceptionDescription copied from interface:FieldIThe power operator.- Specified by:
powin 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 EvaluationExceptionDescription copied from class:AbstractComparativeFieldCompare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.- Specified by:
cmpin 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 EvaluationExceptionDefault implementation returns null.- Specified by:
andin 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 EvaluationExceptionDefault implementation returns null.- Specified by:
orin 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 EvaluationExceptionDefault implementation returns null.- Specified by:
notin 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 EvaluationExceptionDescription copied from interface:IntegerConvertorAttempt to convert argument to an integer- Specified by:
convertToIntin 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:IntegerConvertorAttempt to convert argument from an integer- Specified by:
convertFromIntin interfaceIntegerConvertor- Parameters:
l- value to convert- Returns:
- corresponding value in this field or null if it cannot be converted
- Throws:
EvaluationException- probably never
-
-