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
00023 import java.util.ArrayList;
00024 import java.util.List;
00025
00032 public class ListField<T> extends Field {
00033
00034 private List<T> value;
00035
00036 public static <T> ListField<T> newVariable(FieldType type, String name) {
00037 return new ListField<T>(type, name);
00038 }
00039
00040 private ListField(FieldType type, String name) {
00041 super(type, name, false);
00042 value = new ArrayList<T>();
00043 }
00044
00045 @SuppressWarnings("unchecked")
00046 @Override
00047 public List<T> getValue() {
00048 return value;
00049 }
00050
00051 @SuppressWarnings("unchecked")
00052 @Override
00053 public void setValue(Object value) {
00054 Preconditions.checkNotNull(value);
00055 this.value = (List<T>) value;
00056 }
00057
00058 @Override
00059 public void serialize(ChannelBuffer buffer) {
00060 buffer.writeInt(value.size());
00061 for (T v : value) {
00062 type.serialize(v, buffer);
00063 }
00064 }
00065
00066 @Override
00067 public void deserialize(ChannelBuffer buffer) {
00068 value.clear();
00069 int size = buffer.readInt();
00070 for (int i = 0; i < size; i++) {
00071 value.add(type.<T>deserialize(buffer));
00072 }
00073 }
00074
00075 @Override
00076 public String getMd5String() {
00077 return String.format("%s %s\n", type, name);
00078 }
00079
00080 @Override
00081 public String getJavaTypeName() {
00082 return String.format("java.util.List<%s>", type.getJavaTypeName());
00083 }
00084
00085 @Override
00086 public String toString() {
00087 return "ListField<" + type + ", " + name + ">";
00088 }
00089
00090 @Override
00091 public int hashCode() {
00092 final int prime = 31;
00093 int result = super.hashCode();
00094 result = prime * result + ((value == null) ? 0 : value.hashCode());
00095 return result;
00096 }
00097
00098 @SuppressWarnings("rawtypes")
00099 @Override
00100 public boolean equals(Object obj) {
00101 if (this == obj)
00102 return true;
00103 if (!super.equals(obj))
00104 return false;
00105 if (getClass() != obj.getClass())
00106 return false;
00107 ListField other = (ListField) obj;
00108 if (value == null) {
00109 if (other.value != null)
00110 return false;
00111 } else if (!value.equals(other.value))
00112 return false;
00113 return true;
00114 }
00115 }