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.api;
020
021import org.apache.reef.util.BuilderUtils;
022import org.apache.reef.util.Optional;
023
024import java.util.ArrayList;
025import java.util.List;
026
027/**
028 * Default POJO implementation of ResourceRequestEvent.
029 * Use newBuilder to construct an instance.
030 */
031public final class ResourceRequestEventImpl implements ResourceRequestEvent {
032  private final int resourceCount;
033  private final List<String> nodeNameList;
034  private final List<String> rackNameList;
035  private final Optional<Integer> memorySize;
036  private final Optional<Integer> priority;
037  private final Optional<Integer> virtualCores;
038  private final Optional<Boolean> relaxLocality;
039
040  private ResourceRequestEventImpl(final Builder builder) {
041    this.resourceCount = BuilderUtils.notNull(builder.resourceCount);
042    this.nodeNameList = BuilderUtils.notNull(builder.nodeNameList);
043    this.rackNameList = BuilderUtils.notNull(builder.rackNameList);
044    this.memorySize = Optional.ofNullable(builder.memorySize);
045    this.priority = Optional.ofNullable(builder.priority);
046    this.virtualCores = Optional.ofNullable(builder.virtualCores);
047    this.relaxLocality = Optional.ofNullable(builder.relaxLocality);
048  }
049
050  @Override
051  public int getResourceCount() {
052    return resourceCount;
053  }
054
055  @Override
056  public List<String> getNodeNameList() {
057    return nodeNameList;
058  }
059
060  @Override
061  public List<String> getRackNameList() {
062    return rackNameList;
063  }
064
065  @Override
066  public Optional<Integer> getMemorySize() {
067    return memorySize;
068  }
069
070  @Override
071  public Optional<Integer> getPriority() {
072    return priority;
073  }
074
075  @Override
076  public Optional<Integer> getVirtualCores() {
077    return virtualCores;
078  }
079
080  @Override
081  public Optional<Boolean> getRelaxLocality() {
082    return relaxLocality;
083  }
084
085  public static Builder newBuilder() {
086    return new Builder();
087  }
088
089  /**
090   * Builder used to create ResourceRequestEvent instances.
091   */
092  public static final class Builder implements org.apache.reef.util.Builder<ResourceRequestEvent> {
093    private Integer resourceCount;
094    private List<String> nodeNameList = new ArrayList<>();
095    private List<String> rackNameList = new ArrayList<>();
096    private Integer memorySize;
097    private Integer priority;
098    private Integer virtualCores;
099    private Boolean relaxLocality;
100
101    /**
102     * Create a builder from an existing ResourceRequestEvent
103     */
104    public Builder mergeFrom(final ResourceRequestEvent resourceRequestEvent) {
105      this.resourceCount = resourceRequestEvent.getResourceCount();
106      this.nodeNameList = resourceRequestEvent.getNodeNameList();
107      this.rackNameList = resourceRequestEvent.getRackNameList();
108      this.memorySize = resourceRequestEvent.getMemorySize().orElse(null);
109      this.priority = resourceRequestEvent.getPriority().orElse(null);
110      this.virtualCores = resourceRequestEvent.getVirtualCores().orElse(null);
111      this.relaxLocality = resourceRequestEvent.getRelaxLocality().orElse(null);
112      return this;
113    }
114
115    /**
116     * @see ResourceRequestEvent#getResourceCount()
117     */
118    public Builder setResourceCount(final int resourceCount) {
119      this.resourceCount = resourceCount;
120      return this;
121    }
122
123    /**
124     * Add an entry to the nodeNameList
125     * @see ResourceRequestEvent#getNodeNameList()
126     */
127    public Builder addNodeName(final String nodeName) {
128      this.nodeNameList.add(nodeName);
129      return this;
130    }
131
132    /**
133     * Add an entry to rackNameList
134     * @see ResourceRequestEvent#getRackNameList()
135     */
136    public Builder addRackName(final String rackName) {
137      this.rackNameList.add(rackName);
138      return this;
139    }
140
141    /**
142     * @see ResourceRequestEvent#getMemorySize()
143     */
144    public Builder setMemorySize(final int memorySize) {
145      this.memorySize = memorySize;
146      return this;
147    }
148
149    /**
150     * @see ResourceRequestEvent#getPriority()
151     */
152    public Builder setPriority(final int priority) {
153      this.priority = priority;
154      return this;
155    }
156
157    /**
158     * @see ResourceRequestEvent#getVirtualCores()
159     */
160    public Builder setVirtualCores(final int virtualCores) {
161      this.virtualCores = virtualCores;
162      return this;
163    }
164
165    /**
166     * @see ResourceRequestEvent#getRelaxLocality()
167     */
168    public Builder setRelaxLocality(final boolean relaxLocality) {
169      this.relaxLocality = relaxLocality;
170      return this;
171    }
172
173    @Override
174    public ResourceRequestEvent build() {
175      return new ResourceRequestEventImpl(this);
176    }
177  }
178}