NameResolver.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.namespace;
00018 
00019 import org.ros.exception.RosRuntimeException;
00020 
00021 import java.util.Collections;
00022 import java.util.HashMap;
00023 import java.util.Map;
00024 
00029 public class NameResolver {
00030 
00031   private final GraphName namespace;
00032   private final Map<GraphName, GraphName> remappings;
00033 
00034   public static NameResolver newFromNamespace(GraphName namespace) {
00035     return new NameResolver(namespace, new HashMap<GraphName, GraphName>());
00036   }
00037 
00038   public static NameResolver newFromNamespace(String namespace) {
00039     return newFromNamespace(GraphName.of(namespace));
00040   }
00041 
00042   public static NameResolver newRoot() {
00043     return newFromNamespace(GraphName.root());
00044   }
00045 
00046   public static NameResolver newRootFromRemappings(Map<GraphName, GraphName> remappings) {
00047     return new NameResolver(GraphName.root(), remappings);
00048   }
00049 
00050   public static NameResolver newFromNamespaceAndRemappings(String namespace,
00051       Map<GraphName, GraphName> remappings) {
00052     return new NameResolver(GraphName.of(namespace), remappings);
00053   }
00054 
00055   public NameResolver(GraphName namespace, Map<GraphName, GraphName> remappings) {
00056     this.remappings = Collections.unmodifiableMap(remappings);
00057     this.namespace = namespace;
00058   }
00059 
00060   public GraphName getNamespace() {
00061     return namespace;
00062   }
00063 
00075   public GraphName resolve(GraphName namespace, GraphName name) {
00076     GraphName remappedNamespace = lookUpRemapping(namespace);
00077     if (!remappedNamespace.isGlobal()) {
00078       throw new IllegalArgumentException(String.format(
00079           "Namespace %s (remapped from %s) must be global.", remappedNamespace, namespace));
00080     }
00081     GraphName remappedName = lookUpRemapping(name);
00082     if (remappedName.isGlobal()) {
00083       return remappedName;
00084     }
00085     if (remappedName.isRelative()) {
00086       return remappedNamespace.join(remappedName);
00087     }
00088     if (remappedName.isPrivate()) {
00089       throw new RosRuntimeException("Cannot resolve ~private names in arbitrary namespaces.");
00090     }
00091     throw new RosRuntimeException("Unable to resolve graph name: " + name);
00092   }
00093 
00097   public GraphName resolve(String namespace, String name) {
00098     return resolve(GraphName.of(namespace), GraphName.of(name));
00099   }
00100 
00104   public GraphName resolve(GraphName namespace, String name) {
00105     return resolve(namespace, GraphName.of(name));
00106   }
00107 
00111   public GraphName resolve(String namespace, GraphName name) {
00112     return resolve(GraphName.of(namespace), name);
00113   }
00114 
00120   public GraphName resolve(GraphName name) {
00121     return resolve(namespace, name);
00122   }
00123 
00127   public GraphName resolve(String name) {
00128     return resolve(GraphName.of(name));
00129   }
00130 
00134   public Map<GraphName, GraphName> getRemappings() {
00135     return remappings;
00136   }
00137 
00149   public NameResolver newChild(GraphName namespace) {
00150     return new NameResolver(resolve(namespace), remappings);
00151   }
00152 
00156   public NameResolver newChild(String namespace) {
00157     return newChild(GraphName.of(namespace));
00158   }
00159 
00160   protected GraphName lookUpRemapping(GraphName name) {
00161     GraphName remappedName = name;
00162     if (remappings.containsKey(name)) {
00163       remappedName = remappings.get(name);
00164     }
00165     return remappedName;
00166   }
00167 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49