Home | History | Annotate | Download | only in atomic
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package java.util.concurrent.atomic;
     18 
     19 /**
     20  * GWT emulated version of {@link AtomicLong}.  It's a thin wrapper
     21  * around the primitive {@code long}.
     22  *
     23  * @author Jige Yu
     24  */
     25 public class AtomicLong extends Number implements java.io.Serializable {
     26 
     27   private long value;
     28 
     29   public AtomicLong(long initialValue) {
     30     this.value = initialValue;
     31   }
     32 
     33   public AtomicLong() {
     34   }
     35 
     36   public final long get() {
     37     return value;
     38   }
     39 
     40   public final void set(long newValue) {
     41     value = newValue;
     42   }
     43 
     44   public final void lazySet(long newValue) {
     45     set(newValue);
     46   }
     47 
     48   public final long getAndSet(long newValue) {
     49     long current = value;
     50     value = newValue;
     51     return current;
     52   }
     53 
     54   public final boolean compareAndSet(long expect, long update) {
     55     if (value == expect) {
     56       value = update;
     57       return true;
     58     } else {
     59       return false;
     60     }
     61   }
     62 
     63   public final long getAndIncrement() {
     64     return value++;
     65   }
     66 
     67   public final long getAndDecrement() {
     68     return value--;
     69   }
     70 
     71   public final long getAndAdd(long delta) {
     72     long current = value;
     73     value += delta;
     74     return current;
     75   }
     76 
     77   public final long incrementAndGet() {
     78     return ++value;
     79   }
     80 
     81   public final long decrementAndGet() {
     82     return --value;
     83   }
     84 
     85   public final long addAndGet(long delta) {
     86     value += delta;
     87     return value;
     88   }
     89 
     90   @Override public String toString() {
     91     return Long.toString(value);
     92   }
     93 
     94   public int intValue() {
     95     return (int) value;
     96   }
     97 
     98   public long longValue() {
     99     return value;
    100   }
    101 
    102   public float floatValue() {
    103     return (float) value;
    104   }
    105 
    106   public double doubleValue() {
    107     return (double) value;
    108   }
    109 }
    110