/**
 * 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.hadoop.yarn.server.api.records;

import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceStability.Evolving;
import org.apache.hadoop.yarn.util.Records;

/**
 * <p>
 * <code>ResourceUtilization</code> models the utilization of a set of computer
 * resources in the cluster.
 * </p>
 */
@Private
@Evolving
public abstract class ResourceUtilization implements
    Comparable<ResourceUtilization> {
  public static ResourceUtilization newInstance(int pmem, int vmem, float cpu) {
    ResourceUtilization utilization =
        Records.newRecord(ResourceUtilization.class);
    utilization.setPhysicalMemory(pmem);
    utilization.setVirtualMemory(vmem);
    utilization.setCPU(cpu);
    return utilization;
  }

  /**
   * Get used <em>virtual memory</em>.
   *
   * @return <em>virtual memory</em> in MB
   */
  public abstract int getVirtualMemory();

  /**
   * Set used <em>virtual memory</em>.
   *
   * @param vmem <em>virtual memory</em> in MB
   */
  public abstract void setVirtualMemory(int vmem);

  /**
   * Get <em>physical memory</em>.
   *
   * @return <em>physical memory</em> in MB
   */
  public abstract int getPhysicalMemory();

  /**
   * Set <em>physical memory</em>.
   *
   * @param pmem <em>physical memory</em> in MB
   */
  public abstract void setPhysicalMemory(int pmem);

  /**
   * Get <em>CPU</em> utilization.
   *
   * @return <em>CPU utilization</em> normalized to 1 CPU
   */
  public abstract float getCPU();

  /**
   * Set <em>CPU</em> utilization.
   *
   * @param cpu <em>CPU utilization</em> normalized to 1 CPU
   */
  public abstract void setCPU(float cpu);

  @Override
  public int hashCode() {
    final int prime = 263167;
    int result = 3571;
    result = prime * result + getVirtualMemory();
    result = prime * result + getPhysicalMemory();
    result = 31 * result + Float.valueOf(getCPU()).hashCode();
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (!(obj instanceof ResourceUtilization)) {
      return false;
    }
    ResourceUtilization other = (ResourceUtilization) obj;
    if (getVirtualMemory() != other.getVirtualMemory()
        || getPhysicalMemory() != other.getPhysicalMemory()
        || getCPU() != other.getCPU()) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "<pmem:" + getPhysicalMemory() + ", vmem:" + getVirtualMemory()
        + ", vCores:" + getCPU() + ">";
  }

  /**
   * Add utilization to the current one.
   * @param pmem Physical memory used to add.
   * @param vmem Virtual memory used to add.
   * @param cpu CPU utilization to add.
   */
  public void addTo(int pmem, int vmem, float cpu) {
    this.setPhysicalMemory(this.getPhysicalMemory() + pmem);
    this.setVirtualMemory(this.getVirtualMemory() + vmem);
    this.setCPU(this.getCPU() + cpu);
  }
}