org.apache.commons.collections.Closure Java Examples

The following examples show how to use org.apache.commons.collections.Closure. 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: ChainedClosure.java    From Penetration_Testing_POC with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Closure that calls each closure in turn, passing the 
 * result into the next closure. The ordering is that of the iterator()
 * method on the collection.
 * 
 * @param closures  a collection of closures to chain
 * @return the <code>chained</code> closure
 * @throws IllegalArgumentException if the closures collection is null
 * @throws IllegalArgumentException if any closure in the collection is null
 */
public static Closure getInstance(Collection closures) {
    if (closures == null) {
        throw new IllegalArgumentException("Closure collection must not be null");
    }
    if (closures.size() == 0) {
        return NOPClosure.INSTANCE;
    }
    // convert to array like this to guarantee iterator() ordering
    Closure[] cmds = new Closure[closures.size()];
    int i = 0;
    for (Iterator it = closures.iterator(); it.hasNext();) {
        cmds[i++] = (Closure) it.next();
    }
    FunctorUtils.validate(cmds);
    return new ChainedClosure(cmds);
}
 
Example #2
Source File: JavaSoundAudioSink.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
private void runVolumeCommand(Closure closure) {
    Mixer.Info[] infos = AudioSystem.getMixerInfo();
    for (Mixer.Info info : infos) {
        Mixer mixer = AudioSystem.getMixer(info);
        if (mixer.isLineSupported(Port.Info.SPEAKER)) {
            Port port;
            try {
                port = (Port) mixer.getLine(Port.Info.SPEAKER);
                port.open();
                if (port.isControlSupported(FloatControl.Type.VOLUME)) {
                    FloatControl volume = (FloatControl) port.getControl(FloatControl.Type.VOLUME);
                    closure.execute(volume);
                }
                port.close();
            } catch (LineUnavailableException e) {
                logger.error("Cannot access master volume control", e);
            }
        }
    }
}
 
Example #3
Source File: JavaSoundAudioSink.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void setVolume(final PercentType volume) throws IOException {
    if (volume.intValue() < 0 || volume.intValue() > 100) {
        throw new IllegalArgumentException("Volume value must be in the range [0,100]!");
    }
    if (!isMac) {
        runVolumeCommand(new Closure() {
            @Override
            public void execute(Object input) {
                FloatControl volumeControl = (FloatControl) input;
                volumeControl.setValue(volume.floatValue() / 100f);
            }
        });
    } else {
        Runtime.getRuntime()
                .exec(new String[] { "osascript", "-e", "set volume output volume " + volume.intValue() });
        macVolumeValue = volume;
    }
}
 
Example #4
Source File: IfClosure.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that performs validation.
 * 
 * @param predicate  predicate to switch on
 * @param trueClosure  closure used if true
 * @param falseClosure  closure used if false
 * @return the <code>if</code> closure
 * @throws IllegalArgumentException if any argument is null
 */
public static Closure getInstance(Predicate predicate, Closure trueClosure, Closure falseClosure) {
    if (predicate == null) {
        throw new IllegalArgumentException("Predicate must not be null");
    }
    if (trueClosure == null || falseClosure == null) {
        throw new IllegalArgumentException("Closures must not be null");
    }
    return new IfClosure(predicate, trueClosure, falseClosure);
}
 
Example #5
Source File: ChainedClosure.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that performs validation and copies the parameter array.
 * 
 * @param closures  the closures to chain, copied, no nulls
 * @return the <code>chained</code> closure
 * @throws IllegalArgumentException if the closures array is null
 * @throws IllegalArgumentException if any closure in the array is null
 */
public static Closure getInstance(Closure[] closures) {
    FunctorUtils.validate(closures);
    if (closures.length == 0) {
        return NOPClosure.INSTANCE;
    }
    closures = FunctorUtils.copy(closures);
    return new ChainedClosure(closures);
}
 
Example #6
Source File: FunctorUtils.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Validate the closures to ensure that all is well.
 * 
 * @param closures  the closures to validate
 */
static void validate(Closure[] closures) {
    if (closures == null) {
        throw new IllegalArgumentException("The closure array must not be null");
    }
    for (int i = 0; i < closures.length; i++) {
        if (closures[i] == null) {
            throw new IllegalArgumentException("The closure array must not contain a null closure, index " + i + " was null");
        }
    }
}
 
Example #7
Source File: FunctorUtils.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Clone the closures to ensure that the internal reference can't be messed with.
 * 
 * @param closures  the closures to copy
 * @return the cloned closures
 */
static Closure[] copy(Closure[] closures) {
    if (closures == null) {
        return null;
    }
    return (Closure[]) closures.clone();
}
 
Example #8
Source File: KeyValueSet.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * Walk entries.
 *
 * @param handler handler to call.
 * @param filter  filter to use.
 * @throws IllegalArgumentException if closure or filter is null.
 */
