Package gw.lang.parser
Class StandardCoercionManager
- java.lang.Object
-
- gw.config.BaseService
-
- gw.lang.parser.StandardCoercionManager
-
- All Implemented Interfaces:
IService,ICoercionManager,IPluginHost
public class StandardCoercionManager extends BaseService implements ICoercionManager
-
-
Field Summary
Fields Modifier and Type Field Description TypeSystemAwareCache<Pair<IType,IType>,ICoercer>_coercerCachestatic ObjectNO_DICE
-
Constructor Summary
Constructors Constructor Description StandardCoercionManager()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static booleanarePrimitiveTypesAssignable(IType toType, IType fromType)booleancanCoerce(IType lhsType, IType rhsType)ObjectconvertNullAsPrimitive(IType intrType, boolean isForBoxing)Converts a null value to a value compatible with the specified primitive type.ObjectconvertValue(Object value, IType intrType)Given a value and a target Class, return a compatible value via the target Class.ICoercerfindCoercer(IType lhsType, IType rhsType, boolean runtime)StringformatDate(Date value, String strFormat)StringformatNumber(Double value, String strFormat)StringformatTime(Date value, String strFormat)protected ICoercergetCoercerInternal(IType lhsType, IType rhsType, boolean runtime)Returns a coercer from values of rhsType to values of lhsType if one exists.protected ICoercergetHighPriorityPrimitiveOrBoxedConverter(IType type)protected ICoercergetPrimitiveOrBoxedConverter(IType type)static booleanisBoxed(IType lhsType)booleanisDateTime(String str)static booleanisObjectMethod(IMethodInfo mi)booleanisPrimitiveOrBoxed(IType lhsType)Determine whether the specified type is either a primitive or a boxed primitive.static booleanisStructurallyAssignable(IType toType, IType fromType)static booleanisStructurallyAssignable_Laxed(IType toType, IType fromType)static booleanisStructurallyAssignable_Laxed(IType toType, IType fromType, TypeVarToTypeMap inferenceMap)static booleanisStructurallyAssignable_Laxed(IType toType, IType fromType, IMethodInfo specificMethod, TypeVarToTypeMap inferenceMap)BigDecimalmakeBigDecimalFrom(Object obj)BigIntegermakeBigIntegerFrom(Object obj)BooleanmakeBooleanFrom(Object obj)DatemakeDateFrom(Object obj)Returns a new Date instance representing the object.DoublemakeDoubleFrom(Object obj)Returns a Double for an arbitrary object.FloatmakeFloatFrom(Object obj)IntegermakeIntegerFrom(Object obj)LongmakeLongFrom(Object obj)booleanmakePrimitiveBooleanFrom(Object obj)doublemakePrimitiveDoubleFrom(Object obj)floatmakePrimitiveFloatFrom(Object obj)intmakePrimitiveIntegerFrom(Object obj)longmakePrimitiveLongFrom(Object obj)RationalmakeRationalFrom(Object obj)StringmakeStringFrom(Object obj)booleannotCoercibleOrRequiresExplicitCoercion(IType lhsType, IType rhsType)DateparseDateTime(String str)Produce a date from a string using standard DateFormat parsing.NumberparseNumber(String strValue)ICoercerresolveCoercerStatically(IType typeToCoerceTo, IType typeToCoerceFrom)Determine and return a statically valid coercer from the rhsType to the lhsType.ITypeverifyTypesComparable(IType lhsType, IType rhsType, boolean bBiDirectional)Verifies that the right hand type can be converted or coerced to the left hand type.ITypeverifyTypesComparable(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:
canCoercein 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:
findCoercerin 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:ICoercionManagerDetermine whether the specified type is either a primitive or a boxed primitive.- Specified by:
isPrimitiveOrBoxedin 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:ICoercionManagerVerifies 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:
verifyTypesComparablein interfaceICoercionManager- Throws:
ParseException
-
verifyTypesComparable
public IType verifyTypesComparable(IType lhsType, IType rhsType, boolean bBiDirectional, IFullParserState parserState) throws ParseException
Description copied from interface:ICoercionManagerVerifies 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:
verifyTypesComparablein 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:
notCoercibleOrRequiresExplicitCoercionin 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:
convertValuein 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:ICoercionManagerConverts a null value to a value compatible with the specified primitive type.- Specified by:
convertNullAsPrimitivein 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:ICoercionManagerDetermine and return a statically valid coercer from the rhsType to the lhsType. Returns null if no coercion is necessary.- Specified by:
resolveCoercerStaticallyin interfaceICoercionManager- Parameters:
typeToCoerceTo- the type to coerce totypeToCoerceFrom- the type to coerce from
-
makeDoubleFrom
public Double makeDoubleFrom(Object obj)
Description copied from interface:ICoercionManagerReturns 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:
makeDoubleFromin interfaceICoercionManager- Returns:
- A Double for an arbitrary object (may return a pooled instance).
-
makePrimitiveIntegerFrom
public int makePrimitiveIntegerFrom(Object obj)
- Specified by:
makePrimitiveIntegerFromin interfaceICoercionManager
-
makeIntegerFrom
public Integer makeIntegerFrom(Object obj)
- Specified by:
makeIntegerFromin interfaceICoercionManager
-
makePrimitiveLongFrom
public long makePrimitiveLongFrom(Object obj)
- Specified by:
makePrimitiveLongFromin interfaceICoercionManager
-
makeLongFrom
public Long makeLongFrom(Object obj)
- Specified by:
makeLongFromin interfaceICoercionManager
-
makeBigDecimalFrom
public BigDecimal makeBigDecimalFrom(Object obj)
- Specified by:
makeBigDecimalFromin interfaceICoercionManager
-
makeRationalFrom
public Rational makeRationalFrom(Object obj)
- Specified by:
makeRationalFromin interfaceICoercionManager
-
makeBigIntegerFrom
public BigInteger makeBigIntegerFrom(Object obj)
- Specified by:
makeBigIntegerFromin interfaceICoercionManager
-
makePrimitiveDoubleFrom
public double makePrimitiveDoubleFrom(Object obj)
- Specified by:
makePrimitiveDoubleFromin interfaceICoercionManager
-
makeFloatFrom
public Float makeFloatFrom(Object obj)
- Specified by:
makeFloatFromin interfaceICoercionManager
-
makePrimitiveFloatFrom
public float makePrimitiveFloatFrom(Object obj)
- Specified by:
makePrimitiveFloatFromin interfaceICoercionManager
-
makeStringFrom
public String makeStringFrom(Object obj)
- Specified by:
makeStringFromin interfaceICoercionManager
-
makePrimitiveBooleanFrom
public boolean makePrimitiveBooleanFrom(Object obj)
- Specified by:
makePrimitiveBooleanFromin interfaceICoercionManager- Returns:
- A Boolean for an arbitrary object.
-
makeBooleanFrom
public Boolean makeBooleanFrom(Object obj)
- Specified by:
makeBooleanFromin interfaceICoercionManager
-
makeDateFrom
public Date makeDateFrom(Object obj)
Returns a new Date instance representing the object.- Specified by:
makeDateFromin interfaceICoercionManager
-
isDateTime
public boolean isDateTime(String str) throws ParseException
- Specified by:
isDateTimein interfaceICoercionManager- Throws:
ParseException
-
parseDateTime
public Date parseDateTime(String str) throws ParseException
Produce a date from a string using standard DateFormat parsing.- Specified by:
parseDateTimein interfaceICoercionManager- Throws:
ParseException
-
formatDate
public String formatDate(Date value, String strFormat)
- Specified by:
formatDatein interfaceICoercionManager
-
formatTime
public String formatTime(Date value, String strFormat)
- Specified by:
formatTimein interfaceICoercionManager
-
formatNumber
public String formatNumber(Double value, String strFormat)
- Specified by:
formatNumberin interfaceICoercionManager
-
parseNumber
public Number parseNumber(String strValue)
- Specified by:
parseNumberin interfaceICoercionManager
-
-