org.jdeferred.multiple.MasterDeferredObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jdeferred-core Show documentation
Show all versions of jdeferred-core Show documentation
Java Deferred/Promise library similar to JQuery.
/*
* Copyright 2013 Ray Tsang
*
* 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 org.jdeferred.multiple;
import java.util.concurrent.atomic.AtomicInteger;
import org.jdeferred.AlwaysCallback;
import org.jdeferred.DoneCallback;
import org.jdeferred.FailCallback;
import org.jdeferred.ProgressCallback;
import org.jdeferred.Promise;
import org.jdeferred.impl.DeferredObject;
/**
* This will return a special Promise called {@link MasterDeferredObject}. In short,
*
* - {@link Promise#done(DoneCallback)} will be triggered if all promises resolves
* (i.e., all finished successfully) with {@link MultipleResults}.
* - {@link Promise#fail(FailCallback)} will be
* triggered if any promises rejects (i.e., if any one failed) with {@link OneReject}.
* - {@link Promise#progress(ProgressCallback)} will be triggered whenever one
* promise resolves or rejects ({#link {@link MasterProgress}),
* or whenever a promise was notified progress ({@link OneProgress}).
* - {@link Promise#always(AlwaysCallback)} will be triggered whenever
* {@link Promise#done(DoneCallback)} or {@link Promise#fail(FailCallback)}
* would be triggered
*
*
* @author Ray Tsang
*
*/
@SuppressWarnings("rawtypes")
public class MasterDeferredObject extends
DeferredObject
implements Promise {
private final int numberOfPromises;
private final AtomicInteger doneCount = new AtomicInteger();
private final AtomicInteger failCount = new AtomicInteger();
private final MultipleResults results;
@SuppressWarnings("unchecked")
public MasterDeferredObject(Promise... promises) {
if (promises == null || promises.length == 0)
throw new IllegalArgumentException("Promises is null or empty");
this.numberOfPromises = promises.length;
results = new MultipleResults(numberOfPromises);
int count = 0;
for (final Promise promise : promises) {
final int index = count++;
promise.fail(new FailCallback