diff options
author | pschonbac | 2009-02-22 17:42:19 +0000 |
---|---|---|
committer | pschonbac | 2009-02-22 17:42:19 +0000 |
commit | 168ee03ffefced4beafe2fb6d6a6bd05d137e63f (patch) | |
tree | 22a52fa5034e93302015d7b76000e50d479a699b /plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types | |
parent | f1a73033d1c2a4d26bce888923aa36caebe4232d (diff) | |
download | org.eclipse.xpand-168ee03ffefced4beafe2fb6d6a6bd05d137e63f.tar.gz org.eclipse.xpand-168ee03ffefced4beafe2fb6d6a6bd05d137e63f.tar.xz org.eclipse.xpand-168ee03ffefced4beafe2fb6d6a6bd05d137e63f.zip |
Ensured correct regression
Diffstat (limited to 'plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types')
2 files changed, 355 insertions, 363 deletions
diff --git a/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/IntegerTypeImpl.java b/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/IntegerTypeImpl.java index 2e2d8f53..538b590a 100644 --- a/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/IntegerTypeImpl.java +++ b/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/IntegerTypeImpl.java @@ -34,12 +34,12 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { } public boolean isInstance(final Object o) { - return o instanceof Integer || o instanceof BigInteger || o instanceof Byte || o instanceof Long + return o instanceof BigInteger || o instanceof Integer || o instanceof Byte || o instanceof Long || o instanceof Short; } public Object newInstance() { - return new Long(-1); + return new BigInteger("-1"); } @Override @@ -50,7 +50,7 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { public Object evaluateInternal(final Object target, final Object[] params) { if (params[0] == null) return null; - return new Long(((Number) target).longValue() + ((Number) params[0]).longValue()); + return toInt(target).add(toInt(params[0])); } }, new OperationImpl(this, "-", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) { @@ -58,13 +58,13 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { public Object evaluateInternal(final Object target, final Object[] params) { if (params[0] == null) return null; - return new Long(((Number) target).longValue() - ((Number) params[0]).longValue()); + return toInt(target).subtract(toInt(params[0])); } }, new OperationImpl(this, "-", IntegerTypeImpl.this, new Type[] {}) { @Override public Object evaluateInternal(final Object target, final Object[] params) { - return new Long(((Number) target).longValue() * -1l); + return toInt(target).negate(); } }, new OperationImpl(this, "*", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) { @@ -73,7 +73,7 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { if (params[0] == null) return null; - return new Long(((Number) target).longValue() * ((Number) params[0]).longValue()); + return toInt(target).multiply(toInt(params[0])); } }, new OperationImpl(this, "/", IntegerTypeImpl.this, new Type[] { IntegerTypeImpl.this }) { @@ -82,48 +82,38 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { if (params[0] == null) return null; - return new Long(((Number) target).longValue() / ((Number) params[0]).longValue()); + return toInt(target).divide(toInt(params[0])); } }, - new OperationImpl(this, "==", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getObjectType() }) { + new OperationImpl(this, "==", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) { @Override public Object evaluateInternal(final Object target, final Object[] params) { if (target == null) return new Boolean(target == params[0]); - try { - return toLong(target).equals(toLong(params[0])); + return toInt(target).equals(toInt(params[0])); } catch (Exception exc) { - if (target instanceof Number && params[0] instanceof Number) - return ((Number) target).doubleValue() == ((Number) params[0]).doubleValue(); - return false; } } }, - new OperationImpl(this, "!=", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getObjectType() }) { + new OperationImpl(this, "!=", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) { @Override public Object evaluateInternal(final Object target, final Object[] params) { if (target == null) return params[0] != null; try { - return ! toLong(target).equals(toLong(params[0])); + return ! toInt(target).equals(toInt(params[0])); } catch (Exception exc) { - if (target instanceof Number && params[0] instanceof Number) - return ((Number) target).doubleValue() != ((Number) params[0]).doubleValue(); - return true; } } }, - new OperationImpl(this, ">", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getObjectType() }) { + new OperationImpl(this, ">", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) { @Override public Object evaluateInternal(final Object target, final Object[] params) { if (target == null) @@ -132,15 +122,14 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { return Boolean.FALSE; try { - return ((Comparable<Long>) toLong(target)).compareTo(toLong(params[0])) > 0; + return toInt(target).compareTo(toInt(params[0])) > 0; } catch (Exception exc) { - return ((Number) target).doubleValue() > ((Number) params[0]).doubleValue(); + return Boolean.FALSE; } } }, - new OperationImpl(this, ">=", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getObjectType() }) { + new OperationImpl(this, ">=", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) { @Override public Object evaluateInternal(final Object target, final Object[] params) { if (target == null) @@ -149,15 +138,14 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { return Boolean.FALSE; try { - return ((Comparable<Long>) toLong(target)).compareTo(toLong(params[0])) >= 0; + return toInt(target).compareTo(toInt(params[0])) >= 0; } catch (Exception exc) { - return ((Number) target).doubleValue() >= ((Number) params[0]).doubleValue(); + return Boolean.FALSE; } } }, - new OperationImpl(this, "<", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getObjectType() }) { + new OperationImpl(this, "<", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) { @Override public Object evaluateInternal(final Object target, final Object[] params) { if (target == null) @@ -166,15 +154,14 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { return Boolean.FALSE; try { - return ((Comparable<Long>) toLong(target)).compareTo(toLong(params[0])) < 0; + return toInt(target).compareTo(toInt(params[0])) < 0; } catch (Exception exc) { - return ((Number) target).doubleValue() < ((Number) params[0]).doubleValue(); + return Boolean.FALSE; } } }, - new OperationImpl(this, "<=", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getObjectType() }) { + new OperationImpl(this, "<=", getTypeSystem().getBooleanType(), new Type[] { IntegerTypeImpl.this }) { @Override public Object evaluateInternal(final Object target, final Object[] params) { if (target == null) @@ -183,30 +170,30 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { return Boolean.FALSE; try { - return ((Comparable<Long>) toLong(target)).compareTo(toLong(params[0])) <= 0; + return toInt(target).compareTo(toInt(params[0])) <= 0; } catch (Exception exc) { - return ((Number) target).doubleValue() <= ((Number) params[0]).doubleValue(); + return Boolean.FALSE; } } }, new OperationImpl(this, "upTo", getTypeSystem().getListType(this), new Type[] { this }) { @Override public String getDocumentation() { - return "returns a List of Integers starting with the value of the target expression, up to" + return "returns a List of Integers starting with the value of the target expression, up to " + "the value of the specified Integer, incremented by one.<br/>" + "e.g. '1.upTo(5)' evaluates to {1,2,3,4,5}"; } @Override public Object evaluateInternal(final Object target, final Object[] params) { - final List<Long> result = new ArrayList<Long>(); - long l1 = toLong(target).longValue(); - final long l2 = toLong(params[0]).longValue(); + final List<BigInteger> result = new ArrayList<BigInteger>(); + BigInteger l1 = toInt(target); + final BigInteger l2 = toInt(params[0]); - while (l1 <= l2) { - result.add(new Long(l1)); - l1++; + while (l1.compareTo(l2) <= 0) { + result.add(l1); + l1 = l1.add(BigInteger.ONE); } return result; } @@ -214,21 +201,21 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { @Override public String getDocumentation() { - return "returns a List of Integers starting with the value of the target expression, up to" + return "returns a List of Integers starting with the value of the target expression, up to " + "the value of the first paramter, incremented by the second parameter.<br/>" + "e.g. '1.upTo(10, 2)' evaluates to {1,3,5,7,9}"; } @Override public Object evaluateInternal(final Object target, final Object[] params) { - final List<Long> result = new ArrayList<Long>(); - long l1 = toLong(target).longValue(); - final long l2 = toLong(params[0]).longValue(); - final long l3 = toLong(params[1]).longValue(); + final List<BigInteger> result = new ArrayList<BigInteger>(); + BigInteger l1 = toInt(target); + final BigInteger l2 = toInt(params[0]); + final BigInteger l3 = toInt(params[1]); - while (l1 <= l2) { - result.add(new Long(l1)); - l1 = l1 + l3; + while (l1.compareTo(l2) <= 0) { + result.add(l1); + l1 = l1.add(l3); } return result; } @@ -240,36 +227,39 @@ public final class IntegerTypeImpl extends BuiltinBaseType implements Type { return Collections.singleton(getTypeSystem().getRealType()); } - Long toLong(final Object o) { - if (o == null) + protected BigInteger toInt(final Object o) { + if(o == null) return null; + if (o instanceof BigInteger) + return (BigInteger) o; + if (o instanceof Integer) - return new Long(((Integer) o).longValue()); - else if (o instanceof BigInteger) - return new Long(((BigInteger) o).longValue()); + return BigInteger.valueOf(((Integer)o).longValue()); else if (o instanceof Byte) - return new Long(((Byte) o).longValue()); + return BigInteger.valueOf(((Byte)o).longValue()); else if (o instanceof Long) - return (Long) o; + return BigInteger.valueOf((Long)o); else if (o instanceof Short) - return new Long(((Short) o).longValue()); + return BigInteger.valueOf(((Short) o).longValue()); + throw new IllegalArgumentException(o.getClass().getName() + " not supported"); } @Override public Object convert(final Object src, final Class<?> targetType) { - final Long l = toLong(src); - if (targetType.isAssignableFrom(Integer.class) || targetType.isAssignableFrom(Integer.TYPE)) - return new Integer(l.intValue()); - else if (targetType.isAssignableFrom(BigInteger.class)) - return BigInteger.valueOf(l.longValue()); - else if (targetType.isAssignableFrom(Byte.class) || targetType.isAssignableFrom(Byte.TYPE)) - return new Byte(l.byteValue()); + final BigInteger value = toInt(src); + + if (targetType.isAssignableFrom(BigInteger.class)) + return value; else if (targetType.isAssignableFrom(Long.class) || targetType.isAssignableFrom(Long.TYPE)) - return src; + return value.longValue(); + else if (targetType.isAssignableFrom(Integer.class) || targetType.isAssignableFrom(Integer.TYPE)) + return value.intValue(); + else if (targetType.isAssignableFrom(Byte.class) || targetType.isAssignableFrom(Byte.TYPE)) + return value.byteValue(); else if (targetType.isAssignableFrom(Short.class) || targetType.isAssignableFrom(Short.TYPE)) - return new Short(l.shortValue()); + return value.shortValue(); return super.convert(src, targetType); } diff --git a/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/StringTypeImpl.java b/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/StringTypeImpl.java index 1f6e9f08..46191613 100644 --- a/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/StringTypeImpl.java +++ b/plugins/org.eclipse.xtend/src/org/eclipse/internal/xtend/type/baseimpl/types/StringTypeImpl.java @@ -33,300 +33,302 @@ import org.eclipse.xtend.typesystem.Type; */ @SuppressWarnings("unchecked") public class StringTypeImpl extends BuiltinBaseType implements Type { - final Log log = LogFactory.getLog(getClass()); - - public StringTypeImpl(final TypeSystem ts, final String name) { - super(ts, name); - } - - public boolean isInstance(final Object o) { - return o instanceof String || o instanceof StringBuffer || o instanceof Character; - } - - public Object newInstance() { - return ""; - } - - @Override - public Feature[] getContributedFeatures() { - return new Feature[] { - new PropertyImpl(this, "length", getTypeSystem().getIntegerType()) { - - @Override - public String getDocumentation() { - return "the length of this string"; - } - - public Object get(final Object target) { - return new Long(target.toString().length()); - } - }, - - new OperationImpl(this, "+", getTypeSystem().getStringType(), new Type[] { getTypeSystem() - .getObjectType() }) { - - @Override - public String getDocumentation() { - return "concatenates two strings"; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return target.toString() + params[0]; - } - }, - - new OperationImpl(this, "startsWith", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getStringType() }) { - - @Override - public String getDocumentation() { - return "Tests if this string starts with the specified prefix."; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - final String token = (String) params[0]; - return new Boolean(target.toString().startsWith(token)); - } - }, - new OperationImpl(this, "contains", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getStringType() }) { - - @Override - public String getDocumentation() { - return "Tests if this string contains substring."; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - final String token = (String) params[0]; - return new Boolean(target.toString().indexOf(token) >= 0); - } - }, - new OperationImpl(this, "endsWith", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getStringType() }) { - - @Override - public String getDocumentation() { - return "Tests if this string ends with the specified prefix."; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - final String token = (String) params[0]; - return new Boolean(target.toString().endsWith(token)); - } - }, - new OperationImpl(this, "subString", getTypeSystem().getStringType(), new Type[] { getTypeSystem() - .getIntegerType(),getTypeSystem().getIntegerType() }) { - - @Override - public String getDocumentation() { - return "Tests if this string ends with the specified prefix."; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - final Number from = (Number) params[0]; - final Number to = (Number) params[1]; - return target.toString().substring(from.intValue(),to.intValue()); - } - }, - - new OperationImpl(this, "toUpperCase", getTypeSystem().getStringType(), new Type[] {}) { - - @Override - public String getDocumentation() { - return "Converts all of the characters in this String to upper" - + " case using the rules of the default locale (from Java)"; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return target.toString().toUpperCase(); - } - }, - - new OperationImpl(this, "toLowerCase", getTypeSystem().getStringType(), new Type[] {}) { - @Override - public String getDocumentation() { - return "Converts all of the characters in this String to lower" - + " case using the rules of the default locale (from Java)"; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return target.toString().toLowerCase(); - } - }, - - new OperationImpl(this, "toFirstUpper", getTypeSystem().getStringType(), new Type[] {}) { - @Override - public String getDocumentation() { - return "Converts the first character in this String to upper" - + " case using the rules of the default locale (from Java)"; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return StringHelper.firstUpper(target.toString()); - } - }, - - new OperationImpl(this, "toFirstLower", getTypeSystem().getStringType(), new Type[] {}) { - @Override - public String getDocumentation() { - return "Converts the first character in this String to lower" - + " case using the rules of the default locale (from Java)"; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return StringHelper.firstLower(target.toString()); - } - }, - - new OperationImpl(this, "toCharList", getTypeSystem().getListType(getTypeSystem().getStringType()), - new Type[] {}) { - - @Override - public String getDocumentation() { - return "splits this String into a List[String] containing Strings of length 1"; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - final String txt = target.toString(); - final List<String> result = new ArrayList<String>(); - final char[] chars = txt.toCharArray(); - for (int i = 0; i < chars.length; i++) { - result.add(String.valueOf(chars[i])); - } - return result; - } - }, - - new OperationImpl(this, "replaceAll", getTypeSystem().getStringType(), new Type[] { - getTypeSystem().getStringType(), getTypeSystem().getStringType() }) { - - @Override - public String getDocumentation() { - return "Replaces each substring of this string that matches the given " - + "regular expression with the given replacement."; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return target.toString().replaceAll(params[0].toString(), params[1].toString()); - } - }, - - new OperationImpl(this, "replaceFirst", getTypeSystem().getStringType(), new Type[] { - getTypeSystem().getStringType(), getTypeSystem().getStringType() }) { - - @Override - public String getDocumentation() { - return "Replaces the first substring of this string that matches the given" - + " regular expression with the given replacement."; - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return target.toString().replaceFirst(params[0].toString(), params[1].toString()); - } - }, - - new OperationImpl(this, "split", getTypeSystem().getListType(getTypeSystem().getStringType()), - new Type[] { getTypeSystem().getStringType() }) { - - @Override - public String getDocumentation() { - return "Splits this string around matches of the given regular expression (from Java 1.4)"; - - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return new ArrayList<String>(Arrays.asList(target.toString().split(params[0].toString()))); - } - }, - - new OperationImpl(this, "matches", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() - .getStringType() }) { - - @Override - public String getDocumentation() { - return "Tells whether or not this string matches the given regular expression. (from Java 1.4)"; - - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return Boolean.valueOf(((String) target).matches((String) params[0])); - } - }, - - new OperationImpl(this, "trim", getTypeSystem().getStringType(), new Type[] {}) { - - @Override - public String getDocumentation() { - return "Returns a copy of the string, with leading and trailing whitespace omitted. (from Java 1.4)"; - - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - return ((String) target).trim(); - } - }, - - new OperationImpl(this, "asInteger", getTypeSystem().getIntegerType(), new Type[] {}) { - - @Override - public String getDocumentation() { - return "Returns an Integer object holding the value of the specified String (from Java 1.5)"; - - } - - @Override - public Object evaluateInternal(final Object target, final Object[] params) { - try { - return Integer.valueOf((String) target); - } catch (NumberFormatException nfe) { - log.error("'asInteger' on '"+target+"' returned null!"); - return null; - } - } - } - - }; - } - - @Override - public Set<Type> getSuperTypes() { - return Collections.singleton(getTypeSystem().getObjectType()); - } - - private String toString(final Object o) { - if (o == null) - return null; - if (isInstance(o)) - return o.toString(); - throw new IllegalArgumentException(o.getClass().getName() + " not supported"); - } - - @Override - public Object convert(final Object src, final Class targetType) { - final String s = toString(src); - if (targetType.isAssignableFrom(String.class)) - return s; - else if (targetType.isAssignableFrom(Character.class) || targetType.isAssignableFrom(Character.TYPE)) { - if (s.length() == 1) - return new Character(s.charAt(0)); - } else if (targetType.isAssignableFrom(StringBuffer.class)) - return new StringBuffer(s); - return super.convert(src, targetType); - } + final Log log = LogFactory.getLog(getClass()); + + public StringTypeImpl(final TypeSystem ts, final String name) { + super(ts, name); + } + + public boolean isInstance(final Object o) { + return o instanceof String || o instanceof StringBuffer || o instanceof Character; + } + + public Object newInstance() { + return ""; + } + + @Override + public Feature[] getContributedFeatures() { + return new Feature[] { + new PropertyImpl(this, "length", getTypeSystem().getIntegerType()) { + + @Override + public String getDocumentation() { + return "the length of this string"; + } + + public Object get(final Object target) { + return new Long(target.toString().length()); + } + }, + + new OperationImpl(this, "+", getTypeSystem().getStringType(), new Type[] { getTypeSystem() + .getObjectType() }) { + + @Override + public String getDocumentation() { + return "concatenates two strings"; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return target.toString() + params[0]; + } + }, + + new OperationImpl(this, "startsWith", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() + .getStringType() }) { + + @Override + public String getDocumentation() { + return "Tests if this string starts with the specified prefix."; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + final String token = (String) params[0]; + return new Boolean(target.toString().startsWith(token)); + } + }, + new OperationImpl(this, "contains", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() + .getStringType() }) { + + @Override + public String getDocumentation() { + return "Tests if this string contains substring."; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + final String token = (String) params[0]; + return new Boolean(target.toString().indexOf(token) >= 0); + } + }, + new OperationImpl(this, "endsWith", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() + .getStringType() }) { + + @Override + public String getDocumentation() { + return "Tests if this string ends with the specified prefix."; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + final String token = (String) params[0]; + return new Boolean(target.toString().endsWith(token)); + } + }, + new OperationImpl(this, "subString", getTypeSystem().getStringType(), new Type[] { + getTypeSystem().getIntegerType(), getTypeSystem().getIntegerType() }) { + + @Override + public String getDocumentation() { + return "Returns a new string that is a substring of this string."; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + final Number from = (Number) params[0]; + final Number to = (Number) params[1]; + return target.toString().substring(from.intValue(), to.intValue()); + } + }, + + new OperationImpl(this, "toUpperCase", getTypeSystem().getStringType(), new Type[] {}) { + + @Override + public String getDocumentation() { + return "Converts all of the characters in this String to upper" + + " case using the rules of the default locale (from Java)"; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return target.toString().toUpperCase(); + } + }, + + new OperationImpl(this, "toLowerCase", getTypeSystem().getStringType(), new Type[] {}) { + @Override + public String getDocumentation() { + return "Converts all of the characters in this String to lower" + + " case using the rules of the default locale (from Java)"; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return target.toString().toLowerCase(); + } + }, + + new OperationImpl(this, "toFirstUpper", getTypeSystem().getStringType(), new Type[] {}) { + @Override + public String getDocumentation() { + return "Converts the first character in this String to upper" + + " case using the rules of the default locale (from Java)"; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return StringHelper.firstUpper(target.toString()); + } + }, + + new OperationImpl(this, "toFirstLower", getTypeSystem().getStringType(), new Type[] {}) { + @Override + public String getDocumentation() { + return "Converts the first character in this String to lower" + + " case using the rules of the default locale (from Java)"; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return StringHelper.firstLower(target.toString()); + } + }, + + new OperationImpl(this, "toCharList", getTypeSystem().getListType(getTypeSystem().getStringType()), + new Type[] {}) { + + @Override + public String getDocumentation() { + return "splits this String into a List[String] containing Strings of length 1"; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + final String txt = target.toString(); + final List<String> result = new ArrayList<String>(); + final char[] chars = txt.toCharArray(); + for (int i = 0; i < chars.length; i++) { + result.add(String.valueOf(chars[i])); + } + return result; + } + }, + + new OperationImpl(this, "replaceAll", getTypeSystem().getStringType(), new Type[] { + getTypeSystem().getStringType(), getTypeSystem().getStringType() }) { + + @Override + public String getDocumentation() { + return "Replaces each substring of this string that matches the given " + + "regular expression with the given replacement."; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return target.toString().replaceAll(params[0].toString(), params[1].toString()); + } + }, + + new OperationImpl(this, "replaceFirst", getTypeSystem().getStringType(), new Type[] { + getTypeSystem().getStringType(), getTypeSystem().getStringType() }) { + + @Override + public String getDocumentation() { + return "Replaces the first substring of this string that matches the given" + + " regular expression with the given replacement."; + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return target.toString().replaceFirst(params[0].toString(), params[1].toString()); + } + }, + + new OperationImpl(this, "split", getTypeSystem().getListType(getTypeSystem().getStringType()), + new Type[] { getTypeSystem().getStringType() }) { + + @Override + public String getDocumentation() { + return "Splits this string around matches of the given regular expression (from Java 1.4)"; + + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return new ArrayList<String>(Arrays.asList(target.toString().split(params[0].toString()))); + } + }, + + new OperationImpl(this, "matches", getTypeSystem().getBooleanType(), new Type[] { getTypeSystem() + .getStringType() }) { + + @Override + public String getDocumentation() { + return "Tells whether or not this string matches the given regular expression. (from Java 1.4)"; + + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return Boolean.valueOf(((String) target).matches((String) params[0])); + } + }, + + new OperationImpl(this, "trim", getTypeSystem().getStringType(), new Type[] {}) { + + @Override + public String getDocumentation() { + return "Returns a copy of the string, with leading and trailing whitespace omitted. (from Java 1.4)"; + + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + return ((String) target).trim(); + } + }, + + new OperationImpl(this, "asInteger", getTypeSystem().getIntegerType(), new Type[] {}) { + + @Override + public String getDocumentation() { + return "Returns an Integer object holding the value of the specified String (from Java 1.5)"; + + } + + @Override + public Object evaluateInternal(final Object target, final Object[] params) { + try { + return new BigInteger((String) target); + } + catch (NumberFormatException nfe) { + log.error("'asInteger' on '" + target + "' returned null!"); + return null; + } + } + } + + }; + } + + @Override + public Set<Type> getSuperTypes() { + return Collections.singleton(getTypeSystem().getObjectType()); + } + + private String toString(final Object o) { + if (o == null) + return null; + if (isInstance(o)) + return o.toString(); + throw new IllegalArgumentException(o.getClass().getName() + " not supported"); + } + + @Override + public Object convert(final Object src, final Class targetType) { + final String s = toString(src); + if (targetType.isAssignableFrom(String.class)) + return s; + else if (targetType.isAssignableFrom(Character.class) || targetType.isAssignableFrom(Character.TYPE)) { + if (s.length() == 1) + return new Character(s.charAt(0)); + } + else if (targetType.isAssignableFrom(StringBuffer.class)) + return new StringBuffer(s); + return super.convert(src, targetType); + } } |