nwangtw commented on a change in pull request #3142: Validate resource 
constraint (RAM and CPU) in RoundRobinPacking
URL: https://github.com/apache/incubator-heron/pull/3142#discussion_r245047409
 
 

 ##########
 File path: heron/common/src/java/org/apache/heron/common/basics/CPUShare.java
 ##########
 @@ -0,0 +1,125 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.heron.common.basics;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public final class CPUShare implements ResourceMeasure<CPUShare> {
+  private final Double cpuShare;
+
+  private CPUShare(Double cpuShare) {
+    this.cpuShare = cpuShare;
+  }
+
+  public static CPUShare fromDouble(double share) {
+    return new CPUShare(share);
+  }
+
+  public Double getCpuShare() {
+    return cpuShare;
+  }
+
+  @Override
+  public boolean isZero() {
+    return cpuShare == 0.0;
+  }
+
+  @Override
+  public CPUShare minus(CPUShare other) {
+    return new CPUShare(cpuShare - other.cpuShare);
+  }
+
+  @Override
+  public CPUShare plus(CPUShare other) {
+    return new CPUShare(cpuShare + other.cpuShare);
+  }
+
+  @Override
+  public CPUShare multiply(int factor) {
+    return new CPUShare(cpuShare * factor);
+  }
+
+  @Override
+  public CPUShare divide(int factor) {
+    return new CPUShare(cpuShare / factor);
+  }
+
+  @Override
+  public CPUShare increaseBy(int percentage) {
+    return new CPUShare(cpuShare * (1.0 + percentage / 100.0));
+  }
+
+  @Override
+  public boolean greaterThan(CPUShare other) {
+    return cpuShare > other.cpuShare;
+  }
+
+  @Override
+  public boolean greaterOrEqual(CPUShare other) {
+    return cpuShare >= other.cpuShare;
+  }
+
+  @Override
+  public boolean lessThan(CPUShare other) {
+    return cpuShare < other.cpuShare;
+  }
+
+  @Override
+  public boolean lessOrEqual(CPUShare other) {
+    return cpuShare <= other.cpuShare;
+  }
+
+  public static Map<String, CPUShare> 
convertDoubleMapToCpuShareMap(Map<String, Double> doubleMap) {
+    Map<String, CPUShare> retval = new HashMap<>();
+    for (Map.Entry<String, Double> entry : doubleMap.entrySet()) {
+      retval.put(entry.getKey(), new CPUShare(entry.getValue()));
+    }
+    return retval;
+  }
+
+  @Override
+  public int compareTo(CPUShare o) {
+    return Double.compare(cpuShare, o.cpuShare);
+  }
+
+  @Override
+  public int hashCode() {
+    return cpuShare.hashCode();
+  }
+
+  @Override
+  public boolean equals(Object other) {
+    if (this == other) {
+      return true;
+    }
+    if (other == null || getClass() != other.getClass()) {
 
 Review comment:
   "!(other instanceof CPUShare))" might be safer.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to