Move adsal into its own subdirectory.
[controller.git] / opendaylight / adsal / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / query / WrapperTypeInfo.java
diff --git a/opendaylight/adsal/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/query/WrapperTypeInfo.java b/opendaylight/adsal/northbound/commons/src/main/java/org/opendaylight/controller/northbound/commons/query/WrapperTypeInfo.java
new file mode 100644 (file)
index 0000000..a8172f2
--- /dev/null
@@ -0,0 +1,59 @@
+/**
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.northbound.commons.query;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import javax.xml.bind.annotation.XmlElement;
+
+public class WrapperTypeInfo extends TypeInfo {
+
+    protected WrapperTypeInfo(String name, Accessor accessor) {
+        super(name, accessor.getType(), accessor);
+    }
+
+    @Override
+    public Object retrieve(Object target, String[] query, int index) throws QueryException {
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("retrieve collection: {}/{}", index, query.length);
+        }
+        if (index >= query.length) return null;
+        explore();
+        TypeInfo child = getChild(query[index]);
+        if (child == null) return null;
+        if (query.length == index+1) { // skipping this node
+            return target;
+        }else { // if list of list go to next node to get value
+            return child.retrieve(target, query, index+1);
+        }
+    }
+
+    @Override
+    public synchronized void explore() {
+        if (_explored) return;
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("exploring wrapper type: {} gtype: {}", _class,
+                    _accessor.getGenericType());
+        }
+        String tn = null;
+        AccessibleObject accessibleObject = _accessor.getAccessibleObject();
+        XmlElement xmlElement = accessibleObject.getAnnotation(XmlElement.class);
+        if (accessibleObject instanceof Field) {
+            Field f = (Field) accessibleObject;
+            tn = DEFAULT_NAME.equals(xmlElement.name())?f.getName() : xmlElement.name();
+        }else if (accessibleObject instanceof Method) {
+            Method m = (Method) accessibleObject;
+            tn = DEFAULT_NAME.equals(xmlElement.name())?m.getName() : xmlElement.name();
+        }
+        this._types.put(tn, new IteratableTypeInfo(tn, this._accessor));
+        _explored = true;
+    }
+
+}