com.arpnetworking.utility.CollectFutureBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metrics-cluster-aggregator Show documentation
Show all versions of metrics-cluster-aggregator Show documentation
(Re)Aggregates host level statistics across clusters and writes both host and cluster statistics to various destinations.
/*
* Copyright 2014 Groupon.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arpnetworking.utility;
import akka.dispatch.Futures;
import akka.dispatch.OnComplete;
import com.google.common.collect.Lists;
import scala.Function0;
import scala.concurrent.ExecutionContext;
import scala.concurrent.Future;
import scala.concurrent.Promise;
import scala.runtime.AbstractFunction0;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Collects futures and provides them in a combined promise.
*
* @param Return future type
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
*/
public final class CollectFutureBuilder {
/**
* Method to create a {@code } from the completed {@link scala.concurrent.Future}s.
*
* @param callback Callback function
* @return this builder
*/
public CollectFutureBuilder map(final Function0 callback) {
_callback = callback;
return this;
}
/**
* Registers a {@link scala.concurrent.Future} in the collection. A future must be registered in order
* to be waited on.
*
* @param future Future to register
* @return this builder
*/
public CollectFutureBuilder addFuture(final Future> future) {
_futures.add(future);
return this;
}
/**
* Sets the list of {@link scala.concurrent.Future}s to wait on.
*
* @param futures The list of futures
* @return this builder
*/
public CollectFutureBuilder setFutures(final List> futures) {
_futures.clear();
_futures.addAll(futures);
return this;
}
/**
* Builds the final future.
*
* @param context context to execute the futures on
* @return the new future
*/
@SuppressWarnings("unchecked")
public Future build(final ExecutionContext context) {
final Promise result = Futures.promise();
final AtomicInteger latch = new AtomicInteger(_futures.size());
final OnComplete
© 2015 - 2025 Weber Informatics LLC | Privacy Policy