16 package org.openkilda.messaging.payload;
18 import com.google.common.base.MoreObjects;
19 import com.google.common.collect.ImmutableSet;
22 import java.util.concurrent.ConcurrentHashMap;
36 private final Set<Integer> resources = ConcurrentHashMap.newKeySet();
37 private Integer nextId;
38 private Integer lower;
39 private Integer upper;
47 public ResourcePool(
final Integer minValue,
final Integer maxValue) {
48 this.nextId = minValue;
49 this.lower = minValue;
50 this.upper = maxValue;
59 int range = upper - lower;
60 if (resources.size() <= range) {
64 for (
int i = 0;
i < range;
i++) {
70 synchronized (nextId) {
74 if (resources.add(next)) {
79 throw new ArrayIndexOutOfBoundsException(
"Could not allocate resource: pool is full");
94 return resources.add(
id) ?
id : null;
104 return resources.remove(resourceId) ? resourceId : null;
113 return ImmutableSet.copyOf(resources);
121 return MoreObjects.toStringHelper(
this)
122 .add(
"resources", resources)
123 .add(
"nextId", nextId)
Integer deallocate(final Integer resourceId)
ResourcePool(final Integer minValue, final Integer maxValue)
Set< Integer > dumpPool()
Integer allocate(Integer id)