package org.opendaylight.controller.md.sal.dom.xsql;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class XSQLBluePrintRelation implements Serializable {
    private static final long serialVersionUID = 2L;
    private XSQLBluePrintNode parent = null;
    private String property = null;
    private Class<?> childClass = null;

    public XSQLBluePrintRelation(XSQLBluePrintNode _parent, String _property,
        Class<?> _childClass) {
        this.parent = _parent;
        this.property = _property;
        this.childClass = _childClass;
    }

    public Class<?> getNEClosestClass() {
        Class<?> p = parent.getInterface();
        return getNEClosestClass(p);
    }


    public static Class<?> getNEClosestClass(Class<?> p) {
        while (!p.getInterfaces()[0]
            .equals(Object.class/*XSQLBluePrint.STOP_INTERFACE*/)) {
            p = p.getInterfaces()[0];
        }
        return p;
    }

    public XSQLBluePrintNode getParent() {
        return parent;
    }

    public String getProperty() {
        return property;
    }

    public Class<?> getChildClass() {
        return this.childClass;
    }

    public boolean equals(Object obj) {
        XSQLBluePrintRelation other = (XSQLBluePrintRelation) obj;
        if (other.parent != null && this.parent == null) {
            return false;
        }
        if (other.parent == null && this.parent != null) {
            return false;
        }

        if (other.parent == null && this.parent == null) {
            return property.equals(other.property);
        }

        if (other.parent.toString().equals(this.parent.toString())) {
            return property.equals(other.property);
        }

        return false;
    }

    public int hashCode() {
        if (parent != null) {
            return parent.toString().hashCode() + property.hashCode();
        }
        return property.hashCode();
    }

    public String toString() {
        if (parent != null) {
            return parent.toString() + ":" + property;
        } else {
            return property;
        }
    }

    public List<?> execute(Object o) {
        List<Object> result = new LinkedList<>();
        if (o == null) {
            return null;
        }

        if (Set.class.isAssignableFrom(o.getClass())) {
            Set<?> lst = (Set<?>) o;
            for (Object oo : lst) {
                addToResult(result, execute(oo));
            }
            return result;
        } else if (List.class.isAssignableFrom(o.getClass())) {
            List<?> lst = (List<?>) o;
            for (Object oo : lst) {
                addToResult(result, execute(oo));
            }
            return result;
        } else if (Map.class.isAssignableFrom(o.getClass())) {
            Map<?, ?> map = (Map<?, ?>) o;
            for (Object oo : map.values()) {
                addToResult(result, execute(oo));
            }
            return result;
        }

        addToResult(result, XSQLCriteria.getValue(o, this.property));

        return result;
    }

    private static void addToResult(List<Object> result, Object o) {
        if (o == null) {
            return;
        }
        if (Set.class.isAssignableFrom(o.getClass())) {
            Set<?> lst = (Set<?>) o;
            for (Object oo : lst) {
                result.add(oo);
            }
        } else if (List.class.isAssignableFrom(o.getClass())) {
            List<?> lst = (List<?>) o;
            for (Object oo : lst) {
                result.add(oo);
            }
        } else if (Map.class.isAssignableFrom(o.getClass())) {
            Map<?, ?> map = (Map<?, ?>) o;
            for (Object oo : map.values()) {
                result.add(oo);
            }
        } else {
            result.add(o);
        }
    }

}


@@ -2202,58 +2202,8 @@
 ) %7B%0A
-        List%3CObject%3E result = new LinkedList%3C%3E();%0A
     
@@ -2271,92 +2271,81 @@
     
-if (Set.class.isAssignableFrom(o.getClass())) %7B%0A            Set%3C?%3E lst = (Set%3C?%3E) o;
+List%3CObject%3E result = new LinkedList%3C%3E();%0A        if (o instanceof Set) %7B
 %0A   
@@ -2362,35 +2362,42 @@
 or (Object oo : 
-lst
+(Set%3C?%3E) o
 ) %7B%0A            
@@ -2460,129 +2460,39 @@
     
-    return result;%0A        %7D else if (List.class.isAssignableFrom(o.getClass())) %7B%0A            List%3C?%3E lst = (List%3C?%3E) o;
+%7D else if (o instanceof List) %7B
 %0A   
@@ -2509,35 +2509,43 @@
 or (Object oo : 
-lst
+(List%3C?%3E) o
 ) %7B%0A            
@@ -2608,132 +2608,38 @@
     
-    return result;%0A        %7D else if (Map.class.isAssignableFrom(o.getClass())) %7B%0A            Map%3C?, ?%3E map = (Map%3C?, ?%3E) o;
+%7D else if (o instanceof Map) %7B
 %0A   
@@ -2656,35 +2656,47 @@
 or (Object oo : 
-map
+((Map%3C?, ?%3E) o)
 .values()) %7B%0A   
@@ -2768,38 +2768,21 @@
     
-    return result;%0A    
+%7D else %7B%0A
     
-%7D%0A%0A
     
@@ -2847,16 +2847,25 @@
 erty));%0A
+        %7D
 %0A       
@@ -2974,182 +2974,26 @@
  (o 
-== null) %7B%0A            return;%0A        %7D%0A        if (Set.class.isAssignableFrom(o.getClass())) %7B%0A            Set%3C?%3E lst = (Set%3C?%3E) o;%0A            for (Object oo : ls
+instanceof Se
 t) %7B%0A
-    
     
@@ -3014,88 +3014,60 @@
 .add
-(oo);%0A            %7D%0A        %7D else if (List.class.isAssignableFrom(o.getClass())
+All((Set%3C?%3E)o);%0A        %7D else if (o instanceof List
 ) %7B%0A
@@ -3082,22 +3082,22 @@
     
-List%3C?%3E lst = 
+result.addAll(
 (Lis
@@ -3101,30 +3101,28 @@
 List%3C?%3E)
- o;%0A  
+o);%0A
         
   for (O
@@ -3117,291 +3117,120 @@
     
-  for (Object oo : lst) %7B%0A                result.add(oo);%0A            %7D%0A        %7D else if (Map.class.isAssignableFrom(o.getClass())) %7B%0A            Map%3C?, ?%3E map = (Map%3C?, ?%3E) o;%0A            for (Object oo : map.values()) %7B%0A                result.add(oo);%0A            %7D%0A        %7D else
+%7D else if (o instanceof Map) %7B%0A            result.addAll(((Map%3C?, ?%3E)o).values());%0A        %7D else if (o != null)
  %7B%0A 
@@ -3271,12 +3271,11 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A%0A

Optimize xsql's use of collections