ae1dffdf325d3c5594dc4ba414714450b8288444
[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 /**
18  * To be removed in Nitrogen
19  */
20 @Deprecated
21 public class XSQLBluePrintRelation implements Serializable {
22     private static final long serialVersionUID = 2L;
23     private XSQLBluePrintNode parent = null;
24     private String property = null;
25     private Class<?> childClass = null;
26
27     public XSQLBluePrintRelation(XSQLBluePrintNode _parent, String _property,
28         Class<?> _childClass) {
29         this.parent = _parent;
30         this.property = _property;
31         this.childClass = _childClass;
32     }
33
34     public Class<?> getNEClosestClass() {
35         Class<?> p = parent.getInterface();
36         return getNEClosestClass(p);
37     }
38
39
40     public static Class<?> getNEClosestClass(Class<?> p) {
41         while (!p.getInterfaces()[0]
42             .equals(Object.class/*XSQLBluePrint.STOP_INTERFACE*/)) {
43             p = p.getInterfaces()[0];
44         }
45         return p;
46     }
47
48     public XSQLBluePrintNode getParent() {
49         return parent;
50     }
51
52     public String getProperty() {
53         return property;
54     }
55
56     public Class<?> getChildClass() {
57         return this.childClass;
58     }
59
60     public boolean equals(Object obj) {
61         XSQLBluePrintRelation other = (XSQLBluePrintRelation) obj;
62         if (other.parent != null && this.parent == null) {
63             return false;
64         }
65         if (other.parent == null && this.parent != null) {
66             return false;
67         }
68
69         if (other.parent == null && this.parent == null) {
70             return property.equals(other.property);
71         }
72
73         if (other.parent.toString().equals(this.parent.toString())) {
74             return property.equals(other.property);
75         }
76
77         return false;
78     }
79
80     public int hashCode() {
81         if (parent != null) {
82             return parent.toString().hashCode() + property.hashCode();
83         }
84         return property.hashCode();
85     }
86
87     public String toString() {
88         if (parent != null) {
89             return parent.toString() + ":" + property;
90         } else {
91             return property;
92         }
93     }
94
95     public List<?> execute(Object o) {
96         if (o == null) {
97             return null;
98         }
99
100         List<Object> result = new LinkedList<>();
101         if (o instanceof Set) {
102             for (Object oo : (Set<?>) o) {
103                 addToResult(result, execute(oo));
104             }
105         } else if (o instanceof List) {
106             for (Object oo : (List<?>) o) {
107                 addToResult(result, execute(oo));
108             }
109         } else if (o instanceof Map) {
110             for (Object oo : ((Map<?, ?>) o).values()) {
111                 addToResult(result, execute(oo));
112             }
113         } else {
114             addToResult(result, XSQLCriteria.getValue(o, this.property));
115         }
116         return result;
117     }
118
119     private static void addToResult(List<Object> result, Object o) {
120         if (o instanceof Set) {
121             result.addAll((Set<?>)o);
122         } else if (o instanceof List) {
123             result.addAll((List<?>)o);
124         } else if (o instanceof Map) {
125             result.addAll(((Map<?, ?>)o).values());
126         } else if (o != null) {
127             result.add(o);
128         }
129     }
130 }
131