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.runtime.common.driver.resourcemanager;
020
021import org.apache.reef.proto.ReefServiceProtos;
022import org.apache.reef.util.BuilderUtils;
023import org.apache.reef.util.Optional;
024
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * Default POJO implementation of RuntimeStatusEvent.
030 * Use newBuilder to construct an instance.
031 */
032public final class RuntimeStatusEventImpl implements RuntimeStatusEvent {
033  private final String name;
034  private final ReefServiceProtos.State state;
035  private final List<String> containerAllocationList;
036  private final Optional<ReefServiceProtos.RuntimeErrorProto> error;
037  private final Optional<Integer> outstandingContainerRequests;
038
039  private RuntimeStatusEventImpl(final Builder builder) {
040    this.name = BuilderUtils.notNull(builder.name);
041    this.state = BuilderUtils.notNull(builder.state);
042    this.containerAllocationList = BuilderUtils.notNull(builder.containerAllocationList);
043    this.error = Optional.ofNullable(builder.error);
044    this.outstandingContainerRequests = Optional.ofNullable(builder.outstandingContainerRequests);
045  }
046
047  @Override
048  public String getName() {
049    return name;
050  }
051
052  @Override
053  public ReefServiceProtos.State getState() {
054    return state;
055  }
056
057  @Override
058  public List<String> getContainerAllocationList() {
059    return containerAllocationList;
060  }
061
062  @Override
063  public Optional<ReefServiceProtos.RuntimeErrorProto> getError() {
064    return error;
065  }
066
067  @Override
068  public Optional<Integer> getOutstandingContainerRequests() {
069    return outstandingContainerRequests;
070  }
071
072  public static Builder newBuilder() {
073    return new Builder();
074  }
075
076  /**
077   * Builder used to create RuntimeStatusEvent instances.
078   */
079  public static final class Builder implements org.apache.reef.util.Builder<RuntimeStatusEvent> {
080    private String name;
081    private ReefServiceProtos.State state;
082    private List<String> containerAllocationList = new ArrayList<>();
083    private ReefServiceProtos.RuntimeErrorProto error;
084    private Integer outstandingContainerRequests;
085
086    /**
087     * @see RuntimeStatusEvent#getName()
088     */
089    public Builder setName(final String name) {
090      this.name = name;
091      return this;
092    }
093
094    /**
095     * @see RuntimeStatusEvent#getState()
096     */
097    public Builder setState(final ReefServiceProtos.State state) {
098      this.state = state;
099      return this;
100    }
101
102    /**
103     * Add an entry to containerAllocationList
104     * @see RuntimeStatusEvent#getContainerAllocationList()
105     */
106    public Builder addContainerAllocation(final String containerAllocation) {
107      this.containerAllocationList.add(containerAllocation);
108      return this;
109    }
110
111    /**
112     * @see RuntimeStatusEvent#getError()
113     */
114    public Builder setError(final ReefServiceProtos.RuntimeErrorProto error) {
115      this.error = error;
116      return this;
117    }
118
119    /**
120     * @see RuntimeStatusEvent#getOutstandingContainerRequests()
121     */
122    public Builder setOutstandingContainerRequests(final int outstandingContainerRequests) {
123      this.outstandingContainerRequests = outstandingContainerRequests;
124      return this;
125    }
126
127    @Override
128    public RuntimeStatusEvent build() {
129      return new RuntimeStatusEventImpl(this);
130    }
131  }
132}