Initial merge-commit of the OIC code. Should successfully do discovery for single...
[platform/upstream/iotivity.git] / csdk / android / OCLib / src / com / oc / OCResource.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
5
6 package com.oc;
7
8 import java.net.URI;
9 import java.util.ArrayList;
10
11 /**
12  * Static methods allow you to call to the OC stack, while non-static methods provide traversal
13  * through the tree of hierarchical resources. Non-static methods also retrieve properties of the
14  * current OCResource object.
15  */
16 public class OCResource {
17   private OCResource _parent = null;
18   private ArrayList<OCResource> _children = null;
19   private boolean _isContainer = false;
20
21   public OCResource(OCResource parent, ArrayList<OCResource> children, boolean isContainer) {
22     _parent = parent;
23     _children = children;
24     _isContainer = isContainer;
25   }
26
27   OCResource getParent() {
28     return _parent;
29   }
30
31   ArrayList<OCResource> getChildren() {
32     return _children;
33   }
34
35   /**
36    * Recursively gets all the operable resources below this in the tree to a depth of 2.
37    * 
38    * @return Returns an ArrayList of operable OCResource objects below this OCResource's position in
39    *         the tree.
40    */
41   ArrayList<OCResource> getResource() {
42     return getResources(2);
43   }
44
45   /**
46    * Recursively gets all the operable resources below this in the tree.
47    * 
48    * @param depth - The depth to traverse the tree downward.
49    * @return Returns an ArrayList of operable OCResource objects below this OCResource's position in
50    *         the tree.
51    */
52   ArrayList<OCResource> getResources(int depth) {
53     if (depth - 1 <= 0) {
54       return null;
55     }
56     ArrayList<OCResource> retRes = new ArrayList<OCResource>();
57     if (_isContainer == true) { // If this is a resource container, add all the resources below me
58                                 // in my tree.
59       for (OCResource child : getChildren()) {
60         retRes.addAll(child.getResources(depth - 1));
61       }
62     } else {
63       retRes.add(OCResource.this);
64     }
65     return retRes;
66   }
67
68   /**
69    * Recursively gets all the container (i.e. inoperable) resources below this in the tree to a 
70    * depth of 2.
71    * 
72    * @return Returns an ArrayList of inoperable OCResource objects below this OCResource's position
73    *         in the tree.
74    */
75   ArrayList<OCResource> getResourceContainers() {
76     return getResourceContainers(2);
77   }
78
79   /**
80    * Recursively gets all the container (i.e. inoperable) resources below this in the tree.
81    * 
82    * @param depth - The depth to traverse the tree downward.
83    * 
84    * @return Returns an ArrayList of inoperable OCResource objects below this OCResource's position
85    *         in the tree.
86    */
87   ArrayList<OCResource> getResourceContainers(int depth) {
88     if (depth - 1 <= 0) {
89       return null;
90     }
91     ArrayList<OCResource> retRes = new ArrayList<OCResource>();
92     if (_isContainer == true) {
93       for (OCResource child : getChildren()) {
94         retRes.addAll(child.getResourceContainers(depth - 1));
95       }
96       retRes.add(OCResource.this);
97     }
98     return retRes;
99   }
100   /**
101    * Recursively gets all the resources (operable & inoperable) resources below this in the tree
102    * to a depth of 2.
103    * 
104    * @return Returns an ArrayList of operable & inoperable OCResource objects below this
105    *         OCResource's position in the tree.
106    */
107   ArrayList<OCResource> getAllResources() {
108     return getAllResources(2);
109   }
110
111   /**
112    * Recursively gets all the resources (operable & inoperable) resources below this in the tree.
113    * 
114    * @param depth - The depth to traverse the tree downward.
115    * 
116    * @return Returns an ArrayList of operable & inoperable OCResource objects below this
117    *         OCResource's position in the tree.
118    */
119   ArrayList<OCResource> getAllResources(int depth) {
120     if (depth - 1 <= 0) {
121       return null;
122     }
123     ArrayList<OCResource> retRes = new ArrayList<OCResource>();
124     if (_isContainer == true) {
125       for (OCResource child : getChildren()) {
126         retRes.addAll(child.getAllResources(depth - 1));
127       }
128       retRes.add(OCResource.this);
129     } else {
130       retRes.add(OCResource.this);
131     }
132     return retRes;
133   }
134
135   /**
136    * 
137    * @return Returns the resource URI to address this Resource. You can determine the service that
138    *         is hosting this resource by parsing this URI.
139    */
140   URI getURI() {
141     return null;
142   }
143
144   /**
145    * Returns the requested interface for this resource
146    * 
147    * @param interfaceClass - The interface that is desired.
148    * 
149    * @return The interface requested or NULL if not supported.
150    */
151   public <T> T getInterface(java.lang.Class<T> interfaceClass) {
152     return null;
153   }
154 }