MessageIdentifier.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.message;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00026 public class MessageIdentifier {
00027 
00028   private String type;
00029   private String pkg;
00030   private String name;
00031 
00032   public static MessageIdentifier of(String pkg, String name) {
00033     Preconditions.checkNotNull(pkg);
00034     Preconditions.checkNotNull(name);
00035     return new MessageIdentifier(pkg, name);
00036   }
00037 
00038   public static MessageIdentifier of(String type) {
00039     Preconditions.checkNotNull(type);
00040     // We're not using Preconditions.checkArgument() here because we want a
00041     // useful error message without paying the performance penalty of
00042     // constructing it every time.
00043     if (!type.contains("/")) {
00044       throw new IllegalArgumentException(String.format(
00045           "Type name is invalid or not fully qualified: \"%s\"", type));
00046     }
00047     return new MessageIdentifier(type);
00048   }
00049 
00050   private MessageIdentifier(String type) {
00051     this.type = type;
00052   }
00053 
00054   private MessageIdentifier(String pkg, String name) {
00055     this.pkg = pkg;
00056     this.name = name;
00057   }
00058 
00059   public String getType() {
00060     if (type == null) {
00061       // Using StringBuilder like this is about 40% faster than using the +
00062       // operator.
00063       StringBuilder stringBuilder = new StringBuilder(pkg.length() + name.length() + 1);
00064       stringBuilder.append(pkg);
00065       stringBuilder.append("/");
00066       stringBuilder.append(name);
00067       type = stringBuilder.toString();
00068     }
00069     return type;
00070   }
00071 
00072   private void splitType() {
00073     String[] packageAndName = type.split("/", 2);
00074     pkg = packageAndName[0];
00075     name = packageAndName[1];
00076   }
00077 
00078   public String getPackage() {
00079     if (pkg == null) {
00080       splitType();
00081     }
00082     return pkg;
00083   }
00084 
00085   public String getName() {
00086     if (name == null) {
00087       splitType();
00088     }
00089     return name;
00090   }
00091 
00092   @Override
00093   public String toString() {
00094     return String.format("MessageIdentifier<%s>", type);
00095   }
00096 
00097   @Override
00098   public int hashCode() {
00099     final int prime = 31;
00100     int result = 1;
00101     result = prime * result + ((type == null) ? 0 : type.hashCode());
00102     return result;
00103   }
00104 
00105   @Override
00106   public boolean equals(Object obj) {
00107     if (this == obj)
00108       return true;
00109     if (obj == null)
00110       return false;
00111     if (getClass() != obj.getClass())
00112       return false;
00113     MessageIdentifier other = (MessageIdentifier) obj;
00114     if (type == null) {
00115       if (other.type != null)
00116         return false;
00117     } else if (!type.equals(other.type))
00118       return false;
00119     return true;
00120   }
00121 }


rosjava_bootstrap
Author(s): Daniel Stonier , Damon Kohler
autogenerated on Fri Aug 28 2015 12:41:44