1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.giraph.counters; 20 21 import com.google.common.base.Objects; 22 import org.apache.hadoop.mapreduce.Counter; 23 24 import java.io.DataInput; 25 import java.io.DataOutput; 26 import java.io.IOException; 27 28 /** 29 * Wrapper around Hadoop Counter to make it easier to use. 30 */ 31 public class GiraphHadoopCounter { 32 /** Hadoop Counter we're wrapping. */ 33 private Counter counter; 34 35 /** 36 * Create wrapping a Hadoop Counter. 37 * 38 * @param counter Hadoop Counter to wrap. 39 */ 40 public GiraphHadoopCounter(Counter counter) { 41 this.counter = counter; 42 } 43 44 /** 45 * Get underlying Hadoop Counter we're wrapping. 46 * 47 * @return Hadoop Counter being wrapped. 48 */ 49 public Counter getHadoopCounter() { 50 return counter; 51 } 52 53 @Override 54 public int hashCode() { 55 return counter.hashCode(); 56 } 57 58 @Override 59 public boolean equals(Object genericRight) { 60 if (genericRight == null) { 61 return false; 62 } 63 if (getClass() != genericRight.getClass()) { 64 return false; 65 } 66 GiraphHadoopCounter right = (GiraphHadoopCounter) genericRight; 67 return Objects.equal(counter, right.counter); 68 } 69 70 /** 71 * Set counter to value. Should be greater than current value. 72 * 73 * @param value long value to set to. 74 */ 75 public void setValue(long value) { 76 increment(value - getValue()); 77 } 78 79 /** 80 * Increment counter value by 1. 81 */ 82 public void increment() { 83 increment(1); 84 } 85 86 /** 87 * Increment counter value. 88 * 89 * @param incr amount to increment by. 90 */ 91 public void increment(long incr) { 92 counter.increment(incr); 93 } 94 95 /** 96 * Get counter value 97 * 98 * @return long value of counter 99 */ 100 public long getValue() { 101 return counter.getValue(); 102 } 103 104 /** 105 * Get counter display name. 106 * 107 * @return String Hadoop counter display name. 108 */ 109 public String getDisplayName() { 110 return counter.getDisplayName(); 111 } 112 113 /** 114 * Get counter name. 115 * 116 * @return String Hadoop counter name. 117 */ 118 public String getName() { 119 return counter.getName(); 120 } 121 122 /** 123 * Write to Hadoop output. 124 * 125 * @param out DataOutput to write to. 126 * @throws IOException if something goes wrong. 127 */ 128 public void write(DataOutput out) throws IOException { 129 counter.write(out); 130 } 131 132 /** 133 * Read from Hadoop input. 134 * 135 * @param in DataInput to read from. 136 * @throws IOException if something goes wrong reading. 137 */ 138 public void readFields(DataInput in) throws IOException { 139 counter.readFields(in); 140 } 141 }