/*
 *
 *  * Copyright 2019 the original author or authors.
 *  *
 *  * 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 leap.lang.beans;

import leap.lang.Classes;
import leap.lang.Enumerable;
import leap.lang.Enumerables;

import java.util.Map;
import java.util.function.BiConsumer;

public class BeanTraverser {

    private final Object bean;

    public BeanTraverser(Object bean) {
        this.bean = bean;
    }

    public void traverse(BiConsumer<ValMeta, Object> func) {
        traverse(ValMeta.ROOT, bean, func);
    }

    protected void traverse(ValMeta meta, Object val, BiConsumer<ValMeta, Object> func) {
        func.accept(meta, val);

        if (null == val) {
            return;
        }

        if (val instanceof Map) {
            traverseMap((Map) val, func);
            return;
        }

        Enumerable e = Enumerables.tryOf(val);
        if (null != e) {
            traverseEnumerable(val, e, func);
            return;
        }

        if (!Classes.isSimpleValueType(val.getClass())) {
            traverseBean(val, func);
        }
    }

    protected void traverseMap(Map map, BiConsumer<ValMeta, Object> func) {
        map.forEach((k, v) -> {
            traverse(new ValMeta(map, k.toString()), v, func);
        });
    }

    protected void traverseEnumerable(Object raw, Enumerable e, BiConsumer<ValMeta, Object> func) {
        for (int i = 0; i < e.size(); i++) {
            Object v = e.get(i);
            traverse(new ValMeta(raw, i), v, func);
        }
    }

    protected void traverseBean(Object bean, BiConsumer<ValMeta, Object> func) {
        for (BeanProperty bp : BeanType.of(bean.getClass()).getProperties()) {
            if(!bp.isReadable()) {
                continue;
            }
            Object p = bp.getValue(bean);
            traverse(new ValMeta(bean, bp), p, func);
        }
    }

    /**
     * The meta info of value.
     */
    public static class ValMeta {
        private static final ValMeta ROOT = new ValMeta(null, null, null, null, true);

        private final Object       parent;
        private final String       name;
        private final Integer      index;
        private final boolean      root;
        private final BeanProperty property;

        protected ValMeta(Object parent, String name) {
            this(parent, name, null, null, false);
        }

        protected ValMeta(Object parent, BeanProperty property) {
            this(parent, property.getName(), null, property, false);
        }

        protected ValMeta(Object parent, Integer index) {
            this(parent, null, index, null, false);
        }

        private ValMeta(Object parent, String name, Integer index, BeanProperty property, boolean root) {
            this.parent = parent;
            this.name = name;
            this.index = index;
            this.property = property;
            this.root = root;
        }

        public Object getParent() {
            return parent;
        }

        public String getName() {
            return name;
        }

        public Integer getIndex() {
            return index;
        }

        public BeanProperty getProperty() {
            return property;
        }

        public boolean isRoot() {
            return root;
        }
    }
}

@@ -763,24 +763,58 @@
 umerables;%0A%0A
+import java.util.IdentityHashMap;%0A
 import java.
@@ -819,24 +819,24 @@
 a.util.Map;%0A
-
 import java.
@@ -917,20 +917,134 @@
  Object 
-bean
+                          bean;%0A    private final IdentityHashMap%3CObject, Boolean%3E traversed = new IdentityHashMap%3C%3E()
 ;%0A%0A    p
@@ -1305,32 +1305,120 @@
 Object%3E func) %7B%0A
+        if (null != val && traversed.containsKey(val)) %7B%0A            return;%0A        %7D%0A%0A
         func.acc
@@ -1473,32 +1473,32 @@
         return;%0A
-
         %7D%0A%0A     
@@ -1484,32 +1484,75 @@
 urn;%0A        %7D%0A%0A
+        traversed.put(val, Boolean.TRUE);%0A%0A
         if (val 
@@ -2514,16 +2514,17 @@
       if
+ 
 (!bp.isR

fix circulation at BeanTraverser