/*
 * Copyright (C) 2014 Kalin Maldzhanski
 *
 * 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 org.djodjo.json;


import java.util.Iterator;
import java.util.Map;


/**
 * Helper class that can be used for Json Objects containing always the same type;
 *
 * @param <T> The type
 */
public abstract class TypedJsonObject<T> extends JsonObjectWrapper implements Iterable<Map.Entry<String, T>> {

    public T getValue(String key) throws JsonException{
        return get(getJson().get(key));
    }

    public T optValue(String key) throws JsonException{
        return get(getJson().opt(key));
    }

    public TypedJsonObject<T> putValue(String key, T value) throws JsonException {
        getJson().put(key, to(value));
        return this;
    }

    protected abstract T get(JsonElement jsonElement);
    protected abstract JsonElement to(T value);

    @Override
    public Iterator<Map.Entry<String, T>> iterator() {
        return new Iterator<Map.Entry<String, T>>() {
            @Override
            public boolean hasNext() {
                return getJson().iterator().hasNext();
            }

            @Override
            public Map.Entry<String, T> next() {
                return new TypedObjectEntry(getJson().iterator().next());
            }

            @Override
            public void remove() {
                getJson().iterator().remove();
            }
        };
    }

    public int length() {
        return getJson().length();
    }

    final class TypedObjectEntry implements Map.Entry<String, T> {
        private final String key;
        private T value;

        public TypedObjectEntry(Map.Entry<String, JsonElement> entry) {
            this.key = entry.getKey();
            this.value = get(entry.getValue());
        }

        public TypedObjectEntry(String key, T value) {
            this.key = key;
            this.value = value;
        }

        @Override
        public String getKey() {
            return key;
        }

        @Override
        public T getValue() {
            return value;
        }

        @Override
        public T setValue(T value) {
            T old = this.value;
            this.value = value;
            return old;
        }
    }
}

@@ -1432,24 +1432,112 @@
 terator() %7B%0A
+        final Iterator%3CMap.Entry%3CString, JsonElement%3E%3E iterator = getJson().iterator();%0A
         retu
@@ -1574,24 +1574,24 @@
 ng, T%3E%3E() %7B%0A
-
             
@@ -1662,34 +1662,24 @@
  return 
-getJson().
 iterator
 ().hasNe
@@ -1670,18 +1670,16 @@
 iterator
-()
 .hasNext
@@ -1812,34 +1812,24 @@
 ctEntry(
-getJson().
 iterator
 ().next(
@@ -1820,18 +1820,16 @@
 iterator
-()
 .next())
@@ -1918,34 +1918,24 @@
         
-getJson().
 iterator
 ().remov
@@ -1926,18 +1926,16 @@
 iterator
-()
 .remove(

fix typedJsonObject iterator