BUG-2218: Keep existing link augmentations during discovery process
[controller.git] / opendaylight / adsal / northbound / commons / src / main / java / org / opendaylight / controller / northbound / commons / query / IteratableTypeInfo.java
1 package org.opendaylight.controller.northbound.commons.query;
2
3 import java.lang.reflect.ParameterizedType;
4 import java.lang.reflect.Type;
5 import java.util.ArrayList;
6 import java.util.Collection;
7 import java.util.Iterator;
8 import java.util.List;
9
10 /**
11  */
12 /*package*/ class IteratableTypeInfo extends TypeInfo {
13
14     public IteratableTypeInfo(String name, Accessor accessor) {
15         super(name, accessor.getType(), accessor);
16     }
17
18     @Override
19     public Object retrieve(Object target, String[] query, int index) throws QueryException {
20         if (LOGGER.isDebugEnabled()) {
21             LOGGER.debug("retrieve collection: {}/{} type:{}", index, query.length,
22                     target.getClass());
23         }
24         explore();
25         Collection<?> c = (Collection<?>) target;
26         Iterator<?> it = c.iterator();
27         List<Object> objects = new ArrayList<Object>();
28         while (it.hasNext()) {
29             Object item = it.next();
30             for (TypeInfo child : _types.values()) {
31                 Object val = child.retrieve(item, query, index);
32                 if (val != null) objects.add(val);
33             }
34         }
35         return objects;
36
37     }
38
39     @Override
40     public synchronized void explore() {
41         if (_explored) return;
42         if (LOGGER.isDebugEnabled()) {
43             LOGGER.debug("exploring iteratable type: {} gtype: {}", _class,
44                     _accessor.getGenericType());
45         }
46         Type t = _accessor.getGenericType();
47         if (t instanceof ParameterizedType) {
48             Type[] pt = ((ParameterizedType) t).getActualTypeArguments();
49             // First type is a child, ignore rest
50             if (pt.length > 0) {
51                 _types.put(_name, new TypeInfo(_name, (Class)pt[0], null));
52             }
53         }
54         _explored = true;
55     }
56
57     @Override
58     public TypeInfo getCollectionChild(Class<?> childType) {
59         explore();
60         for (TypeInfo ti : _types.values()) {
61             if (ti.getType().equals(childType)) {
62                 return ti;
63             }
64         }
65         return null;
66     }
67 }