public void walk( final Closure handler, final Predicate filter ) throws IllegalArgumentException {
  Assert.assertNotNull( handler, "IHandler cannot be null" );
  Assert.assertNotNull( filter, "Filter cannot be null" );
  for ( KeyValue<?> keyValue : this.entries.values() ) {
    if ( filter.evaluate( keyValue ) ) {
      handler.execute( keyValue );
    }
  }
}
 
Example #9
Source File: TriggerProcessBean.java    From development with Apache License 2.0 5 votes vote down vote up
public void setSelectAll(boolean selectAll) {
    this.selectAll = selectAll;
    if (this.triggerProcessList != null) {
        final boolean isSelectAll = this.isSelectAll();
        CollectionUtils.forAllDo(this.triggerProcessList, new Closure() {
            @Override
            public void execute(Object tp) {
                ((TriggerProcess) tp).setSelected(isSelectAll);
            }
        });
    }
}
 
Example #10
Source File: WhileClosure.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that performs validation.
 * 
 * @param predicate  the predicate used to evaluate when the loop terminates, not null
 * @param closure  the closure the execute, not null
 * @param doLoop  true to act as a do-while loop, always executing the closure once
 * @return the <code>while</code> closure
 * @throws IllegalArgumentException if the predicate or closure is null
 */
public static Closure getInstance(Predicate predicate, Closure closure, boolean doLoop) {
    if (predicate == null) {
        throw new IllegalArgumentException("Predicate must not be null");
    }
    if (closure == null) {
        throw new IllegalArgumentException("Closure must not be null");
    }
    return new WhileClosure(predicate, closure, doLoop);
}
 
Example #11
Source File: SwitchClosure.java    From Penetration_Testing_POC with Apache License 2.0 5 votes vote down vote up
/**
 * Factory method that performs validation and copies the parameter arrays.
 * 
 * @param predicates  array of predicates, cloned, no nulls
 * @param closures  matching array of closures, cloned, no nulls
 * @param defaultClosure  the closure to use if no match, null means nop
 * @return the <code>chained</code> closure
 * @throws IllegalArgumentException if array is null
 * @throws IllegalArgumentException if any element in the array is null
 */
