Merge "Remove raw references to Map in XSQL"
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLBluePrintRelation.java
1 package org.opendaylight.controller.md.sal.dom.xsql;
2
3 import java.io.Serializable;
4 import java.util.LinkedList;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8
9 public class XSQLBluePrintRelation implements Serializable {
10     private static final long serialVersionUID = 2L;
11     private XSQLBluePrintNode parent = null;
12     private String property = null;
13     private Class<?> childClass = null;
14
15     public XSQLBluePrintRelation(XSQLBluePrintNode _parent, String _property,
16         Class<?> _childClass) {
17         this.parent = _parent;
18         this.property = _property;
19         this.childClass = _childClass;
20     }
21
22     public Class<?> getNEClosestClass() {
23         Class<?> p = parent.getInterface();
24         return getNEClosestClass(p);
25     }
26
27
28     public static Class<?> getNEClosestClass(Class<?> p) {
29         while (!p.getInterfaces()[0]
30             .equals(Object.class/*XSQLBluePrint.STOP_INTERFACE*/)) {
31             p = p.getInterfaces()[0];
32         }
33         return p;
34     }
35
36     public XSQLBluePrintNode getParent() {
37         return parent;
38     }
39
40     public String getProperty() {
41         return property;
42     }
43
44     public Class<?> getChildClass() {
45         return this.childClass;
46     }
47
48     public boolean equals(Object obj) {
49         XSQLBluePrintRelation other = (XSQLBluePrintRelation) obj;
50         if (other.parent != null && this.parent == null) {
51             return false;
52         }
53         if (other.parent == null && this.parent != null) {
54             return false;
55         }
56
57         if (other.parent == null && this.parent == null) {
58             return property.equals(other.property);
59         }
60
61         if (other.parent.toString().equals(this.parent.toString())) {
62             return property.equals(other.property);
63         }
64
65         return false;
66     }
67
68     public int hashCode() {
69         if (parent != null) {
70             return parent.toString().hashCode() + property.hashCode();
71         }
72         return property.hashCode();
73     }
74
75     public String toString() {
76         if (parent != null) {
77             return parent.toString() + ":" + property;
78         } else {
79             return property;
80         }
81     }
82
83     public List<?> execute(Object o) {
84         List<Object> result = new LinkedList<>();
85         if (o == null) {
86             return null;
87         }
88
89         if (Set.class.isAssignableFrom(o.getClass())) {
90             Set<?> lst = (Set<?>) o;
91             for (Object oo : lst) {
92                 addToResult(result, execute(oo));
93             }
94             return result;
95         } else if (List.class.isAssignableFrom(o.getClass())) {
96             List<?> lst = (List<?>) o;
97             for (Object oo : lst) {
98                 addToResult(result, execute(oo));
99             }
100             return result;
101         } else if (Map.class.isAssignableFrom(o.getClass())) {
102             Map<?, ?> map = (Map<?, ?>) o;
103             for (Object oo : map.values()) {
104                 addToResult(result, execute(oo));
105             }
106             return result;
107         }
108
109         addToResult(result, XSQLCriteria.getValue(o, this.property));
110
111         return result;
112     }
113
114     private static void addToResult(List<Object> result, Object o) {
115         if (o == null) {
116             return;
117         }
118         if (Set.class.isAssignableFrom(o.getClass())) {
119             Set<?> lst = (Set<?>) o;
120             for (Object oo : lst) {
121                 result.add(oo);
122             }
123         } else if (List.class.isAssignableFrom(o.getClass())) {
124             List<?> lst = (List<?>) o;
125             for (Object oo : lst) {
126                 result.add(oo);
127             }
128         } else if (Map.class.isAssignableFrom(o.getClass())) {
129             Map<?, ?> map = (Map<?, ?>) o;
130             for (Object oo : map.values()) {
131                 result.add(oo);
132             }
133         } else {
134             result.add(o);
135         }
136     }
137
138 }
139