001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.metrics2;
020
021import com.google.common.base.Objects;
022import static com.google.common.base.Preconditions.*;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026
027/**
028 * Immutable tag for metrics (for grouping on host/queue/username etc.)
029 */
030@InterfaceAudience.Public
031@InterfaceStability.Evolving
032public class MetricsTag implements MetricsInfo {
033  private final MetricsInfo info;
034  private final String value;
035
036  /**
037   * Construct the tag with name, description and value
038   * @param info  of the tag
039   * @param value of the tag
040   */
041  public MetricsTag(MetricsInfo info, String value) {
042    this.info = checkNotNull(info, "tag info");
043    this.value = value;
044  }
045
046  @Override public String name() {
047    return info.name();
048  }
049
050  @Override public String description() {
051    return info.description();
052  }
053
054  /**
055   * @return the info object of the tag
056   */
057  public MetricsInfo info() {
058    return info;
059  }
060
061  /**
062   * Get the value of the tag
063   * @return  the value
064   */
065  public String value() {
066    return value;
067  }
068
069  @Override public boolean equals(Object obj) {
070    if (obj instanceof MetricsTag) {
071      final MetricsTag other = (MetricsTag) obj;
072      return Objects.equal(info, other.info()) &&
073             Objects.equal(value, other.value());
074    }
075    return false;
076  }
077
078  @Override public int hashCode() {
079    return Objects.hashCode(info, value);
080  }
081
082  @Override public String toString() {
083    return Objects.toStringHelper(this)
084        .add("info", info)
085        .add("value", value())
086        .toString();
087  }
088}