public static void main(String[] args) {
    Closure discount =
        new Closure() {
          int count = 0;

          public int getCount() {
            return count;
          }

          public void execute(Object input) {
            count++;
            Product product = (Product) input;
            product.setPrice(product.getPrice() * 0.9);
            System.out.println(count);
          }
        };

    Product shoes = new Product();
    shoes.setName("Fancy Shoes");
    shoes.setPrice(120.00);
    System.out.println("Shoes before discount:" + shoes.getPrice());

    discount.execute(shoes);
    System.out.println("Shoes after discount:" + shoes.getPrice());

    discount.execute(shoes);
    discount.execute(shoes);
    System.out.println("after discount:" + shoes.getPrice());
  }
Beispiel #2
0
 /**
  * Executes the true or false closure accoring to the result of the predicate.
  *
  * @param input the input object
  */
 public void execute(Object input) {
   if (iPredicate.evaluate(input) == true) {
     iTrueClosure.execute(input);
   } else {
     iFalseClosure.execute(input);
   }
 }
  public void notifyOf(Producer<? extends Iteration<ValuePointListTelemetryColored>> producer) {
    Iteration<ValuePointListTelemetryColored> i = producer.getValue();

    if (i != null && i.getValue() != null) {
      datasetRecreation.execute(i.getValue().getValue());
    }
  }
 /**
  * Utility method to perform action for all pending CrawlURI instances.
  *
  * @param c Closure action to perform
  * @throws DatabaseException
  */
 protected void forAllPendingDo(Closure c) throws DatabaseException {
   DatabaseEntry key = new DatabaseEntry();
   DatabaseEntry value = new DatabaseEntry();
   Cursor cursor = pendingUrisDB.openCursor(null, null);
   while (cursor.getNext(key, value, null) == OperationStatus.SUCCESS) {
     if (value.getData().length == 0) {
       continue;
     }
     CrawlURI item = (CrawlURI) crawlUriBinding.entryToObject(value);
     c.execute(item);
   }
   cursor.close();
 }
Beispiel #5
0
 private static 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);
       }
     }
   }
 }
 /**
  * Transforms the input to result by executing a closure.
  *
  * @param input the input object to transform
  * @return the transformed result
  */
 public Object transform(Object input) {
   iClosure.execute(input);
   return input;
 }
 void notifyListeners(Commontags commontag) {
   for (Closure listener : listeners) {
     listener.execute(commontag);
   }
 }
 @Override
 public void dispose() {
   actionBlock.execute(handover);
   super.dispose();
 }