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.internal.message.context; 00018 00019 import com.google.common.collect.Lists; 00020 import com.google.common.collect.Maps; 00021 00022 import org.ros.internal.message.field.FieldFactory; 00023 import org.ros.message.MessageDeclaration; 00024 import org.ros.message.MessageFactory; 00025 import org.ros.message.MessageIdentifier; 00026 00027 import java.util.Collections; 00028 import java.util.List; 00029 import java.util.Map; 00030 00038 public class MessageContext { 00039 00040 private final MessageDeclaration messageDeclaration; 00041 private final MessageFactory messageFactory; 00042 private final Map<String, FieldFactory> fieldFactories; 00043 private final Map<String, String> fieldGetterNames; 00044 private final Map<String, String> fieldSetterNames; 00045 private final List<String> fieldNames; 00046 00047 public MessageContext(MessageDeclaration messageDeclaration, MessageFactory messageFactory) { 00048 this.messageDeclaration = messageDeclaration; 00049 this.messageFactory = messageFactory; 00050 this.fieldFactories = Maps.newHashMap(); 00051 this.fieldGetterNames = Maps.newHashMap(); 00052 this.fieldSetterNames = Maps.newHashMap(); 00053 this.fieldNames = Lists.newArrayList(); 00054 } 00055 00056 public MessageFactory getMessageFactory() { 00057 return messageFactory; 00058 } 00059 00060 public MessageIdentifier getMessageIdentifer() { 00061 return messageDeclaration.getMessageIdentifier(); 00062 } 00063 00064 public String getType() { 00065 return messageDeclaration.getType(); 00066 } 00067 00068 public String getPackage() { 00069 return messageDeclaration.getPackage(); 00070 } 00071 00072 public String getName() { 00073 return messageDeclaration.getName(); 00074 } 00075 00076 public String getDefinition() { 00077 return messageDeclaration.getDefinition(); 00078 } 00079 00080 public void addFieldFactory(String name, FieldFactory fieldFactory) { 00081 fieldFactories.put(name, fieldFactory); 00082 fieldGetterNames.put(name, "get" + getJavaName(name)); 00083 fieldSetterNames.put(name, "set" + getJavaName(name)); 00084 fieldNames.add(name); 00085 } 00086 00087 private String getJavaName(String name) { 00088 String[] parts = name.split("_"); 00089 StringBuilder fieldName = new StringBuilder(); 00090 for (String part : parts) { 00091 fieldName.append(part.substring(0, 1).toUpperCase() + part.substring(1)); 00092 } 00093 return fieldName.toString(); 00094 } 00095 00096 public boolean hasField(String name) { 00097 // O(1) instead of an O(n) check against the list of field names. 00098 return fieldFactories.containsKey(name); 00099 } 00100 00101 public String getFieldGetterName(String name) { 00102 return fieldGetterNames.get(name); 00103 } 00104 00105 public String getFieldSetterName(String name) { 00106 return fieldSetterNames.get(name); 00107 } 00108 00109 public FieldFactory getFieldFactory(String name) { 00110 return fieldFactories.get(name); 00111 } 00112 00116 public List<String> getFieldNames() { 00117 return Collections.unmodifiableList(fieldNames); 00118 } 00119 00120 @Override 00121 public int hashCode() { 00122 final int prime = 31; 00123 int result = 1; 00124 result = prime * result + ((messageDeclaration == null) ? 0 : messageDeclaration.hashCode()); 00125 return result; 00126 } 00127 00128 @Override 00129 public boolean equals(Object obj) { 00130 if (this == obj) 00131 return true; 00132 if (obj == null) 00133 return false; 00134 if (getClass() != obj.getClass()) 00135 return false; 00136 MessageContext other = (MessageContext) obj; 00137 if (messageDeclaration == null) { 00138 if (other.messageDeclaration != null) 00139 return false; 00140 } else if (!messageDeclaration.equals(other.messageDeclaration)) 00141 return false; 00142 return true; 00143 } 00144 }