Java Code Examples for org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation#castToBoolean()

The following examples show how to use org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation#castToBoolean() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultGroovyStaticMethods.java    From groovy with Apache License 2.0 6 votes vote down vote up
/**
 * This method is used by both sleep() methods to implement sleeping
 * for the given time even if interrupted
 *
 * @param millis  the number of milliseconds to sleep
 * @param closure optional closure called when interrupted
 *                as long as the closure returns false the sleep continues
 */
private static void sleepImpl(long millis, Closure closure) {
    long start = System.currentTimeMillis();
    long rest = millis;
    long current;
    while (rest > 0) {
        try {
            Thread.sleep(rest);
            rest = 0;
        } catch (InterruptedException e) {
            if (closure != null) {
                if (DefaultTypeTransformation.castToBoolean(closure.call(e))) {
                    return;
                }
            }
            current = System.currentTimeMillis(); // compensate for closure's time
            rest = millis + start - current;
        }
    }
}
 
Example 2
Source File: ForLoopBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next loopCond(Object cond) {
    if (DefaultTypeTransformation.castToBoolean(cond)) {
        // loop
        return then(body,e,increment);
    } else {
        // exit loop
        return loopEnd.receive(null);
    }
}
 
Example 3
Source File: NioExtensions.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean notFiltered(Path path, Object filter, Object nameFilter, Object excludeFilter, Object excludeNameFilter) {
    if (filter == null && nameFilter == null && excludeFilter == null && excludeNameFilter == null) return true;
    if (filter != null && nameFilter != null)
        throw new IllegalArgumentException("Can't set both 'filter' and 'nameFilter'");
    if (excludeFilter != null && excludeNameFilter != null)
        throw new IllegalArgumentException("Can't set both 'excludeFilter' and 'excludeNameFilter'");
    Object filterToUse = null;
    Object filterParam = null;
    if (filter != null) {
        filterToUse = filter;
        filterParam = path;
    } else if (nameFilter != null) {
        filterToUse = nameFilter;
        filterParam = path.getFileName().toString();
    }
    Object excludeFilterToUse = null;
    Object excludeParam = null;
    if (excludeFilter != null) {
        excludeFilterToUse = excludeFilter;
        excludeParam = path;
    } else if (excludeNameFilter != null) {
        excludeFilterToUse = excludeNameFilter;
        excludeParam = path.getFileName().toString();
    }
    final MetaClass filterMC = filterToUse == null ? null : InvokerHelper.getMetaClass(filterToUse);
    final MetaClass excludeMC = excludeFilterToUse == null ? null : InvokerHelper.getMetaClass(excludeFilterToUse);
    boolean included = filterToUse == null || DefaultTypeTransformation.castToBoolean(filterMC.invokeMethod(filterToUse, "isCase", filterParam));
    boolean excluded = excludeFilterToUse != null && DefaultTypeTransformation.castToBoolean(excludeMC.invokeMethod(excludeFilterToUse, "isCase", excludeParam));
    return included && !excluded;
}
 
Example 4
Source File: LogicalOpBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next decide(Object lhs) {
    boolean v = DefaultTypeTransformation.castToBoolean(lhs);
    if (and) {
        if (!v)     return k.receive(false);    // false && ...
        else        return then(rhs,e,k);
    } else {
        if (v)      return k.receive(true);    // true || ...
        else        return then(rhs,e,k);
    }
}
 
Example 5
Source File: WhileBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next loopCond(Object cond) {
    if (DefaultTypeTransformation.castToBoolean(cond)) {
        // loop
        return then(body,e,loopHead);
    } else {
        // exit loop
        return loopEnd.receive(null);
    }
}
 
Example 6
Source File: ElvisBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next jump(Object cond) {
    if (DefaultTypeTransformation.castToBoolean(cond)) {
        return k.receive(cond);
    } else {
        return then(falseExp, e, k);
    }
}
 
Example 7
Source File: DoWhileBlock.java    From groovy-cps with Apache License 2.0 5 votes vote down vote up
public Next loopCond(Object cond) {
    if (DefaultTypeTransformation.castToBoolean(cond)) {
        // loop
        return top();
    } else {
        // exit loop
        return loopEnd.receive(null);
    }
}
 
