68c8376050444134558e376b500fa7422ca1f4bd
[controller.git] / opendaylight / md-sal / sal-dom-xsql / src / main / java / org / opendaylight / controller / md / sal / dom / xsql / XSQLBluePrintRelation.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.md.sal.dom.xsql;
10
11 import java.io.Serializable;
12 import java.util.LinkedList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 public class XSQLBluePrintRelation implements Serializable {
18     private static final long serialVersionUID = 2L;
19     private XSQLBluePrintNode parent = null;
20     private String property = null;
21     private Class<?> childClass = null;
22
23     public XSQLBluePrintRelation(XSQLBluePrintNode _parent, String _property,
24         Class<?> _childClass) {
25         this.parent = _parent;
26         this.property = _property;
27         this.childClass = _childClass;
28     }
29
30     public Class<?> getNEClosestClass() {
31         Class<?> p = parent.getInterface();
32         return getNEClosestClass(p);
33     }
34
35
36     public static Class<?> getNEClosestClass(Class<?> p) {
37         while (!p.getInterfaces()[0]
38             .equals(Object.class/*XSQLBluePrint.STOP_INTERFACE*/)) {
39             p = p.getInterfaces()[0];
40         }
41         return p;
42     }
43
44     public XSQLBluePrintNode getParent() {
45         return parent;
46     }
47
48     public String getProperty() {
49         return property;
50     }
51
52     public Class<?> getChildClass() {
53         return this.childClass;
54     }
55
56     public boolean equals(Object obj) {
57         XSQLBluePrintRelation other = (XSQLBluePrintRelation) obj;
58         if (other.parent != null && this.parent == null) {
59             return false;
60         }
61         if (other.parent == null && this.parent != null) {
62             return false;
63         }
64
65         if (other.parent == null && this.parent == null) {
66             return property.equals(other.property);
67         }
68
69         if (other.parent.toString().equals(this.parent.toString())) {
70             return property.equals(other.property);
71         }
72
73         return false;
74     }
75
76     public int hashCode() {
77         if (parent != null) {
78             return parent.toString().hashCode() + property.hashCode();
79         }
80         return property.hashCode();
81     }
82
83     public String toString() {
84         if (parent != null) {
85             return parent.toString() + ":" + property;
86         } else {
87             return property;
88         }
89     }
90
91     public List<?> execute(Object o) {
92         if (o == null) {
93             return null;
94         }
95
96         List<Object> result = new LinkedList<>();
97         if (o instanceof Set) {
98             for (Object oo : (Set<?>) o) {
99                 addToResult(result, execute(oo));
100             }
101         } else if (o instanceof List) {
102             for (Object oo : (List<?>) o) {
103                 addToResult(result, execute(oo));
104             }
105         } else if (o instanceof Map) {
106             for (Object oo : ((Map<?, ?>) o).values()) {
107                 addToResult(result, execute(oo));
108             }
109         } else {
110             addToResult(result, XSQLCriteria.getValue(o, this.property));
111         }
112         return result;
113     }
114
115     private static void addToResult(List<Object> result, Object o) {
116         if (o instanceof Set) {
117             result.addAll((Set<?>)o);
118         } else if (o instanceof List) {
119             result.addAll((List<?>)o);
120         } else if (o instanceof Map) {
121             result.addAll(((Map<?, ?>)o).values());
122         } else if (o != null) {
123             result.add(o);
124         }
125     }
126 }
127