Class Convertible
- java.lang.Object
-
- groovy.lang.GroovyObjectSupport
-
- org.freeplane.plugin.script.proxy.Convertible
-
- All Implemented Interfaces:
groovy.lang.GroovyObject,Convertible
public class Convertible extends groovy.lang.GroovyObjectSupport implements Convertible
Utility wrapper class around a String that is used to convert node texts to different types. It's especially important for Formulas.
-
-
Constructor Summary
Constructors Constructor Description Convertible(java.lang.Object object)UsetoString(Object)to convert to String, i.e.Convertible(java.lang.String text)Use thetextunchanged, i.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanasBoolean()For implicit conversion to boolean: true if the text is not empty.intcompareTo(java.lang.Object string)intcompareTo(Convertible convertible)booleanequals(java.lang.Object obj)booleangetBool()parses the text (case insensitive) as boolean viaBoolean.parseBoolean(String).java.util.CalendargetCalendar()Converts to Calendar if possible.java.util.DategetDate()Converts to Date if possible.java.lang.NumbergetNum()Convert to Number.java.lang.NumbergetNum0()Safe variant ofgetNum(), throws nothing - on any error (long) 0 is returned.java.lang.ObjectgetObject()Uses the following priority ranking to determine the type of the text: null Long Double Date Stringjava.lang.StringgetPlain()Removes HTML markup if necessary.java.lang.ObjectgetProperty(java.lang.String property)pretend we are a String if we don't provide a property for ourselves.java.lang.ObjectgetRaw()Returns original objectjava.lang.StringgetString()No conversion.java.lang.StringgetText()No conversion.ConvertiblegetTo()Allow statements like this:node['attr_name'].to.num.java.net.URIgetUri()Converts to URI if possible.inthashCode()since equals handles Strings special we have to stick to that here too since equal objects have to have the same hasCode.java.lang.ObjectinvokeMethod(java.lang.String name, java.lang.Object args)pretend we are a String if we don't provide a method for ourselves.booleanisDate()Type check.booleanisNum()Type check.voidsetProperty(java.lang.String property, java.lang.Object newValue)java.lang.StringtoString()static java.lang.StringtoString(java.lang.Object value)has special conversions for Date and Calendar are converted by org.apache.commons.lang.time.DateFormatUtils.format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"), i.e.
-
-
-
Constructor Detail
-
Convertible
public Convertible(java.lang.String text)
Use thetextunchanged, i. e. oesn't evaluate formulas since this would require a calculation rule or NodeModel.- Parameters:
text- the String to convert.
-
Convertible
public Convertible(java.lang.Object object)
UsetoString(Object)to convert to String, i.e. conversion is done properly.- Parameters:
object- the Object to convert
-
-
Method Detail
-
getNum
public java.lang.Number getNum() throws org.freeplane.plugin.script.proxy.ConversionExceptionConvert to Number. All Java number literals are allowed as described byLong.decode(String)- Specified by:
getNumin interfaceConvertible- Returns:
- a Long or a Double, whatever fits best.
- Throws:
org.freeplane.plugin.script.proxy.ConversionException- if text is not a number.
-
getNum0
public java.lang.Number getNum0()
Safe variant ofgetNum(), throws nothing - on any error (long) 0 is returned.- Specified by:
getNum0in interfaceConvertible- Returns:
- a Long or a Double if text is convertible to it or 0 otherwise (even if text is null).
-
getString
public java.lang.String getString()
No conversion.- Specified by:
getStringin interfaceConvertible- Returns:
- The original string.
-
getText
public java.lang.String getText()
No conversion.- Specified by:
getTextin interfaceConvertible- Returns:
- The original string.
-
getPlain
public java.lang.String getPlain()
Removes HTML markup if necessary.- Specified by:
getPlainin interfaceConvertible- Returns:
- The result of
HtmlUtils.htmlToPlain(String)
-
getDate
public java.util.Date getDate() throws org.freeplane.plugin.script.proxy.ConversionExceptionConverts to Date if possible. The valid date patterns are "yyyy-MM-dd HH:dd:ss.SSSZ" with optional '-', ':'. ' ' may be replaced by 'T'.- Specified by:
getDatein interfaceConvertible- Returns:
- a Date for the parsed text
- Throws:
org.freeplane.plugin.script.proxy.ConversionException- if the text is not convertible to a Date.
-
getCalendar
public java.util.Calendar getCalendar() throws org.freeplane.plugin.script.proxy.ConversionExceptionConverts to Calendar if possible. SeegetDate()for recognized patterns.- Specified by:
getCalendarin interfaceConvertible- Returns:
- a Calendar for the parsed text.
- Throws:
org.freeplane.plugin.script.proxy.ConversionException- if the text is not convertible to a Date.
-
getUri
public java.net.URI getUri() throws org.freeplane.plugin.script.proxy.ConversionExceptionConverts to URI if possible.- Specified by:
getUriin interfaceConvertible- Returns:
- a URI
- Throws:
org.freeplane.plugin.script.proxy.ConversionException- if the text is not convertible to a URI.
-
getObject
public java.lang.Object getObject()
Uses the following priority ranking to determine the type of the text:- null
- Long
- Double
- Date
- String
- Specified by:
getObjectin interfaceConvertible- Returns:
- Object - the type that fits best.
-
getTo
public Convertible getTo()
Allow statements like this:node['attr_name'].to.num.- Returns:
- this
-
isNum
public boolean isNum()
Type check.- Specified by:
isNumin interfaceConvertible- Returns:
- true if the text is convertible to number.
-
isDate
public boolean isDate()
Type check.- Specified by:
isDatein interfaceConvertible- Returns:
- true if the text is convertible to date.
-
getProperty
public java.lang.Object getProperty(java.lang.String property)
pretend we are a String if we don't provide a property for ourselves.- Specified by:
getPropertyin interfacegroovy.lang.GroovyObject- Overrides:
getPropertyin classgroovy.lang.GroovyObjectSupport- Parameters:
property- a property ofString, e. g. "bytes".- Returns:
- the property of the original string.
-
invokeMethod
public java.lang.Object invokeMethod(java.lang.String name, java.lang.Object args)pretend we are a String if we don't provide a method for ourselves.- Specified by:
invokeMethodin interfacegroovy.lang.GroovyObject- Overrides:
invokeMethodin classgroovy.lang.GroovyObjectSupport- Parameters:
name- method nameargs- method args- Returns:
- the result of the method invocation on the original string.
-
toString
public static java.lang.String toString(java.lang.Object value)
has special conversions for- Date and Calendar are converted by org.apache.commons.lang.time.DateFormatUtils.format(date, "yyyy-MM-dd'T'HH:mm:ss.SSSZ"), i.e. to GMT timestamps, e.g.: "2010-08-16T22:31:55.123+0000".
- null is "converted" to null
- Parameters:
value- the object to convert- Returns:
- the converted string
-
compareTo
public int compareTo(java.lang.Object string)
- Specified by:
compareToin interfaceConvertible
-
compareTo
public int compareTo(Convertible convertible)
- Specified by:
compareToin interfaceConvertible
-
hashCode
public int hashCode()
since equals handles Strings special we have to stick to that here too since equal objects have to have the same hasCode.- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
setProperty
public void setProperty(java.lang.String property, java.lang.Object newValue)- Specified by:
setPropertyin interfacegroovy.lang.GroovyObject- Overrides:
setPropertyin classgroovy.lang.GroovyObjectSupport
-
getBool
public boolean getBool()
parses the text (case insensitive) as boolean viaBoolean.parseBoolean(String).- Specified by:
getBoolin interfaceConvertible- Returns:
- boolean
-
asBoolean
public boolean asBoolean()
For implicit conversion to boolean: true if the text is not empty.- Specified by:
asBooleanin interfaceConvertible- Returns:
- boolean
-
getRaw
public java.lang.Object getRaw()
Description copied from interface:ConvertibleReturns original object- Specified by:
getRawin interfaceConvertible- Returns:
- the original object
-
-