Use OptionalDouble
@@ -9,7 +9,7 @@ import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;
-import java.util.Optional;
+import java.util.OptionalDouble;
/**
* @author freva
@@ -22,7 +22,7 @@ public class ClusterCapacity {
private final double vcpu;
private final double memoryGb;
private final double diskGb;
- private final Optional<Double> bandwidthGbps;
+ private final OptionalDouble bandwidthGbps;
@JsonCreator
public ClusterCapacity(@JsonProperty("count") int count,
@@ -34,19 +34,21 @@ public class ClusterCapacity {
this.vcpu = requireNonNegative("vcpu", vcpu);
this.memoryGb = requireNonNegative("memoryGb", memoryGb);
this.diskGb = requireNonNegative("diskGb", diskGb);
- this.bandwidthGbps = Optional.ofNullable(bandwidthGbps);
+ this.bandwidthGbps = bandwidthGbps == null ? OptionalDouble.empty() : OptionalDouble.of(bandwidthGbps);
}
/** Returns a new ClusterCapacity equal to {@code this}, but with the given count. */
public ClusterCapacity withCount(int count) {
- return new ClusterCapacity(count, vcpu, memoryGb, diskGb, bandwidthGbps.orElse(null));
+ return new ClusterCapacity(count, vcpu, memoryGb, diskGb, bandwidthGbpsOrNull());
}
@JsonGetter("count") public int count() { return count; }
@JsonGetter("vcpu") public double vcpu() { return vcpu; }
@JsonGetter("memoryGb") public double memoryGb() { return memoryGb; }
@JsonGetter("diskGb") public double diskGb() { return diskGb; }
- @JsonGetter("bandwidthGbps") public Double bandwidthGbpsOrNull() { return bandwidthGbps.orElse(null); }
+ @JsonGetter("bandwidthGbps") public Double bandwidthGbpsOrNull() {
+ return bandwidthGbps.isPresent() ? bandwidthGbps.getAsDouble() : null;
+ }
@JsonIgnore
public double bandwidthGbps() { return bandwidthGbps.orElse(1.0); }
@@ -71,9 +73,7 @@ public class ClusterCapacity {
Double.compare(that.vcpu, vcpu) == 0 &&
Double.compare(that.memoryGb, memoryGb) == 0 &&
Double.compare(that.diskGb, diskGb) == 0 &&
- ((bandwidthGbps.isEmpty() && that.bandwidthGbps.isEmpty()) ||
- ((bandwidthGbps.isPresent() && that.bandwidthGbps.isPresent() &&
- Double.compare(that.bandwidthGbps.get(), bandwidthGbps.get()) == 0)));
+ bandwidthGbps.equals(that.bandwidthGbps);
}
@Override
