001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.vortex.api;
020
021import org.apache.reef.annotations.Unstable;
022import org.apache.reef.annotations.audience.ClientSide;
023import org.apache.reef.annotations.audience.Public;
024import org.apache.reef.util.Optional;
025
026import java.util.Collections;
027import java.util.List;
028
029/**
030 * The result of an aggregate. Registered to callbacks for {@link VortexAggregateFuture}.
031 */
032@Public
033@ClientSide
034@Unstable
035public final class AggregateResult<TInput, TOutput> {
036
037  private final Optional<TOutput> aggregatedOutput;
038  private final List<TInput> inputList;
039  private final Optional<Exception> exception;
040
041  AggregateResult(final Exception exception,
042                  final List<TInput> inputList) {
043    this(Optional.<TOutput>empty(), Optional.of(exception), inputList);
044  }
045
046  AggregateResult(final TOutput aggregatedOutput,
047                  final List<TInput> inputList) {
048    this(Optional.of(aggregatedOutput), Optional.<Exception>empty(), inputList);
049  }
050
051  private AggregateResult(final Optional<TOutput> aggregatedOutput,
052                          final Optional<Exception> exception,
053                          final List<TInput> inputList) {
054    this.aggregatedOutput = aggregatedOutput;
055    this.inputList = Collections.unmodifiableList(inputList);
056    this.exception = exception;
057  }
058
059  /**
060   * @return the output of an aggregation, throws the Exception if a Tasklet or an aggregation fails.
061   * If an aggregation fails, {@link VortexAggregateException} will be thrown, otherwise
062   * the Exception that caused the Tasklet to fail will be thrown directly.
063   * @throws Exception the Exception that caused the Tasklet or aggregation failure.
064   */
065  public TOutput getAggregateResult() throws VortexAggregateException {
066    if (exception.isPresent()) {
067      throw new VortexAggregateException(exception.get(), inputList);
068    }
069
070    return aggregatedOutput.get();
071  }
072
073  /**
074   * @return the associated inputs of an aggregation
075   */
076  public List<TInput> getAggregatedInputs() {
077    return inputList;
078  }
079
080  /**
081   * If an aggregation fails, {@link VortexAggregateException} will be thrown, otherwise
082   * the Exception that caused the Tasklet to fail will be thrown directly.
083   * @return the Exception that caused the Tasklet or aggregation failure, if any.
084   */
085  public Optional<Exception> getException() {
086    return exception;
087  }
088}