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 019 package org.apache.hadoop.metrics2; 020 021 import com.google.common.base.Objects; 022 import static com.google.common.base.Preconditions.*; 023 024 import org.apache.hadoop.classification.InterfaceAudience; 025 import org.apache.hadoop.classification.InterfaceStability; 026 027 /** 028 * The immutable metric 029 */ 030 @InterfaceAudience.Public 031 @InterfaceStability.Evolving 032 public abstract class AbstractMetric implements MetricsInfo { 033 private final MetricsInfo info; 034 035 /** 036 * Construct the metric 037 * @param info about the metric 038 */ 039 protected AbstractMetric(MetricsInfo info) { 040 this.info = checkNotNull(info, "metric info"); 041 } 042 043 @Override public String name() { 044 return info.name(); 045 } 046 047 @Override public String description() { 048 return info.description(); 049 } 050 051 protected MetricsInfo info() { 052 return info; 053 } 054 055 /** 056 * Get the value of the metric 057 * @return the value of the metric 058 */ 059 public abstract Number value(); 060 061 /** 062 * Get the type of the metric 063 * @return the type of the metric 064 */ 065 public abstract MetricType type(); 066 067 /** 068 * Accept a visitor interface 069 * @param visitor of the metric 070 */ 071 public abstract void visit(MetricsVisitor visitor); 072 073 @Override public boolean equals(Object obj) { 074 if (obj instanceof AbstractMetric) { 075 final AbstractMetric other = (AbstractMetric) obj; 076 return Objects.equal(info, other.info()) && 077 Objects.equal(value(), other.value()); 078 } 079 return false; 080 } 081 082 @Override public int hashCode() { 083 return Objects.hashCode(info, value()); 084 } 085 086 @Override public String toString() { 087 return Objects.toStringHelper(this) 088 .add("info", info) 089 .add("value", value()) 090 .toString(); 091 } 092 }