/*
 * Copyright 2013 Diwaker Gupta
 *
 * 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 jetcd;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import retrofit.RestAdapter;
import retrofit.RetrofitError;

import java.util.Map;

public class EtcdClientImpl implements EtcdClient {
    private final EtcdApi etcd;

    EtcdClientImpl(final String server) {
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setServer(server)
                .build();
        etcd = restAdapter.create(EtcdApi.class);
    }

    @Override
    public String get(String key) throws EtcdException {
        Preconditions.checkNotNull(key);
        try {
            return etcd.get(key).value;
        } catch (RetrofitError e) {
            throw new EtcdException(e);
        }
    }

    @Override
    public void set(String key, String value) throws EtcdException {
        Preconditions.checkNotNull(key);
        Preconditions.checkNotNull(value);
        try {
            etcd.set(key, value, /*ttl=*/ null);
        } catch (RetrofitError e) {
            throw new EtcdException(e);
        }
    }

    @Override
    public void set(String key, String value, int ttl) throws EtcdException {
        Preconditions.checkNotNull(key);
        Preconditions.checkNotNull(value);
        try {
            etcd.set(key, value, ttl);
        } catch (RetrofitError e) {
            throw new EtcdException(e);
        }
    }

    @Override
    public void delete(String key) throws EtcdException {
        Preconditions.checkNotNull(key);
        try {
            etcd.delete(key);
        } catch (RetrofitError e) {
            throw new EtcdException(e);
        }
    }

    @Override
    public Map<String, String> list(String path) throws EtcdException {
        Preconditions.checkNotNull(path);
        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
        try {
            for (Response response : etcd.list(path)) {
                builder.put(response.key, response.value);
            }
        } catch (RetrofitError e) {
            throw new EtcdException(e);
        }
        return builder.build();
    }

    @Override
    public String testAndSet(String key, String oldValue, String newValue)
            throws EtcdException {
        Preconditions.checkNotNull(key);
        Preconditions.checkNotNull(oldValue);
        Preconditions.checkNotNull(newValue);
        Preconditions.checkArgument(!oldValue.equals(newValue));
        try {
            Response response = etcd.testAndSet(key, oldValue, newValue);
            return response.prevValue;
        } catch (RetrofitError e) {
            throw new EtcdException(e);
        }
    }
}

@@ -652,16 +652,55 @@
 itions;%0A
+import com.google.common.base.Strings;%0A
 import c
@@ -738,16 +738,16 @@
 bleMap;%0A
-
 import r
@@ -1221,36 +1221,61 @@
 itions.check
-NotNull
+Argument(!Strings.isNullOrEmpty
 (key)
+)
 ;%0A        tr
@@ -1517,36 +1517,61 @@
 itions.check
-NotNull
+Argument(!Strings.isNullOrEmpty
 (key)
+)
 ;%0A        Pr
@@ -1583,38 +1583,63 @@
 itions.check
-NotNull
+Argument(!Strings.isNullOrEmpty
 (value)
+)
 ;%0A        tr
@@ -1899,36 +1899,61 @@
 itions.check
-NotNull
+Argument(!Strings.isNullOrEmpty
 (key)
+)
 ;%0A        Pr
@@ -1973,24 +1973,96 @@
 heck
-NotNull
+Argument(!Strings.isNullOrEmpty
 (value)
+)
 ;%0A
+        Preconditions.checkArgument(ttl %3E 0);%0A%0A
     
@@ -2298,38 +2298,64 @@
 itions.check
-NotNull
+Argument(!Strings.isNullOrEmpty
 (key)
+)
 ;%0A
+%0A
         try 
@@ -2596,23 +2596,49 @@
 heck
-NotNull
+Argument(!Strings.isNullOrEmpty
 (path)
+)
 ;%0A
+%0A
     
@@ -3135,20 +3135,45 @@
 heck
-NotNull
+Argument(!Strings.isNullOrEmpty
 (key)
+)
 ;%0A  
@@ -3197,23 +3197,47 @@
 ns.check
-NotNull
+Argument(!Strings.isNullOrEmpty
 (oldValu
@@ -3234,24 +3234,25 @@
 ty(oldValue)
+)
 ;%0A        Pr
@@ -3272,15 +3272,39 @@
 heck
-NotNull
+Argument(!Strings.isNullOrEmpty
 (new
@@ -3301,32 +3301,33 @@
 rEmpty(newValue)
+)
 ;%0A        Precon
@@ -3369,32 +3369,33 @@
 als(newValue));%0A
+%0A
         try %7B%0A  

Check for null + empty strings