// Copyright 2016 Twitter. All rights reserved.
//
// Licensed 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 com.twitter.heron.spi.packing;

/**
 * Definition of Resources. Used to form packing structure output.
 */
public class Resource {
  private double cpu;
  private long ram;
  private long disk;

  public Resource(double cpu, long ram, long disk) {
    this.cpu = cpu;
    this.ram = ram;
    this.disk = disk;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj instanceof Resource) {
      Resource r = (Resource) obj;
      return (this.getCpu() == r.getCpu())
          && (this.getRam() == r.getRam())
          && (this.getDisk() == r.getDisk());
    } else {
      return false;
    }
  }

  public double getCpu() {
    return cpu;
  }

  public long getRam() {
    return ram;
  }

  public long getDisk() {
    return disk;
  }

  public Resource cloneWithRam(long newRam) {
    return new Resource(this.getCpu(), newRam, this.getDisk());
  }

  @Override
  public int hashCode() {
    return (Long.hashCode(getRam()) << 2)
         & (Long.hashCode(getDisk()) << 1)
         & (Double.hashCode(getCpu()));
  }

  @Override
  public String toString() {
    return String.format("{cpu: %f, ram: %d, disk: %d}", getCpu(), getRam(), getDisk());
  }
}

@@ -1526,32 +1526,31 @@
 eturn (Long.
-hashCode
+valueOf
 (getRam()) %3C
@@ -1547,16 +1547,27 @@
 etRam())
+.hashCode()
  %3C%3C 2)%0A 
@@ -1582,24 +1582,23 @@
 & (Long.
-hashCode
+valueOf
 (getDisk
@@ -1600,16 +1600,27 @@
 tDisk())
+.hashCode()
  %3C%3C 1)%0A 
@@ -1637,24 +1637,23 @@
 (Double.
-hashCode
+valueOf
 (getCpu(
@@ -1654,16 +1654,27 @@
 etCpu())
+.hashCode()
 );%0A  %7D%0A%0A

Change hashCode() call on numbers to be java 1.7 compliant (#1352)