Class GenericField<E>
java.lang.Object
com.singularsys.extensions.field.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,Serializable
- Direct Known Subclasses:
BigDecimalField,BigIntegerField,BigModulusField,ExtDocsTest.StringField3,FixedPointField,GenericPowerField,IntegerField,ModulusField
Abstract-generic base class where the type-variable specifies the arguments and return type.
Subclasses must implement
E cast(Object l)
which converts arguments to the type-E, and also methods
E addG(E l, E r) etc. The class provides implementations of
Object add(Object l,Object r) which calls cast(Object l)
on both arguments and then calls addG,
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:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionFirst callscast(Object)on both arguments then call theaddG(E, E).abstract EAdds two elements of type E.Default implementation returns null.abstract EConvert the input to type E.Compare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.abstract IntegerCompare two objects of the same type.abstract EAttempt to convert argument from an integerAttempt to convert argument to an integerabstract IntegerconvertToIntE(E l) If possible convert the argument to an Integer to enable integral powers.Divides two members of the field.abstract EDivides two elements of type E.abstract EgetOne()Get the multiplicative identity for this field.abstract EgetZero()Get the additive identity for this fieldThe modulus of two members of the field.abstract EModulus of two elements of type E.Multiplies two members of the field.abstract EMultiplies two elements of type E.The negation -x of an element.abstract ENegates an elements of type E.Default implementation returns null.Default implementation returns null.The power operator.abstract ERaises l to the power of rSubtract two members of the field.abstract ESubtracts two elements of type E.
-
Constructor Details
-
GenericField
public GenericField()
-
-
Method Details
-
getOne
Description copied from interface:FieldIGet the multiplicative identity for this field. -
getZero
Description copied from interface:FieldIGet the additive identity for this field -
cast
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
Adds two elements of type E.- Parameters:
l- lhs argumentr- rhs argument- Returns:
- the sum
- Throws:
EvaluationException- on error
-
subG
Subtracts two elements of type E.- Parameters:
l- lhs argumentr- rhs argument- Returns:
- the difference
- Throws:
EvaluationException- on error
-
negG
Negates an elements of type E.- Parameters:
l- the argument- Returns:
- the negation
- Throws:
EvaluationException- on error
-
mulG
Multiplies two elements of type E.- Parameters:
l- lhs argumentr- rhs argument- Returns:
- the product
- Throws:
EvaluationException- on error
-
divG
Divides two elements of type E.- Parameters:
l- numeratorr- denominator- Returns:
- the division
- Throws:
EvaluationException- on error
-
modG
Modulus of two elements of type E.- Parameters:
l- lhs argumentr- rhs argument- Returns:
- the modulus
- Throws:
EvaluationException- on error
-
powG
Raises l to the power of r- Parameters:
l- lhs argumentr- rhs argument- Returns:
- the power
- Throws:
EvaluationException- on error
-
cmpG
Compare two objects of the same type.- Parameters:
l- left argumentr- right argument- Returns:
- -1, 0, 1 or null
- Throws:
EvaluationException
-
add
First 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
Description 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
Description 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
Description 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
Description 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
Description 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
Description 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
Description copied from class:AbstractComparativeFieldCompare two elements, return -1,0,1 if the comparison can be carried out, null otherwise.- Specified by:
cmpin classAbstractComparativeField- Parameters:
l-r-- 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
Default implementation returns null. -
or
Default implementation returns null. -
not
Default implementation returns null. -
convertToIntE
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
Description 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
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
-