/*
 * Copyright 2017-present Open Networking Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.atomix.core.counter.impl;

import io.atomix.core.counter.AtomicCounterType;
import io.atomix.primitive.service.AbstractPrimitiveService;
import io.atomix.primitive.service.BackupInput;
import io.atomix.primitive.service.BackupOutput;
import io.atomix.utils.serializer.Namespace;
import io.atomix.utils.serializer.Namespaces;
import io.atomix.utils.serializer.Serializer;

import java.util.Objects;

/**
 * Atomix long state.
 */
public class DefaultAtomicCounterService extends AbstractPrimitiveService implements AtomicCounterService {
  private static final Serializer SERIALIZER = Serializer.using(Namespace.builder()
      .register(Namespaces.BASIC)
      .build());

  private long value;

  public DefaultAtomicCounterService() {
    super(AtomicCounterType.instance());
  }

  @Override
  public Serializer serializer() {
    return SERIALIZER;
  }

  @Override
  public void backup(BackupOutput writer) {
    writer.writeLong(value);
  }

  @Override
  public void restore(BackupInput reader) {
    value = reader.readLong();
  }

  @Override
  public void set(long value) {
    this.value = value;
  }

  @Override
  public long get() {
    return value;
  }

  @Override
  public boolean compareAndSet(long expect, long update) {
    if (Objects.equals(value, expect)) {
      value = update;
      return true;
    }
    return false;
  }

  @Override
  public long incrementAndGet() {
    Long oldValue = value;
    value = oldValue + 1;
    return value;
  }

  @Override
  public long getAndIncrement() {
    Long oldValue = value;
    value = oldValue + 1;
    return oldValue;
  }

  @Override
  public long decrementAndGet() {
    Long oldValue = value;
    value = oldValue - 1;
    return value;
  }

  @Override
  public long getAndDecrement() {
    Long oldValue = value;
    value = oldValue - 1;
    return oldValue;
  }

  @Override
  public long addAndGet(long delta) {
    Long oldValue = value;
    value = oldValue + delta;
    return value;
  }

  @Override
  public long getAndAdd(long delta) {
    Long oldValue = value;
    value = oldValue + delta;
    return oldValue;
  }
}
@@ -860,170 +860,54 @@
 ut;%0A
+%0A
 import 
-io.atomix.utils.serializer.Namespace;%0Aimport io.atomix.utils.serializer.Namespaces;%0Aimport io.atomix.utils.serializer.Serializer;%0A%0Aimport java.util.Objects
+java.util.concurrent.atomic.AtomicLong
 ;%0A%0A/
@@ -1057,154 +1057,51 @@
 ate 
-static final Serializer SERIALIZER = Serializer.using(Namespace.builder()%0A      .register(Namespaces.BASIC)%0A      .build());%0A%0A  private long value
+final AtomicLong counter = new AtomicLong()
 ;%0A%0A 
@@ -1190,83 +1190,8 @@
  %7D%0A%0A
-  @Override%0A  public Serializer serializer() %7B%0A    return SERIALIZER;%0A  %7D%0A%0A
   @O
@@ -1263,21 +1263,29 @@
 iteLong(
-value
+counter.get()
 );%0A  %7D%0A%0A
@@ -1344,24 +1344,28 @@
 ) %7B%0A    
-value = 
+counter.set(
 reader.r
@@ -1373,16 +1373,17 @@
 adLong()
+)
 ;%0A  %7D%0A%0A 
@@ -1433,26 +1433,26 @@
     
-this.value = 
+counter.set(
 value
+)
 ;%0A  
@@ -1491,37 +1491,45 @@
 () %7B%0A    return 
-value
+counter.get()
 ;%0A  %7D%0A%0A  @Overri
@@ -1598,865 +1598,592 @@
     
-if (Objects.equals(value, expect)) %7B%0A      value = update;%0A      return true;%0A    %7D%0A    return false;%0A  %7D%0A%0A  @Override%0A  public long incrementAndGet() %7B%0A    Long oldValue = value;%0A    value = oldValue + 1;%0A    return value;%0A  %7D%0A%0A  @Override%0A  public long getAndIncrement() %7B%0A    Long oldValue = value;%0A    value = oldValue + 1;%0A    return oldValue;%0A  %7D%0A%0A  @Override%0A  public long decrementAndGet() %7B%0A    Long oldValue = value;%0A    value = oldValue - 1;%0A    return value;%0A  %7D%0A%0A  @Override%0A  public long getAndDecrement() %7B%0A    Long oldValue = value;%0A    value = oldValue - 1;%0A    return oldValue;%0A  %7D%0A%0A  @Override%0A  public long addAndGet(long delta) %7B%0A    Long oldValue = value;%0A    value = oldValue + delta;%0A    return value;%0A  %7D%0A%0A  @Override%0A  public long getAndAdd(long delta) %7B%0A    Long oldValue = value;%0A    value = oldValue + delta;%0A    return oldValue
+return counter.compareAndSet(expect, update);%0A  %7D%0A%0A  @Override%0A  public long incrementAndGet() %7B%0A    return counter.incrementAndGet();%0A  %7D%0A%0A  @Override%0A  public long getAndIncrement() %7B%0A    return counter.getAndIncrement();%0A  %7D%0A%0A  @Override%0A  public long decrementAndGet() %7B%0A    return counter.decrementAndGet();%0A  %7D%0A%0A  @Override%0A  public long getAndDecrement() %7B%0A    return counter.getAndDecrement();%0A  %7D%0A%0A  @Override%0A  public long addAndGet(long delta) %7B%0A    return counter.addAndGet(delta);%0A  %7D%0A%0A  @Override%0A  public long getAndAdd(long delta) %7B%0A    return counter.getAndAdd(delta)
 ;%0A  

Simplify AtomicCounterService implementation.