Search in sources :

Example 1 with VisitorResult

use of com.servoy.j2db.util.visitor.IVisitor.VisitorResult in project servoy-client by Servoy.

the class AbstractBaseQuery method acceptVisitor.

public static <T> T acceptVisitor(T o, IVisitor visitor) {
    if (o == null) {
        return null;
    }
    Object v = visitor.visit(o);
    T o2;
    if (v instanceof VisitorResult) {
        o2 = (T) ((VisitorResult) v).object;
        if (!((VisitorResult) v).continueTraversal) {
            return o2;
        }
    } else {
        o2 = (T) v;
        if (o2 != o) {
            // the visitor returned something else then the input, visting stops.
            return o2;
        }
    }
    if (o2 instanceof List) {
        for (int i = 0; i < ((List) o2).size(); i++) {
            ((List<Object>) o2).set(i, acceptVisitor(((List) o2).get(i), visitor));
        }
    } else if (o2 instanceof Map) {
        Iterator<Map.Entry> it = ((Map) o2).entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            entry.setValue(acceptVisitor(entry.getValue(), visitor));
        }
    } else if (o2 instanceof Object[]) {
        for (int i = 0; i < ((Object[]) o2).length; i++) {
            ((Object[]) o2)[i] = acceptVisitor(((Object[]) o2)[i], visitor);
        }
    } else if (o2 instanceof Pair) {
        Pair pair = (Pair) o2;
        pair.setLeft(acceptVisitor(pair.getLeft(), visitor));
        pair.setRight(acceptVisitor(pair.getRight(), visitor));
    } else if (o2 instanceof IVisitable) {
        ((IVisitable) o2).acceptVisitor(visitor);
    }
    return o2;
}
Also used : VisitorResult(com.servoy.j2db.util.visitor.IVisitor.VisitorResult) Iterator(java.util.Iterator) ReplacedObject(com.servoy.j2db.util.serialize.ReplacedObject) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Pair(com.servoy.j2db.util.Pair) IVisitable(com.servoy.j2db.util.visitor.IVisitable)

Aggregations

Pair (com.servoy.j2db.util.Pair)1 ReplacedObject (com.servoy.j2db.util.serialize.ReplacedObject)1 IVisitable (com.servoy.j2db.util.visitor.IVisitable)1 VisitorResult (com.servoy.j2db.util.visitor.IVisitor.VisitorResult)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1