Example 8
Source File: ResourceGroovyMethods.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static boolean notFiltered(File file, Object filter, Object nameFilter, Object excludeFilter, Object excludeNameFilter) {
    if (filter == null && nameFilter == null && excludeFilter == null && excludeNameFilter == null) return true;
    if (filter != null && nameFilter != null)
        throw new IllegalArgumentException("Can't set both 'filter' and 'nameFilter'");
    if (excludeFilter != null && excludeNameFilter != null)
        throw new IllegalArgumentException("Can't set both 'excludeFilter' and 'excludeNameFilter'");
    Object filterToUse = null;
    Object filterParam = null;
    if (filter != null) {
        filterToUse = filter;
        filterParam = file;
    } else if (nameFilter != null) {
        filterToUse = nameFilter;
        filterParam = file.getName();
    }
    Object excludeFilterToUse = null;
    Object excludeParam = null;
    if (excludeFilter != null) {
        excludeFilterToUse = excludeFilter;
        excludeParam = file;
    } else if (excludeNameFilter != null) {
        excludeFilterToUse = excludeNameFilter;
        excludeParam = file.getName();
    }
    final MetaClass filterMC = filterToUse == null ? null : InvokerHelper.getMetaClass(filterToUse);
    final MetaClass excludeMC = excludeFilterToUse == null ? null : InvokerHelper.getMetaClass(excludeFilterToUse);
    boolean included = filterToUse == null || DefaultTypeTransformation.castToBoolean(filterMC.invokeMethod(filterToUse, "isCase", filterParam));
    boolean excluded = excludeFilterToUse != null && DefaultTypeTransformation.castToBoolean(excludeMC.invokeMethod(excludeFilterToUse, "isCase", excludeParam));
    return included && !excluded;
}
 
Example 9
Source File: StackTraceUtils.java    From groovy with Apache License 2.0 5 votes vote down vote up
public static boolean isApplicationClass(String className) {
    for (Closure test : tests) {
        Object result = test.call(className);
        if (result != null) {
            return DefaultTypeTransformation.castToBoolean(result);
        }
    }

    for (String groovyPackage : GROOVY_PACKAGES) {
        if (className.startsWith(groovyPackage)) {
            return false;
        }
    }
    return true;
}
 
Example 10
Source File: NodeChild.java    From groovy with Apache License 2.0 5 votes vote down vote up
public GPathResult find(final Closure closure) {
    if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{this.node}))) {
        return this;
    } else {
        return new NoChildren(this, "", this.namespaceTagHints);
    }
}
 
Example 11
Source File: NodeChildren.java    From groovy with Apache License 2.0 5 votes vote down vote up
public GPathResult find(final Closure closure) {
    for (Object node : this) {
        if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{node}))) {
            return (GPathResult) node;
        }
    }
    return new NoChildren(this, this.name, namespaceTagHints);
}
 
Example 12
Source File: FilteredAttributes.java    From groovy with Apache License 2.0 5 votes vote down vote up
public Iterator nodeIterator() {
    return new NodeIterator(this.parent.iterator())
    {
        protected Object getNextNode(final Iterator iter) {
            while (iter.hasNext()) {
                final Object node = iter.next();
                if (DefaultTypeTransformation.castToBoolean(FilteredAttributes.this.closure.call(new Object[]{node}))) {
                    return node;
                }
            }
            return null;
        }
    };
}
 
Example 13
Source File: Attribute.java    From groovy with Apache License 2.0 5 votes vote down vote up
public GPathResult find(final Closure closure) {
    if (DefaultTypeTransformation.castToBoolean(closure.call(new Object[]{this}))) {
        return this;
    } else {
        return new NoChildren(this, "", this.namespaceTagHints);
    }
}
 
Example 14
Source File: ScriptBytecodeAdapter.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static boolean isCase(Object switchValue, Object caseExpression) throws Throwable {
    if (caseExpression == null) {
        return switchValue == null;
    }
    return DefaultTypeTransformation.castToBoolean(invokeMethodN(caseExpression.getClass(), caseExpression, "isCase", new Object[]{switchValue}));
}
 
Example 15
Source File: InvokerTest.java    From groovy with Apache License 2.0 4 votes vote down vote up
/**
 * Asserts the asBoolean method returns the given flag
 */
protected void assertAsBoolean(boolean expected, Object value) {
    boolean answer = DefaultTypeTransformation.castToBoolean(value);
    assertEquals("value: " + value + " asBoolean()", expected, answer);
}
 
Example 16
Source File: AssertBlock.java    From groovy-cps with Apache License 2.0 4 votes vote down vote up
public Next jump(Object cond) {
    if (DefaultTypeTransformation.castToBoolean(cond))
        return k.receive(null);
    else
        return then(msg, e, fail);
}
 
Example 17
Source File: FilteredNodeChildren.java    From groovy with Apache License 2.0 4 votes vote down vote up
private boolean closureYieldsTrueForNode(Object childNode) {
    return DefaultTypeTransformation.castToBoolean(FilteredNodeChildren.this.closure.call(new Object[]{childNode}));
}