public static Closure getInstance(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
    FunctorUtils.validate(predicates);
    FunctorUtils.validate(closures);
    if (predicates.length != closures.length) {
        throw new IllegalArgumentException("The predicate and closure arrays must be the same size");
    }
    if (predicates.length == 0) {
        return (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
    }
    predicates = FunctorUtils.copy(predicates);
    closures = FunctorUtils.copy(closures);
    return new SwitchClosure(predicates, closures, defaultClosure);
}
 
Example #12
Source File: PluginPropertyHandler.java    From hop with Apache License 2.0 5 votes vote down vote up
/**
 * @param properties properties.
 * @param handler    handler.
 * @throws HopException             ...
 * @throws IllegalArgumentException if properties is null.
 */
public static void walk( final KeyValueSet properties, final Closure handler ) throws HopException,
  IllegalArgumentException {
  assertProperties( properties );
  try {
    properties.walk( handler );
  } catch ( FunctorException e ) {
    throw (HopException) e.getCause();
  }
}
 
Example #13
Source File: PluginPropertyHandler.java    From pentaho-kettle with Apache License 2.0 3 votes vote down vote up
/**
 * @param properties
 *          properties.
 * @param handler
 *          handler.
 * @throws KettleException
 *           ...
 * @throws IllegalArgumentException
 *           if properties is null.
 */
public static void walk( final KeyValueSet properties, final Closure handler ) throws KettleException,
  IllegalArgumentException {
  assertProperties( properties );
  try {
    properties.walk( handler );
  } catch ( FunctorException e ) {
    throw (KettleException) e.getCause();
  }
}
 
Example #14
Source File: WhileClosure.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor that performs no validation.
 * Use <code>getInstance</code> if you want that.
 * 
 * @param predicate  the predicate used to evaluate when the loop terminates, not null
 * @param closure  the closure the execute, not null
 * @param doLoop  true to act as a do-while loop, always executing the closure once
 */
public WhileClosure(Predicate predicate, Closure closure, boolean doLoop) {
    super();
    iPredicate = predicate;
    iClosure = closure;
    iDoLoop = doLoop;
}
 
Example #15
Source File: ClosureTransformer.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method that performs validation.
 * 
 * @param closure  the closure to call, not null
 * @return the <code>closure</code> transformer
 * @throws IllegalArgumentException if the closure is null
 */
public static Transformer getInstance(Closure closure) {
    if (closure == null) {
        throw new IllegalArgumentException("Closure must not be null");
    }
    return new ClosureTransformer(closure);
}
 
Example #16
Source File: ChainedClosure.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method that performs validation.
 * 
 * @param closure1  the first closure, not null
 * @param closure2  the second closure, not null
 * @return the <code>chained</code> closure
 * @throws IllegalArgumentException if either closure is null
 */
public static Closure getInstance(Closure closure1, Closure closure2) {
    if (closure1 == null || closure2 == null) {
        throw new IllegalArgumentException("Closures must not be null");
    }
    Closure[] closures = new Closure[] { closure1, closure2 };
    return new ChainedClosure(closures);
}
 
Example #17
Source File: SwitchClosure.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor that performs no validation.
 * Use <code>getInstance</code> if you want that.
 * 
 * @param predicates  array of predicates, not cloned, no nulls
 * @param closures  matching array of closures, not cloned, no nulls
 * @param defaultClosure  the closure to use if no match, null means nop
 */
public SwitchClosure(Predicate[] predicates, Closure[] closures, Closure defaultClosure) {
    super();
    iPredicates = predicates;
    iClosures = closures;
    iDefault = (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure);
}
 
Example #18
Source File: TransformerClosure.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method that performs validation.
 * <p>
 * A null transformer will return the <code>NOPClosure</code>.
 * 
 * @param transformer  the transformer to call, null means nop
 * @return the <code>transformer</code> closure
 */
public static Closure getInstance(Transformer transformer) {
    if (transformer == null) {
        return NOPClosure.INSTANCE;
    }
    return new TransformerClosure(transformer);
}
 
Example #19
Source File: IfClosure.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Constructor that performs no validation.
 * Use <code>getInstance</code> if you want that.
 * 
 * @param predicate  predicate to switch on, not null
 * @param trueClosure  closure used if true, not null
 * @param falseClosure  closure used if false, not null
 */
public IfClosure(Predicate predicate, Closure trueClosure, Closure falseClosure) {
    super();
    iPredicate = predicate;
    iTrueClosure = trueClosure;
    iFalseClosure = falseClosure;
}
 
Example #20
Source File: ForClosure.java    From Penetration_Testing_POC with Apache License 2.0 3 votes vote down vote up
/**
 * Factory method that performs validation.
 * <p>
 * A null closure or zero count returns the <code>NOPClosure</code>.
 * A count of one returns the specified closure.
 * 
 * @param count  the number of times to execute the closure
 * @param closure  the closure to execute, not null
 * @return the <code>for</code> closure
 */
public static Closure getInstance(int count, Closure closure) {
    if (count <= 0 || closure == null) {
        return NOPClosure.INSTANCE;
    }
    if (count == 1) {
        return closure;
    }
    return new ForClosure(count, closure);
}
 
Example #21
Source File: KeyValueSet.java    From pentaho-kettle with Apache License 2.0 3 votes vote down vote up
/**
 * Walk entries.
 *
 * @param handler
 *          handler to call.
 * @param filter
 *          filter to use.
 * @throws IllegalArgumentException
 *           if closure or filter is null.
 */
public void walk( final Closure handler, final Predicate filter ) throws IllegalArgumentException {
  Assert.assertNotNull( handler, "Handler cannot be null" );
  Assert.assertNotNull( filter, "Filter cannot be null" );
  for ( KeyValue<?> keyValue : this.entries.values() ) {
    if ( filter.evaluate( keyValue ) ) {
      handler.execute( keyValue );
    }
  }
}
 
Example #22
Source File: ClosureTransformer.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the closure.
 * 
 * @return the closure
 * @since Commons Collections 3.1
 */
public Closure getClosure() {
    return iClosure;
}
 
Example #23
Source File: ClosureTransformer.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that performs no validation.
 * Use <code>getInstance</code> if you want that.
 * 
 * @param closure  the closure to call, not null
 */
public ClosureTransformer(Closure closure) {
    super();
    iClosure = closure;
}
 
Example #24
Source File: KeyValueSet.java    From pentaho-kettle with Apache License 2.0 2 votes vote down vote up
/**
 * Walk entries.
 *
 * @param handler
 *          handler to call.
 * @throws IllegalArgumentException
 *           if handler is null.
 */
public void walk( final Closure handler ) throws IllegalArgumentException {
  this.walk( handler, TruePredicate.INSTANCE );
}
 
Example #25
Source File: ChainedClosure.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor that performs no validation.
 * Use <code>getInstance</code> if you want that.
 * 
 * @param closures  the closures to chain, not copied, no nulls
 */
public ChainedClosure(Closure[] closures) {
    super();
    iClosures = closures;
}
 
Example #26
Source File: SwitchClosure.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the default closure.
 * 
 * @return the default closure
 * @since Commons Collections 3.1
 */
public Closure getDefaultClosure() {
    return iDefault;
}
 
Example #27
Source File: SwitchClosure.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the closures, do not modify the array.
 * 
 * @return the closures
 * @since Commons Collections 3.1
 */
public Closure[] getClosures() {
    return iClosures;
}
 
Example #28
Source File: WhileClosure.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the closure.
 * 
 * @return the closure
 * @since Commons Collections 3.1
 */
public Closure getClosure() {
    return iClosure;
}
 
Example #29
Source File: ChainedClosure.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the closures, do not modify the array.
 * @return the closures
 * @since Commons Collections 3.1
 */
public Closure[] getClosures() {
    return iClosures;
}
 
Example #30
Source File: IfClosure.java    From Penetration_Testing_POC with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the closure called when false.
 * 
 * @return the closure
 * @since Commons Collections 3.1
 */
public Closure getFalseClosure() {
    return iFalseClosure;
}