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
/**
 * 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 Project: groovy   File: NioExtensions.java    License: 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 3
Source Project: groovy   File: Attribute.java    License: 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 4
Source Project: groovy   File: FilteredAttributes.java    License: 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 5
Source Project: groovy   File: NodeChildren.java    License: 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 6
Source Project: groovy   File: NodeChild.java    License: 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 7
Source Project: groovy   File: StackTraceUtils.java    License: 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 8
Source Project: groovy   File: ResourceGroovyMethods.java    License: 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 Project: groovy-cps   File: DoWhileBlock.java    License: 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 10
Source Project: groovy-cps   File: ElvisBlock.java    License: 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 11
Source Project: groovy-cps   File: WhileBlock.java    License: 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 12
Source Project: groovy-cps   File: LogicalOpBlock.java    License: 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 13
Source Project: groovy-cps   File: ForLoopBlock.java    License: 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 14
Source Project: groovy   File: FilteredNodeChildren.java    License: 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}));
}
 
Example 15
Source Project: groovy   File: ScriptBytecodeAdapter.java    License: 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 16
Source Project: groovy   File: InvokerTest.java    License: 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 17
Source Project: groovy-cps   File: AssertBlock.java    License: 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);
}