/**
 * Copyright (C) 2012 - 2014 Xeiam LLC http://xeiam.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.xeiam.xchange.bitfinex.v1.service.polling;

import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.RestProxyFactory;

import com.xeiam.xchange.ExchangeSpecification;
import com.xeiam.xchange.bitfinex.v1.BitfinexAuthenticated;
import com.xeiam.xchange.bitfinex.v1.service.BitfinexBaseService;
import com.xeiam.xchange.bitfinex.v1.service.BitfinexHmacPostBodyDigest;
import com.xeiam.xchange.bitfinex.v1.service.BitfinexPayloadDigest;

/**
 * @author Matija Mazi
 */
public class BitfinexBasePollingService extends BitfinexBaseService {

  private static final long START_MILLIS = 1356998400000L; // Jan 1st, 2013 in milliseconds from epoch

  protected final String apiKey;
  protected final BitfinexAuthenticated bitfinex;
  protected final ParamsDigest signatureCreator;
  protected final ParamsDigest payloadCreator;

  // counter for the nonce
  private static int lastNonce = -1;

  /**
   * Constructor
   * 
   * @param exchangeSpecification The {@link ExchangeSpecification}
   */
  public BitfinexBasePollingService(ExchangeSpecification exchangeSpecification) {

    super(exchangeSpecification);
    this.bitfinex = RestProxyFactory.createProxy(BitfinexAuthenticated.class, exchangeSpecification.getSslUri());
    this.apiKey = exchangeSpecification.getApiKey();
    this.signatureCreator = BitfinexHmacPostBodyDigest.createInstance(exchangeSpecification.getSecretKey());
    this.payloadCreator = new BitfinexPayloadDigest();
  }

  protected synchronized int nextNonce() {

    // nonce logic is now more robust.
    // on the first call, it initializes to a number based upon the quarter second. From then on, it increments it.
    //
    // As long as you do not create a new BTCEBasedPollingService more than once every quarter second and make sure
    // that you throw away the old one before you make a new one, this should work out fine.
    if (lastNonce < 0) {
      lastNonce = (int) ((System.currentTimeMillis() - START_MILLIS) / 250L);
    }
    int nonce = lastNonce++;
    return nonce;
  }

}

@@ -1188,16 +1188,67 @@
 lling;%0A%0A
+import java.util.concurrent.atomic.AtomicInteger;%0A%0A
 import s
@@ -1634,38 +1634,8 @@
 t;%0A%0A
-/**%0A * @author Matija Mazi%0A */
 %0Apub
@@ -1701,19 +1701,17 @@
 rvice %7B%0A
-%0A  
+%09
 private 
@@ -1803,19 +1803,145 @@
 m epoch%0A
-%0A  
+%09private static final AtomicInteger lastNonce = new AtomicInteger((int) ((System.currentTimeMillis() - START_MILLIS) / 250L));%0A%0A%09
 protecte
@@ -1963,18 +1963,17 @@
 apiKey;%0A
-  
+%09
 protecte
@@ -2012,18 +2012,17 @@
 tfinex;%0A
-  
+%09
 protecte
@@ -2060,18 +2060,17 @@
 reator;%0A
-  
+%09
 protecte
@@ -2111,81 +2111,14 @@
 r;%0A%0A
-  // counter for the nonce%0A  private static int lastNonce = -1;%0A%0A  
+%09
 /**%0A
-  
+%09
  * C
@@ -2132,16 +2132,14 @@
 tor%0A
-  
+%09
  * %0A
-  
+%09
  * @
@@ -2204,16 +2204,14 @@
 on%7D%0A
-  
+%09
  */%0A
-  
+%09
 publ
@@ -2288,20 +2288,18 @@
 ion) %7B%0A%0A
-    
+%09%09
 super(ex
@@ -2320,20 +2320,18 @@
 ation);%0A
-    
+%09%09
 this.bit
@@ -2428,28 +2428,26 @@
 tSslUri());%0A
-    
+%09%09
 this.apiKey 
@@ -2483,20 +2483,18 @@
 iKey();%0A
-    
+%09%09
 this.sig
@@ -2590,20 +2590,18 @@
 Key());%0A
-    
+%09%09
 this.pay
@@ -2647,15 +2647,13 @@
 ();%0A
-  %7D%0A%0A  
+%09%7D%0A%0A%09
 prot
@@ -2662,20 +2662,8 @@
 ted 
-synchronized
  int
@@ -2681,539 +2681,47 @@
 ) %7B%0A
-%0A    // nonce logic is now more robust.%0A    // on the first call, it initializes to a number based upon the quarter second. From then on, it increments it.%0A    //%0A    // As long as you do not create a new BTCEBasedPollingService more than once every quarter second and make sure%0A    // that you throw away the old one before you make a new one, this should work out fine.%0A    if (lastNonce %3C 0) %7B%0A      lastNonce = (int) ((System.currentTimeMillis() - START_MILLIS) / 250L);%0A    %7D%0A    int nonce = lastNonce++;%0A    return nonce;%0A  %7D%0A
+%09%09return lastNonce.incrementAndGet();%0A%09%7D
 %0A%7D%0A

Use AtomicInteger for nonce