Home | History | Annotate | Download | only in collect
      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 
     15 package com.google.common.collect;
     16 
     17 import com.google.common.annotations.GwtCompatible;
     18 
     19 import java.io.Serializable;
     20 
     21 import 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
     29 final class Count implements Serializable {
     30   private int value;
     31 
     32   Count(int value) {
     33     this.value = value;
     34   }
     35 
     36   public int get() {
     37     return value;
     38   }
     39 
     40   public int getAndAdd(int delta) {
     41     int result = value;
     42     value = result + delta;
     43     return result;
     44   }
     45 
     46   public int addAndGet(int delta) {
     47     return value += delta;
     48   }
     49 
     50   public void set(int newValue) {
     51     value = newValue;
     52   }
     53 
     54   public int getAndSet(int newValue) {
     55     int result = value;
     56     value = newValue;
     57     return result;
     58   }
     59 
     60   @Override
     61   public int hashCode() {
     62     return value;
     63   }
     64 
     65   @Override
     66   public boolean equals(@Nullable Object obj) {
     67     return obj instanceof Count && ((Count) obj).value == value;
     68   }
     69 
     70   @Override
     71   public String toString() {
     72     return Integer.toString(value);
     73   }
     74 }
     75