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 private final String runtimeName; 040 041 private ResourceRequestEventImpl(final Builder builder) { 042 this.resourceCount = BuilderUtils.notNull(builder.resourceCount); 043 this.nodeNameList = BuilderUtils.notNull(builder.nodeNameList); 044 this.rackNameList = BuilderUtils.notNull(builder.rackNameList); 045 this.memorySize = Optional.ofNullable(builder.memorySize); 046 this.priority = Optional.ofNullable(builder.priority); 047 this.virtualCores = Optional.ofNullable(builder.virtualCores); 048 this.relaxLocality = Optional.ofNullable(builder.relaxLocality); 049 this.runtimeName = builder.runtimeName == null ? "" : builder.runtimeName; 050 } 051 052 @Override 053 public int getResourceCount() { 054 return resourceCount; 055 } 056 057 @Override 058 public List<String> getNodeNameList() { 059 return nodeNameList; 060 } 061 062 @Override 063 public List<String> getRackNameList() { 064 return rackNameList; 065 } 066 067 @Override 068 public Optional<Integer> getMemorySize() { 069 return memorySize; 070 } 071 072 @Override 073 public Optional<Integer> getPriority() { 074 return priority; 075 } 076 077 @Override 078 public Optional<Integer> getVirtualCores() { 079 return virtualCores; 080 } 081 082 @Override 083 public Optional<Boolean> getRelaxLocality() { 084 return relaxLocality; 085 } 086 087 @Override 088 public String getRuntimeName() { 089 return runtimeName; 090 } 091 092 public static Builder newBuilder() { 093 return new Builder(); 094 } 095 096 /** 097 * Builder used to create ResourceRequestEvent instances. 098 */ 099 public static final class Builder implements org.apache.reef.util.Builder<ResourceRequestEvent> { 100 private Integer resourceCount; 101 private List<String> nodeNameList = new ArrayList<>(); 102 private List<String> rackNameList = new ArrayList<>(); 103 private Integer memorySize; 104 private Integer priority; 105 private Integer virtualCores; 106 private Boolean relaxLocality; 107 private String runtimeName; 108 109 /** 110 * Create a builder from an existing ResourceRequestEvent. 111 */ 112 public Builder mergeFrom(final ResourceRequestEvent resourceRequestEvent) { 113 this.resourceCount = resourceRequestEvent.getResourceCount(); 114 this.nodeNameList = resourceRequestEvent.getNodeNameList(); 115 this.rackNameList = resourceRequestEvent.getRackNameList(); 116 this.memorySize = resourceRequestEvent.getMemorySize().orElse(null); 117 this.priority = resourceRequestEvent.getPriority().orElse(null); 118 this.virtualCores = resourceRequestEvent.getVirtualCores().orElse(null); 119 this.relaxLocality = resourceRequestEvent.getRelaxLocality().orElse(null); 120 this.runtimeName = resourceRequestEvent.getRuntimeName(); 121 return this; 122 } 123 124 /** 125 * @see ResourceRequestEvent#getResourceCount() 126 */ 127 public Builder setResourceCount(final int resourceCount) { 128 this.resourceCount = resourceCount; 129 return this; 130 } 131 132 /** 133 * Add an entry to the nodeNameList. 134 * @see ResourceRequestEvent#getNodeNameList() 135 */ 136 public Builder addNodeName(final String nodeName) { 137 this.nodeNameList.add(nodeName); 138 return this; 139 } 140 141 /** 142 * Add a list of node names. 143 * @see ResourceRequestEventImpl.Builder#addNodeName 144 */ 145 public Builder addNodeNames(final List<String> nodeNames) { 146 for (final String nodeName : nodeNames) { 147 addNodeName(nodeName); 148 } 149 return this; 150 } 151 152 /** 153 * Add a list of rack names. 154 * @see ResourceRequestEventImpl.Builder#addRackName 155 */ 156 public Builder addRackName(final String rackName) { 157 this.rackNameList.add(rackName); 158 return this; 159 } 160 161 /** 162 * Add an entry to rackNameList. 163 * @see ResourceRequestEvent#getRackNameList 164 */ 165 public Builder addRackNames(final List<String> rackNames) { 166 for (final String rackName : rackNames) { 167 addRackName(rackName); 168 } 169 return this; 170 } 171 172 /** 173 * @see ResourceRequestEvent#getMemorySize 174 */ 175 public Builder setMemorySize(final int memorySize) { 176 this.memorySize = memorySize; 177 return this; 178 } 179 180 /** 181 * @see ResourceRequestEvent#getPriority 182 */ 183 public Builder setPriority(final int priority) { 184 this.priority = priority; 185 return this; 186 } 187 188 /** 189 * @see ResourceRequestEvent#getVirtualCores 190 */ 191 public Builder setVirtualCores(final int virtualCores) { 192 this.virtualCores = virtualCores; 193 return this; 194 } 195 196 /** 197 * @see ResourceRequestEvent#getRelaxLocality 198 */ 199 public Builder setRelaxLocality(final boolean relaxLocality) { 200 this.relaxLocality = relaxLocality; 201 return this; 202 } 203 204 /** 205 * @see ResourceRequestEvent#getRuntimeName 206 */ 207 public Builder setRuntimeName(final String runtimeName) { 208 this.runtimeName = runtimeName; 209 return this; 210 } 211 212 @Override 213 public ResourceRequestEvent build() { 214 return new ResourceRequestEventImpl(this); 215 } 216 } 217}