Package gw.lang.parser
Class StandardCoercionManager
- java.lang.Object
-
- gw.config.BaseService
-
- gw.lang.parser.StandardCoercionManager
-
- All Implemented Interfaces:
IService
,ICoercionManager
,IPluginHost
- Direct Known Subclasses:
GWCoercionManager
public class StandardCoercionManager extends BaseService implements ICoercionManager
-
-
Field Summary
Fields Modifier and Type Field Description TypeSystemAwareCache<Pair<IType,IType>,ICoercer>
_coercerCache
static Object
NO_DICE
-
Constructor Summary
Constructors Constructor Description StandardCoercionManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static boolean
arePrimitiveTypesAssignable(IType toType, IType fromType)
boolean
canCoerce(IType lhsType, IType rhsType)
Object
convertNullAsPrimitive(IType intrType, boolean isForBoxing)
Converts a null value to a value compatible with the specified primitive type.Object
convertValue(Object value, IType intrType)
Given a value and a target Class, return a compatible value via the target Class.ICoercer
findCoercer(IType lhsType, IType rhsType, boolean runtime)
String
formatDate(Date value, String strFormat)
String
formatNumber(Double value, String strFormat)
String
formatTime(Date value, String strFormat)
protected ICoercer
getCoercerInternal(IType lhsType, IType rhsType, boolean runtime)
Returns a coercer from values of rhsType to values of lhsType if one exists.protected ICoercer
getHighPriorityPrimitiveOrBoxedConverter(IType type)
protected ICoercer
getPrimitiveOrBoxedConverter(IType type)
static boolean
isBoxed(IType lhsType)
boolean
isDateTime(String str)
static boolean
isObjectMethod(IMethodInfo mi)
boolean
isPrimitiveOrBoxed(IType lhsType)
Determine whether the specified type is either a primitive or a boxed primitive.static boolean
isStructurallyAssignable(IType toType, IType fromType)
static boolean
isStructurallyAssignable_Laxed(IType toType, IType fromType)
static boolean
isStructurallyAssignable_Laxed(IType toType, IType fromType, TypeVarToTypeMap inferenceMap)
static boolean
isStructurallyAssignable_Laxed(IType toType, IType fromType, IMethodInfo specificMethod, TypeVarToTypeMap inferenceMap)
BigDecimal
makeBigDecimalFrom(Object obj)
BigInteger
makeBigIntegerFrom(Object obj)
Boolean
makeBooleanFrom(Object obj)
Date
makeDateFrom(Object obj)
Returns a new Date instance representing the object.Double
makeDoubleFrom(Object obj)
Returns a Double for an arbitrary object.Float
makeFloatFrom(Object obj)
Integer
makeIntegerFrom(Object obj)
Long
makeLongFrom(Object obj)
boolean
makePrimitiveBooleanFrom(Object obj)
double
makePrimitiveDoubleFrom(Object obj)
float
makePrimitiveFloatFrom(Object obj)
int
makePrimitiveIntegerFrom(Object obj)
long
makePrimitiveLongFrom(Object obj)
Rational
makeRationalFrom(Object obj)
String
makeStringFrom(Object obj)
boolean
notCoercibleOrRequiresExplicitCoercion(IType lhsType, IType rhsType)
Date
parseDateTime(String str)
Produce a date from a string using standard DateFormat parsing.Number
parseNumber(String strValue)
ICoercer
resolveCoercerStatically(IType typeToCoerceTo, IType typeToCoerceFrom)
Determine and return a statically valid coercer from the rhsType to the lhsType.IType
verifyTypesComparable(IType lhsType, IType rhsType, boolean bBiDirectional)
Verifies that the right hand type can be converted or coerced to the left hand type.IType
verifyTypesComparable(IType lhsType, IType rhsType, boolean bBiDirectional, IFullParserState parserState)
Verifies that the right hand type can be converted or coerced to the left hand type.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gw.plugin.IPluginHost
getInterface
-
-
-
-
Method Detail
-
canCoerce
public final boolean canCoerce(IType lhsType, IType rhsType)
- Specified by:
canCoerce
in interfaceICoercionManager
- Parameters:
lhsType
- type to be coerced torhsType
- type to be coerced from- Returns:
- true if a coercion exists from rhsType to lhsType, false otherwise
-
findCoercer
public final ICoercer findCoercer(IType lhsType, IType rhsType, boolean runtime)
- Specified by:
findCoercer
in interfaceICoercionManager
-
getCoercerInternal
protected ICoercer getCoercerInternal(IType lhsType, IType rhsType, boolean runtime)
Returns a coercer from values of rhsType to values of lhsType if one exists. I tried to write a reasonable spec in the comments below that indicate exactly what should coerce to what.- Parameters:
lhsType
- the type to coerce torhsType
- the type to coerce fromruntime
- true if the coercion is happening at runtime rather than compile time (note: This param should go away as we store the coercions on the parsed elements, rather than calling into the coercion manager)- Returns:
- a coercer from the lhsType to the rhsType, or null if no such coercer exists or is needed
-
isPrimitiveOrBoxed
public boolean isPrimitiveOrBoxed(IType lhsType)
Description copied from interface:ICoercionManager
Determine whether the specified type is either a primitive or a boxed primitive.- Specified by:
isPrimitiveOrBoxed
in interfaceICoercionManager
- Parameters:
lhsType
- the type to examine- Returns:
- true if the specified type is a primitive or a boxed primitive
-
isBoxed
public static boolean isBoxed(IType lhsType)
-
getHighPriorityPrimitiveOrBoxedConverter
protected ICoercer getHighPriorityPrimitiveOrBoxedConverter(IType type)
-
verifyTypesComparable
public IType verifyTypesComparable(IType lhsType, IType rhsType, boolean bBiDirectional) throws ParseException
Description copied from interface:ICoercionManager
Verifies that the right hand type can be converted or coerced to the left hand type. If bBiDirectional is true, it will verify that either converts to another- Specified by:
verifyTypesComparable
in interfaceICoercionManager
- Throws:
ParseException
-
verifyTypesComparable
public IType verifyTypesComparable(IType lhsType, IType rhsType, boolean bBiDirectional, IFullParserState parserState) throws ParseException
Description copied from interface:ICoercionManager
Verifies that the right hand type can be converted or coerced to the left hand type. If bBiDirectional is true, it will verify that either converts to another. The parser state will be used to report parse errors with.- Specified by:
verifyTypesComparable
in interfaceICoercionManager
- Throws:
ParseException
-
isStructurallyAssignable
public static boolean isStructurallyAssignable(IType toType, IType fromType)
-
isStructurallyAssignable_Laxed
public static boolean isStructurallyAssignable_Laxed(IType toType, IType fromType)
-
isStructurallyAssignable_Laxed
public static boolean isStructurallyAssignable_Laxed(IType toType, IType fromType, TypeVarToTypeMap inferenceMap)
-
isStructurallyAssignable_Laxed
public static boolean isStructurallyAssignable_Laxed(IType toType, IType fromType, IMethodInfo specificMethod, TypeVarToTypeMap inferenceMap)
-
arePrimitiveTypesAssignable
public static boolean arePrimitiveTypesAssignable(IType toType, IType fromType)
-
isObjectMethod
public static boolean isObjectMethod(IMethodInfo mi)
-
notCoercibleOrRequiresExplicitCoercion
public boolean notCoercibleOrRequiresExplicitCoercion(IType lhsType, IType rhsType)
- Specified by:
notCoercibleOrRequiresExplicitCoercion
in interfaceICoercionManager
-
convertValue
public final Object convertValue(Object value, IType intrType)
Given a value and a target Class, return a compatible value via the target Class.- Specified by:
convertValue
in interfaceICoercionManager
- Parameters:
value
- the value to coerce (may not be null)intrType
- type to coerce to- Returns:
- the converted value
-
convertNullAsPrimitive
public Object convertNullAsPrimitive(IType intrType, boolean isForBoxing)
Description copied from interface:ICoercionManager
Converts a null value to a value compatible with the specified primitive type.- Specified by:
convertNullAsPrimitive
in interfaceICoercionManager
- Parameters:
intrType
- The primitive type to convert to.- Returns:
- A wrapped primitive value corresponding to null.
-
resolveCoercerStatically
public ICoercer resolveCoercerStatically(IType typeToCoerceTo, IType typeToCoerceFrom)
Description copied from interface:ICoercionManager
Determine and return a statically valid coercer from the rhsType to the lhsType. Returns null if no coercion is necessary.- Specified by:
resolveCoercerStatically
in interfaceICoercionManager
- Parameters:
typeToCoerceTo
- the type to coerce totypeToCoerceFrom
- the type to coerce from
-
makeDoubleFrom
public Double makeDoubleFrom(Object obj)
Description copied from interface:ICoercionManager
Returns a Double for an arbitrary object. Uses a semi-intelligent algorithm to create an appropriate Double instance. If the Object argument is a:null value - an appropriate value respecting this parsed element's nullAsZero setting e.g., either null or an 'empty' value. Double - returns the argument as is. Number - the Number's doubleValue(). String - Double.parseDouble( String ) Boolean - a pooled instance of either Double( 0 ) or Double( 1 ) Date - A Double for Date.getTime(). default - A parsed Double for the Object argument's toString() method.
- Specified by:
makeDoubleFrom
in interfaceICoercionManager
- Returns:
- A Double for an arbitrary object (may return a pooled instance).
-
makePrimitiveIntegerFrom
public int makePrimitiveIntegerFrom(Object obj)
- Specified by:
makePrimitiveIntegerFrom
in interfaceICoercionManager
-
makeIntegerFrom
public Integer makeIntegerFrom(Object obj)
- Specified by:
makeIntegerFrom
in interfaceICoercionManager
-
makePrimitiveLongFrom
public long makePrimitiveLongFrom(Object obj)
- Specified by:
makePrimitiveLongFrom
in interfaceICoercionManager
-
makeLongFrom
public Long makeLongFrom(Object obj)
- Specified by:
makeLongFrom
in interfaceICoercionManager
-
makeBigDecimalFrom
public BigDecimal makeBigDecimalFrom(Object obj)
- Specified by:
makeBigDecimalFrom
in interfaceICoercionManager
-
makeRationalFrom
public Rational makeRationalFrom(Object obj)
- Specified by:
makeRationalFrom
in interfaceICoercionManager
-
makeBigIntegerFrom
public BigInteger makeBigIntegerFrom(Object obj)
- Specified by:
makeBigIntegerFrom
in interfaceICoercionManager
-
makePrimitiveDoubleFrom
public double makePrimitiveDoubleFrom(Object obj)
- Specified by:
makePrimitiveDoubleFrom
in interfaceICoercionManager
-
makeFloatFrom
public Float makeFloatFrom(Object obj)
- Specified by:
makeFloatFrom
in interfaceICoercionManager
-
makePrimitiveFloatFrom
public float makePrimitiveFloatFrom(Object obj)
- Specified by:
makePrimitiveFloatFrom
in interfaceICoercionManager
-
makeStringFrom
public String makeStringFrom(Object obj)
- Specified by:
makeStringFrom
in interfaceICoercionManager
-
makePrimitiveBooleanFrom
public boolean makePrimitiveBooleanFrom(Object obj)
- Specified by:
makePrimitiveBooleanFrom
in interfaceICoercionManager
- Returns:
- A Boolean for an arbitrary object.
-
makeBooleanFrom
public Boolean makeBooleanFrom(Object obj)
- Specified by:
makeBooleanFrom
in interfaceICoercionManager
-
makeDateFrom
public Date makeDateFrom(Object obj)
Returns a new Date instance representing the object.- Specified by:
makeDateFrom
in interfaceICoercionManager
-
isDateTime
public boolean isDateTime(String str) throws ParseException
- Specified by:
isDateTime
in interfaceICoercionManager
- Throws:
ParseException
-
parseDateTime
public Date parseDateTime(String str) throws ParseException
Produce a date from a string using standard DateFormat parsing.- Specified by:
parseDateTime
in interfaceICoercionManager
- Throws:
ParseException
-
formatDate
public String formatDate(Date value, String strFormat)
- Specified by:
formatDate
in interfaceICoercionManager
-
formatTime
public String formatTime(Date value, String strFormat)
- Specified by:
formatTime
in interfaceICoercionManager
-
formatNumber
public String formatNumber(Double value, String strFormat)
- Specified by:
formatNumber
in interfaceICoercionManager
-
parseNumber
public Number parseNumber(String strValue)
- Specified by:
parseNumber
in interfaceICoercionManager
-
-