1/* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 11 * express or implied. See the License for the specific language governing permissions and 12 * limitations under the License. 13 */ 14 15package com.google.common.collect; 16 17import com.google.common.annotations.GwtCompatible; 18 19import java.io.Serializable; 20 21import javax.annotation.Nullable; 22 23/** 24 * A mutable value of type {@code int}, for multisets to use in tracking counts of values. 25 * 26 * @author Louis Wasserman 27 */ 28@GwtCompatible 29final class Count implements Serializable { 30 private int value; 31 32 Count() { 33 this(0); 34 } 35 36 Count(int value) { 37 this.value = value; 38 } 39 40 public int get() { 41 return value; 42 } 43 44 public int getAndAdd(int delta) { 45 int result = value; 46 value = result + delta; 47 return result; 48 } 49 50 public int addAndGet(int delta) { 51 return value += delta; 52 } 53 54 public void set(int newValue) { 55 value = newValue; 56 } 57 58 public int getAndSet(int newValue) { 59 int result = value; 60 value = newValue; 61 return result; 62 } 63 64 @Override 65 public int hashCode() { 66 return value; 67 } 68 69 @Override 70 public boolean equals(@Nullable Object obj) { 71 return obj instanceof Count && ((Count) obj).value == value; 72 } 73 74 @Override 75 public String toString() { 76 return Integer.toString(value); 77 } 78} 79