00001 /* 00002 * Copyright (C) 2012 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.field; 00018 00019 import com.google.common.collect.Lists; 00020 import com.google.common.collect.Maps; 00021 00022 import org.ros.exception.RosMessageRuntimeException; 00023 import org.ros.internal.message.context.MessageContext; 00024 00025 import java.util.Collections; 00026 import java.util.List; 00027 import java.util.Map; 00028 00032 public class MessageFields { 00033 00034 private final Map<String, Field> fields; 00035 private final Map<String, Field> setters; 00036 private final Map<String, Field> getters; 00037 private final List<Field> orderedFields; 00038 00039 public MessageFields(MessageContext messageContext) { 00040 fields = Maps.newHashMap(); 00041 setters = Maps.newHashMap(); 00042 getters = Maps.newHashMap(); 00043 orderedFields = Lists.newArrayList(); 00044 for (String name : messageContext.getFieldNames()) { 00045 Field field = messageContext.getFieldFactory(name).create(); 00046 fields.put(name, field); 00047 getters.put(messageContext.getFieldGetterName(name), field); 00048 setters.put(messageContext.getFieldSetterName(name), field); 00049 orderedFields.add(field); 00050 } 00051 } 00052 00053 public Field getField(String name) { 00054 return fields.get(name); 00055 } 00056 00057 public Field getSetterField(String name) { 00058 return setters.get(name); 00059 } 00060 00061 public Field getGetterField(String name) { 00062 return getters.get(name); 00063 } 00064 00065 public List<Field> getFields() { 00066 return Collections.unmodifiableList(orderedFields); 00067 } 00068 00069 public Object getFieldValue(String name) { 00070 Field field = fields.get(name); 00071 if (field != null) { 00072 return field.getValue(); 00073 } 00074 throw new RosMessageRuntimeException("Uknown field: " + name); 00075 } 00076 00077 public void setFieldValue(String name, Object value) { 00078 Field field = fields.get(name); 00079 if (field != null) { 00080 field.setValue(value); 00081 } else { 00082 throw new RosMessageRuntimeException("Uknown field: " + name); 00083 } 00084 } 00085 00086 @Override 00087 public int hashCode() { 00088 final int prime = 31; 00089 int result = 1; 00090 result = prime * result + ((fields == null) ? 0 : fields.hashCode()); 00091 result = prime * result + ((orderedFields == null) ? 0 : orderedFields.hashCode()); 00092 return result; 00093 } 00094 00095 @Override 00096 public boolean equals(Object obj) { 00097 if (this == obj) 00098 return true; 00099 if (obj == null) 00100 return false; 00101 if (getClass() != obj.getClass()) 00102 return false; 00103 MessageFields other = (MessageFields) obj; 00104 if (fields == null) { 00105 if (other.fields != null) 00106 return false; 00107 } else if (!fields.equals(other.fields)) 00108 return false; 00109 if (orderedFields == null) { 00110 if (other.orderedFields != null) 00111 return false; 00112 } else if (!orderedFields.equals(other.orderedFields)) 00113 return false; 00114 return true; 00115 } 00116 }