/**
  * Returns the current value of the counter. NOTE: if the value has never been set, <code>0</code>
  * is returned.
  *
  * @return value info
  * @throws Exception ZooKeeper errors
  */
 public AtomicValue<byte[]> get() throws Exception {
   MutableAtomicValue<byte[]> result = new MutableAtomicValue<byte[]>(null, null, false);
   getCurrentValue(result, new Stat());
   result.postValue = result.preValue;
   result.succeeded = true;
   return result;
 }
 /**
  * Atomically sets the value to the given updated value if the current value {@code ==} the
  * expected value. Remember to always check {@link AtomicValue#succeeded()}.
  *
  * @param expectedValue the expected value
  * @param newValue the new value
  * @return value info
  * @throws Exception ZooKeeper errors
  */
 public AtomicValue<byte[]> compareAndSet(byte[] expectedValue, byte[] newValue) throws Exception {
   Stat stat = new Stat();
   MutableAtomicValue<byte[]> result = new MutableAtomicValue<byte[]>(null, null, false);
   boolean createIt = getCurrentValue(result, stat);
   if (!createIt && Arrays.equals(expectedValue, result.preValue)) {
     try {
       client.setData().withVersion(stat.getVersion()).forPath(path, newValue);
       result.succeeded = true;
       result.postValue = newValue;
     } catch (KeeperException.BadVersionException dummy) {
       result.succeeded = false;
     } catch (KeeperException.NoNodeException dummy) {
       result.succeeded = false;
     }
   } else {
     result.succeeded = false;
   }
   return result;
 }
  private boolean tryOnce(MutableAtomicValue<byte[]> result, MakeValue makeValue) throws Exception {
    Stat stat = new Stat();
    boolean createIt = getCurrentValue(result, stat);

    boolean success = false;
    try {
      byte[] newValue = makeValue.makeFrom(result.preValue);
      if (createIt) {
        client.create().forPath(path, newValue);
      } else {
        client.setData().withVersion(stat.getVersion()).forPath(path, newValue);
      }
      result.postValue = Arrays.copyOf(newValue, newValue.length);
      success = true;
    } catch (KeeperException.NodeExistsException e) {
      // do Retry
    } catch (KeeperException.BadVersionException e) {
      // do Retry
    } catch (KeeperException.NoNodeException e) {
      // do Retry
    }

    return success;
  }