Search in sources :

Example 1 with SimpleNumber

use of freemarker.template.SimpleNumber in project android by JetBrains.

the class FmCompareVersionsMethod method exec.

@Override
public TemplateModel exec(List args) throws TemplateModelException {
    if (args.size() != 2) {
        throw new TemplateModelException("Wrong arguments");
    }
    GradleVersion lhs = GradleVersion.parse(args.get(0).toString());
    GradleVersion rhs = GradleVersion.parse(args.get(1).toString());
    return new SimpleNumber(lhs.compareTo(rhs));
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleNumber(freemarker.template.SimpleNumber) GradleVersion(com.android.ide.common.repository.GradleVersion)

Example 2 with SimpleNumber

use of freemarker.template.SimpleNumber in project leopard by tanhaichao.

the class AbstractTemplateMethod method exec.

@Override
public Object exec(@SuppressWarnings("rawtypes") List arguments) throws TemplateModelException {
    // arguments.toArray();
    Object[] args = new Object[arguments.size()];
    int i = 0;
    for (Object obj : arguments) {
        if (obj != null) {
            if (obj instanceof SimpleDate) {
                args[i] = ((SimpleDate) obj).getAsDate();
            } else if (obj instanceof SimpleNumber) {
                args[i] = ((SimpleNumber) obj).getAsNumber();
            } else if (obj instanceof SimpleNumber) {
                args[i] = ((SimpleNumber) obj).getAsNumber();
            } else if (obj instanceof SimpleScalar) {
                args[i] = ((SimpleScalar) obj).toString();
            } else if (obj instanceof StringModel) {
                args[i] = ((StringModel) obj);
            } else {
                logger.error("obj:" + obj.getClass());
                args[i] = obj.toString();
            }
        }
        i++;
    }
    try {
        return this.execute(args);
    } catch (TemplateModelException e) {
        throw e;
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new TemplateModelException(e.getMessage(), e);
    }
}
Also used : TemplateModelException(freemarker.template.TemplateModelException) SimpleDate(freemarker.template.SimpleDate) SimpleNumber(freemarker.template.SimpleNumber) StringModel(freemarker.ext.beans.StringModel) SimpleScalar(freemarker.template.SimpleScalar) TemplateModelException(freemarker.template.TemplateModelException)

Example 3 with SimpleNumber

use of freemarker.template.SimpleNumber in project android by JetBrains.

the class FmCompareVersionsMethodTest method check.

@SuppressWarnings("rawtypes")
private void check(String lhs, String rhs, int expected) throws Exception {
    FmCompareVersionsMethod method = new FmCompareVersionsMethod();
    List list = newArrayList(new SimpleScalar(lhs), new SimpleScalar(rhs));
    Number result = ((SimpleNumber) method.exec(list)).getAsNumber();
    assertEquals(expected, Integer.signum(result.intValue()));
}
Also used : SimpleNumber(freemarker.template.SimpleNumber) SimpleNumber(freemarker.template.SimpleNumber) List(java.util.List) ContainerUtil.newArrayList(com.intellij.util.containers.ContainerUtil.newArrayList) SimpleScalar(freemarker.template.SimpleScalar)

Example 4 with SimpleNumber

use of freemarker.template.SimpleNumber in project freemarker by apache.

the class UnaryPlusMinusExpression method _eval.

@Override
TemplateModel _eval(Environment env) throws TemplateException {
    TemplateNumberModel targetModel = null;
    TemplateModel tm = target.eval(env);
    try {
        targetModel = (TemplateNumberModel) tm;
    } catch (ClassCastException cce) {
        throw new NonNumericalException(target, tm, env);
    }
    if (!isMinus) {
        return targetModel;
    }
    target.assertNonNull(targetModel, env);
    Number n = targetModel.getAsNumber();
    n = ArithmeticEngine.CONSERVATIVE_ENGINE.multiply(MINUS_ONE, n);
    return new SimpleNumber(n);
}
Also used : SimpleNumber(freemarker.template.SimpleNumber) SimpleNumber(freemarker.template.SimpleNumber) TemplateNumberModel(freemarker.template.TemplateNumberModel) TemplateModel(freemarker.template.TemplateModel)

Example 5 with SimpleNumber

use of freemarker.template.SimpleNumber in project freemarker by apache.

the class ListableRightUnboundedRangeModel method iterator.

public TemplateModelIterator iterator() throws TemplateModelException {
    return new TemplateModelIterator() {

        boolean needInc;

        int nextType = 1;

        int nextInt = getBegining();

        long nextLong;

        BigInteger nextBigInteger;

        public TemplateModel next() throws TemplateModelException {
            if (needInc) {
                switch(nextType) {
                    case 1:
                        if (nextInt < Integer.MAX_VALUE) {
                            nextInt++;
                        } else {
                            nextType = 2;
                            nextLong = nextInt + 1L;
                        }
                        break;
                    case 2:
                        if (nextLong < Long.MAX_VALUE) {
                            nextLong++;
                        } else {
                            nextType = 3;
                            nextBigInteger = BigInteger.valueOf(nextLong);
                            nextBigInteger = nextBigInteger.add(BigInteger.ONE);
                        }
                        break;
                    default:
                        // 3
                        nextBigInteger = nextBigInteger.add(BigInteger.ONE);
                }
            }
            needInc = true;
            return nextType == 1 ? new SimpleNumber(nextInt) : (nextType == 2 ? new SimpleNumber(nextLong) : new SimpleNumber(nextBigInteger));
        }

        public boolean hasNext() throws TemplateModelException {
            return true;
        }
    };
}
Also used : TemplateModelIterator(freemarker.template.TemplateModelIterator) SimpleNumber(freemarker.template.SimpleNumber) BigInteger(java.math.BigInteger)

Aggregations

SimpleNumber (freemarker.template.SimpleNumber)13 SimpleScalar (freemarker.template.SimpleScalar)7 TemplateModelException (freemarker.template.TemplateModelException)7 List (java.util.List)4 StringModel (freemarker.ext.beans.StringModel)2 SimpleDate (freemarker.template.SimpleDate)2 TemplateModel (freemarker.template.TemplateModel)2 TemplateNumberModel (freemarker.template.TemplateNumberModel)2 BigInteger (java.math.BigInteger)2 ArrayList (java.util.ArrayList)2 GradleVersion (com.android.ide.common.repository.GradleVersion)1 ContainerUtil.newArrayList (com.intellij.util.containers.ContainerUtil.newArrayList)1 XPath (com.sun.org.apache.xpath.internal.XPath)1 XBoolean (com.sun.org.apache.xpath.internal.objects.XBoolean)1 XNodeSet (com.sun.org.apache.xpath.internal.objects.XNodeSet)1 XNull (com.sun.org.apache.xpath.internal.objects.XNull)1 XNumber (com.sun.org.apache.xpath.internal.objects.XNumber)1 XObject (com.sun.org.apache.xpath.internal.objects.XObject)1 XString (com.sun.org.apache.xpath.internal.objects.XString)1 FileTemplateLoader (freemarker.cache.FileTemplateLoader)1