Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / android / OCLib / src / com / oc / OCResource.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package com.oc;
22
23 import java.net.URI;
24 import java.util.ArrayList;
25
26 /**
27  * Static methods allow you to call to the OC stack, while non-static methods provide traversal
28  * through the tree of hierarchical resources. Non-static methods also retrieve properties of the
29  * current OCResource object.
30  */
31 public class OCResource {
32   private OCResource _parent = null;
33   private ArrayList<OCResource> _children = null;
34   private boolean _isContainer = false;
35
36   public OCResource(OCResource parent, ArrayList<OCResource> children, boolean isContainer) {
37     _parent = parent;
38     _children = children;
39     _isContainer = isContainer;
40   }
41
42   OCResource getParent() {
43     return _parent;
44   }
45
46   ArrayList<OCResource> getChildren() {
47     return _children;
48   }
49
50   /**
51    * Recursively gets all the operable resources below this in the tree to a depth of 2.
52    * 
53    * @return Returns an ArrayList of operable OCResource objects below this OCResource's position in
54    *         the tree.
55    */
56   ArrayList<OCResource> getResource() {
57     return getResources(2);
58   }
59
60   /**
61    * Recursively gets all the operable resources below this in the tree.
62    * 
63    * @param depth - The depth to traverse the tree downward.
64    * @return Returns an ArrayList of operable OCResource objects below this OCResource's position in
65    *         the tree.
66    */
67   ArrayList<OCResource> getResources(int depth) {
68     if (depth - 1 <= 0) {
69       return null;
70     }
71     ArrayList<OCResource> retRes = new ArrayList<OCResource>();
72     if (_isContainer == true) { // If this is a resource container, add all the resources below me
73                                 // in my tree.
74       for (OCResource child : getChildren()) {
75         retRes.addAll(child.getResources(depth - 1));
76       }
77     } else {
78       retRes.add(OCResource.this);
79     }
80     return retRes;
81   }
82
83   /**
84    * Recursively gets all the container (i.e. inoperable) resources below this in the tree to a 
85    * depth of 2.
86    * 
87    * @return Returns an ArrayList of inoperable OCResource objects below this OCResource's position
88    *         in the tree.
89    */
90   ArrayList<OCResource> getResourceContainers() {
91     return getResourceContainers(2);
92   }
93
94   /**
95    * Recursively gets all the container (i.e. inoperable) resources below this in the tree.
96    * 
97    * @param depth - The depth to traverse the tree downward.
98    * 
99    * @return Returns an ArrayList of inoperable OCResource objects below this OCResource's position
100    *         in the tree.
101    */
102   ArrayList<OCResource> getResourceContainers(int depth) {
103     if (depth - 1 <= 0) {
104       return null;
105     }
106     ArrayList<OCResource> retRes = new ArrayList<OCResource>();
107     if (_isContainer == true) {
108       for (OCResource child : getChildren()) {
109         retRes.addAll(child.getResourceContainers(depth - 1));
110       }
111       retRes.add(OCResource.this);
112     }
113     return retRes;
114   }
115   /**
116    * Recursively gets all the resources (operable & inoperable) resources below this in the tree
117    * to a depth of 2.
118    * 
119    * @return Returns an ArrayList of operable & inoperable OCResource objects below this
120    *         OCResource's position in the tree.
121    */
122   ArrayList<OCResource> getAllResources() {
123     return getAllResources(2);
124   }
125
126   /**
127    * Recursively gets all the resources (operable & inoperable) resources below this in the tree.
128    * 
129    * @param depth - The depth to traverse the tree downward.
130    * 
131    * @return Returns an ArrayList of operable & inoperable OCResource objects below this
132    *         OCResource's position in the tree.
133    */
134   ArrayList<OCResource> getAllResources(int depth) {
135     if (depth - 1 <= 0) {
136       return null;
137     }
138     ArrayList<OCResource> retRes = new ArrayList<OCResource>();
139     if (_isContainer == true) {
140       for (OCResource child : getChildren()) {
141         retRes.addAll(child.getAllResources(depth - 1));
142       }
143       retRes.add(OCResource.this);
144     } else {
145       retRes.add(OCResource.this);
146     }
147     return retRes;
148   }
149
150   /**
151    * 
152    * @return Returns the resource URI to address this Resource. You can determine the service that
153    *         is hosting this resource by parsing this URI.
154    */
155   URI getURI() {
156     return null;
157   }
158
159   /**
160    * Returns the requested interface for this resource
161    * 
162    * @param interfaceClass - The interface that is desired.
163    * 
164    * @return The interface requested or NULL if not supported.
165    */
166   public <T> T getInterface(java.lang.Class<T> interfaceClass) {
167     return null;
168   }
169 }