Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.message.field;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import org.jboss.netty.buffer.ChannelBuffer;
00022
00026 class ValueField<T> extends Field {
00027
00028 private T value;
00029
00030 static <T> ValueField<T> newConstant(FieldType type, String name, T value) {
00031 return new ValueField<T>(type, name, value, true);
00032 }
00033
00034 static <T> ValueField<T> newVariable(FieldType type, String name) {
00035 return new ValueField<T>(type, name, null, false);
00036 }
00037
00038 private ValueField(FieldType type, String name, T value, boolean isConstant) {
00039 super(type, name, isConstant);
00040 this.value = value;
00041 }
00042
00043 @SuppressWarnings("unchecked")
00044 @Override
00045 public T getValue() {
00046 if (value == null) {
00047 setValue(type.getDefaultValue());
00048 }
00049 return value;
00050 }
00051
00052 @SuppressWarnings("unchecked")
00053 @Override
00054 public void setValue(Object value) {
00055 Preconditions.checkNotNull(value);
00056 Preconditions.checkState(!isConstant);
00057 this.value = (T) value;
00058 }
00059
00060 @Override
00061 public void serialize(ChannelBuffer buffer) {
00062 type.serialize(getValue(), buffer);
00063 }
00064
00065 @Override
00066 public void deserialize(ChannelBuffer buffer) {
00067 Preconditions.checkState(!isConstant);
00068 setValue(type.<T>deserialize(buffer));
00069 }
00070
00071 @Override
00072 public String getMd5String() {
00073 return String.format("%s %s\n", type, name);
00074 }
00075
00076 @Override
00077 public String getJavaTypeName() {
00078 return type.getJavaTypeName();
00079 }
00080
00081 @Override
00082 public String toString() {
00083 return "ValueField<" + type + ", " + name + ">";
00084 }
00085
00086 @Override
00087 public int hashCode() {
00088 final int prime = 31;
00089 int result = super.hashCode();
00090 result = prime * result + ((getValue() == null) ? 0 : getValue().hashCode());
00091 return result;
00092 }
00093
00094 @Override
00095 public boolean equals(Object obj) {
00096 if (this == obj)
00097 return true;
00098 if (!super.equals(obj))
00099 return false;
00100 if (getClass() != obj.getClass())
00101 return false;
00102 Field other = (Field) obj;
00103 if (getValue() == null) {
00104 if (other.getValue() != null)
00105 return false;
00106 } else if (!getValue().equals(other.getValue()))
00107 return false;
00108 return true;
00109 }
00110 }