/*
 * Copyright 2016 Red Hat, Inc.
 *
 * Red Hat licenses this file to you 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.vertx.ext.cluster.infinispan.impl;

import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.shareddata.Counter;
import org.infinispan.counter.api.SyncStrongCounter;

import java.util.Objects;

/**
 * @author Thomas Segismont
 */
public class InfinispanCounter implements Counter {

  private final Vertx vertx;
  private final SyncStrongCounter strongCounter;

  public InfinispanCounter(Vertx vertx, SyncStrongCounter strongCounter) {
    this.vertx = vertx;
    this.strongCounter = strongCounter;
  }

  @Override
  public void get(Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    vertx.executeBlocking(future -> {
      future.complete(strongCounter.getValue());
    }, false, resultHandler);
  }

  @Override
  public void incrementAndGet(Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    addAndGet(1L, resultHandler);
  }

  @Override
  public void getAndIncrement(Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    getAndAdd(1L, resultHandler);
  }

  @Override
  public void decrementAndGet(Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    addAndGet(-1L, resultHandler);
  }

  @Override
  public void addAndGet(long value, Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    vertx.executeBlocking(future -> {
      future.complete(strongCounter.addAndGet(value));
    }, false, resultHandler);
  }

  @Override
  public void getAndAdd(long value, Handler<AsyncResult<Long>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    vertx.executeBlocking(future -> {
      future.complete(strongCounter.addAndGet(value) - value);
    }, false, resultHandler);
  }

  @Override
  public void compareAndSet(long expected, long value, Handler<AsyncResult<Boolean>> resultHandler) {
    Objects.requireNonNull(resultHandler, "resultHandler");
    vertx.executeBlocking(future -> {
      future.complete(strongCounter.compareAndSet(expected, value));
    }, false, resultHandler);
  }
}

@@ -696,16 +696,45 @@
 Result;%0A
+import io.vertx.core.Future;%0A
 import i
@@ -1213,24 +1213,180 @@
 unter;%0A  %7D%0A%0A
+  @Override%0A  public Future%3CLong%3E get() %7B%0A    return vertx.executeBlocking(future -%3E %7B%0A      future.complete(strongCounter.getValue());%0A    %7D, false);%0A  %7D%0A%0A
   @Override%0A
@@ -1515,118 +1515,123 @@
     
-vertx.executeBlocking(future -%3E %7B%0A      future.complete(strongCounter.getValue());%0A    %7D, false, resultHandler
+get().setHandler(resultHandler);%0A  %7D%0A%0A  @Override%0A  public Future%3CLong%3E incrementAndGet() %7B%0A    return addAndGet(1L
 );%0A 
@@ -1811,32 +1811,117 @@
 tHandler);%0A  %7D%0A%0A
+  @Override%0A  public Future%3CLong%3E getAndIncrement() %7B%0A    return getAndAdd(1L);%0A  %7D%0A%0A
   @Override%0A  pu
@@ -2081,32 +2081,118 @@
 tHandler);%0A  %7D%0A%0A
+  @Override%0A  public Future%3CLong%3E decrementAndGet() %7B%0A    return addAndGet(-1L);%0A  %7D%0A%0A
   @Override%0A  pu
@@ -2353,32 +2353,210 @@
 tHandler);%0A  %7D%0A%0A
+  @Override%0A  public Future%3CLong%3E addAndGet(long value) %7B%0A    return vertx.executeBlocking(future -%3E %7B%0A      future.complete(strongCounter.addAndGet(value));%0A    %7D, false);%0A  %7D%0A%0A
   @Override%0A  pu
@@ -2687,32 +2687,150 @@
 tHandler%22);%0A    
+addAndGet(value).setHandler(resultHandler);%0A  %7D%0A%0A  @Override%0A  public Future%3CLong%3E getAndAdd(long value) %7B%0A    return 
 vertx.executeBlo
@@ -2891,32 +2891,40 @@
 addAndGet(value)
+ - value
 );%0A    %7D, false,
@@ -2918,39 +2918,24 @@
     %7D, false
-, resultHandler
 );%0A  %7D%0A%0A  @O
@@ -3078,32 +3078,172 @@
 tHandler%22);%0A    
+getAndAdd(value).setHandler(resultHandler);%0A  %7D%0A%0A  @Override%0A  public Future%3CBoolean%3E compareAndSet(long expected, long value) %7B%0A    return 
 vertx.executeBlo
@@ -3300,33 +3300,39 @@
 ter.
-addAndGet(value) -
+compareAndSet(expected,
  value)
+)
 ;%0A  
@@ -3337,39 +3337,24 @@
     %7D, false
-, resultHandler
 );%0A  %7D%0A%0A  @O
@@ -3531,78 +3531,8 @@
     
-vertx.executeBlocking(future -%3E %7B%0A      future.complete(strongCounter.
 comp
@@ -3561,25 +3561,20 @@
 lue)
-);%0A    %7D, false, 
+.setHandler(
 resu

Update to latest vertx-core changes