00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.node.parameter;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import org.ros.exception.ParameterClassCastException;
00022 import org.ros.exception.ParameterNotFoundException;
00023 import org.ros.internal.node.client.ParameterClient;
00024 import org.ros.internal.node.response.Response;
00025 import org.ros.internal.node.response.StatusCode;
00026 import org.ros.internal.node.server.NodeIdentifier;
00027 import org.ros.internal.node.server.ParameterServer;
00028 import org.ros.namespace.GraphName;
00029 import org.ros.namespace.NameResolver;
00030 import org.ros.node.parameter.ParameterListener;
00031 import org.ros.node.parameter.ParameterTree;
00032
00033 import java.net.URI;
00034 import java.util.Arrays;
00035 import java.util.List;
00036 import java.util.Map;
00037
00044 public class DefaultParameterTree implements ParameterTree {
00045
00046 private final ParameterClient parameterClient;
00047 private final ParameterManager parameterManager;
00048 private final NameResolver resolver;
00049
00050 public static DefaultParameterTree newFromNodeIdentifier(NodeIdentifier nodeIdentifier,
00051 URI masterUri, NameResolver resolver, ParameterManager parameterManager) {
00052 ParameterClient client = new ParameterClient(nodeIdentifier, masterUri);
00053 return new DefaultParameterTree(client, parameterManager, resolver);
00054 }
00055
00056 private DefaultParameterTree(ParameterClient parameterClient, ParameterManager parameterManager,
00057 NameResolver resolver) {
00058 this.parameterClient = parameterClient;
00059 this.parameterManager = parameterManager;
00060 this.resolver = resolver;
00061 }
00062
00063 @Override
00064 public boolean has(GraphName name) {
00065 GraphName resolvedName = resolver.resolve(name);
00066 return parameterClient.hasParam(resolvedName).getResult();
00067 }
00068
00069 @Override
00070 public boolean has(String name) {
00071 return has(GraphName.of(name));
00072 }
00073
00074 @Override
00075 public void delete(GraphName name) {
00076 GraphName resolvedName = resolver.resolve(name);
00077 parameterClient.deleteParam(resolvedName);
00078 }
00079
00080 @Override
00081 public void delete(String name) {
00082 delete(GraphName.of(name));
00083 }
00084
00085 @Override
00086 public GraphName search(GraphName name) {
00087 GraphName resolvedName = resolver.resolve(name);
00088 Response<GraphName> response = parameterClient.searchParam(resolvedName);
00089 if (response.getStatusCode() == StatusCode.SUCCESS) {
00090 return response.getResult();
00091 } else {
00092 return null;
00093 }
00094 }
00095
00096 @Override
00097 public GraphName search(String name) {
00098 return search(GraphName.of(name));
00099 }
00100
00101 @Override
00102 public List<GraphName> getNames() {
00103 return parameterClient.getParamNames().getResult();
00104 }
00105
00106 @Override
00107 public void addParameterListener(GraphName name, ParameterListener listener) {
00108 parameterManager.addListener(name, listener);
00109 parameterClient.subscribeParam(name);
00110 }
00111
00112 @Override
00113 public void addParameterListener(String name, ParameterListener listener) {
00114 addParameterListener(GraphName.of(name), listener);
00115 }
00116
00117 @Override
00118 public void set(GraphName name, boolean value) {
00119 GraphName resolvedName = resolver.resolve(name);
00120 parameterClient.setParam(resolvedName, value);
00121 }
00122
00123 @Override
00124 public void set(String name, boolean value) {
00125 set(GraphName.of(name), value);
00126 }
00127
00128 @Override
00129 public void set(GraphName name, int value) {
00130 GraphName resolvedName = resolver.resolve(name);
00131 parameterClient.setParam(resolvedName, value);
00132 }
00133
00134 @Override
00135 public void set(String name, int value) {
00136 set(GraphName.of(name), value);
00137 }
00138
00139 @Override
00140 public void set(GraphName name, double value) {
00141 GraphName resolvedName = resolver.resolve(name);
00142 parameterClient.setParam(resolvedName, value);
00143 }
00144
00145 @Override
00146 public void set(String name, double value) {
00147 set(GraphName.of(name), value);
00148 }
00149
00150 @Override
00151 public void set(GraphName name, String value) {
00152 GraphName resolvedName = resolver.resolve(name);
00153 parameterClient.setParam(resolvedName, value);
00154 }
00155
00156 @Override
00157 public void set(String name, String value) {
00158 set(GraphName.of(name), value);
00159 }
00160
00161 @Override
00162 public void set(GraphName name, List<?> value) {
00163 GraphName resolvedName = resolver.resolve(name);
00164 parameterClient.setParam(resolvedName, value);
00165 }
00166
00167 @Override
00168 public void set(String name, List<?> value) {
00169 set(GraphName.of(name), value);
00170 }
00171
00172 @Override
00173 public void set(GraphName name, Map<?, ?> value) {
00174 GraphName resolvedName = resolver.resolve(name);
00175 parameterClient.setParam(resolvedName, value);
00176 }
00177
00178 @Override
00179 public void set(String name, Map<?, ?> value) {
00180 set(GraphName.of(name), value);
00181 }
00182
00183 private <T> T get(GraphName name, Class<T> type) {
00184 GraphName resolvedName = resolver.resolve(name);
00185 Response<Object> response = parameterClient.getParam(resolvedName);
00186 try {
00187 if (response.getStatusCode() == StatusCode.SUCCESS) {
00188 return type.cast(response.getResult());
00189 }
00190 } catch (ClassCastException e) {
00191 throw new ParameterClassCastException("Cannot cast parameter to: " + type.getName(), e);
00192 }
00193 throw new ParameterNotFoundException("Parameter does not exist: " + name);
00194 }
00195
00196 @SuppressWarnings("unchecked")
00197 private <T> T get(GraphName name, T defaultValue) {
00198 Preconditions.checkNotNull(defaultValue);
00199 GraphName resolvedName = resolver.resolve(name);
00200 Response<Object> response = parameterClient.getParam(resolvedName);
00201 if (response.getStatusCode() == StatusCode.SUCCESS) {
00202 try {
00203 return (T) defaultValue.getClass().cast(response.getResult());
00204 } catch (ClassCastException e) {
00205 throw new ParameterClassCastException("Cannot cast parameter to: "
00206 + defaultValue.getClass().getName(), e);
00207 }
00208 } else {
00209 return defaultValue;
00210 }
00211 }
00212
00213 @Override
00214 public boolean getBoolean(GraphName name) {
00215 return get(name, Boolean.class);
00216 }
00217
00218 @Override
00219 public boolean getBoolean(String name) {
00220 return getBoolean(GraphName.of(name));
00221 }
00222
00223 @Override
00224 public boolean getBoolean(GraphName name, boolean defaultValue) {
00225 return get(name, defaultValue);
00226 }
00227
00228 @Override
00229 public boolean getBoolean(String name, boolean defaultValue) {
00230 return getBoolean(GraphName.of(name), defaultValue);
00231 }
00232
00233 @Override
00234 public int getInteger(GraphName name) {
00235 return get(name, Integer.class);
00236 }
00237
00238 @Override
00239 public int getInteger(String name) {
00240 return getInteger(GraphName.of(name));
00241 }
00242
00243 @Override
00244 public int getInteger(GraphName name, int defaultValue) {
00245 return get(name, defaultValue);
00246 }
00247
00248 @Override
00249 public int getInteger(String name, int defaultValue) {
00250 return getInteger(GraphName.of(name), defaultValue);
00251 }
00252
00253 @Override
00254 public double getDouble(GraphName name) {
00255 return get(name, Double.class);
00256 }
00257
00258 @Override
00259 public double getDouble(String name) {
00260 return getDouble(GraphName.of(name));
00261 }
00262
00263 @Override
00264 public double getDouble(GraphName name, double defaultValue) {
00265 return get(name, defaultValue);
00266 }
00267
00268 @Override
00269 public double getDouble(String name, double defaultValue) {
00270 return getDouble(GraphName.of(name), defaultValue);
00271 }
00272
00273 @Override
00274 public String getString(GraphName name) {
00275 return get(name, String.class);
00276 }
00277
00278 @Override
00279 public String getString(String name) {
00280 return get(GraphName.of(name), String.class);
00281 }
00282
00283 @Override
00284 public String getString(GraphName name, String defaultValue) {
00285 return get(name, defaultValue);
00286 }
00287
00288 @Override
00289 public String getString(String name, String defaultValue) {
00290 return getString(GraphName.of(name), defaultValue);
00291 }
00292
00293 @Override
00294 public List<?> getList(GraphName name) {
00295 return Arrays.asList(get(name, Object[].class));
00296 }
00297
00298 @Override
00299 public List<?> getList(String name) {
00300 return getList(GraphName.of(name));
00301 }
00302
00303 @Override
00304 public List<?> getList(GraphName name, List<?> defaultValue) {
00305 return Arrays.asList(get(name, defaultValue.toArray()));
00306 }
00307
00308 @Override
00309 public List<?> getList(String name, List<?> defaultValue) {
00310 return getList(GraphName.of(name), defaultValue);
00311 }
00312
00313 @Override
00314 public Map<?, ?> getMap(GraphName name) {
00315 return get(name, Map.class);
00316 }
00317
00318 @Override
00319 public Map<?, ?> getMap(String name) {
00320 return getMap(GraphName.of(name));
00321 }
00322
00323 @Override
00324 public Map<?, ?> getMap(GraphName name, Map<?, ?> defaultValue) {
00325 return get(name, defaultValue);
00326 }
00327
00328 @Override
00329 public Map<?, ?> getMap(String name, Map<?, ?> defaultValue) {
00330 return getMap(GraphName.of(name), defaultValue);
00331 }
00332 }