LongArrayField.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.internal.message.field;
00018 
00019 import com.google.common.base.Preconditions;
00020 
00021 import org.jboss.netty.buffer.ChannelBuffer;
00022 
00023 import java.util.Arrays;
00024 
00028 public class LongArrayField extends Field {
00029 
00030   private final int size;
00031 
00032   private long[] value;
00033 
00034   public static LongArrayField newVariable(FieldType type, String name, int size) {
00035     Preconditions.checkArgument(type.equals(PrimitiveFieldType.UINT32)
00036         || type.equals(PrimitiveFieldType.INT64) || type.equals(PrimitiveFieldType.UINT64));
00037     return new LongArrayField(type, name, size);
00038   }
00039 
00040   private LongArrayField(FieldType type, String name, int size) {
00041     super(type, name, false);
00042     this.size = size;
00043     setValue(new long[Math.max(0, size)]);
00044   }
00045 
00046   @SuppressWarnings("unchecked")
00047   @Override
00048   public long[] getValue() {
00049     return value;
00050   }
00051 
00052   @Override
00053   public void setValue(Object value) {
00054     Preconditions.checkArgument(size < 0 || ((long[]) value).length == size);
00055     this.value = (long[]) value;
00056   }
00057 
00058   @Override
00059   public void serialize(ChannelBuffer buffer) {
00060     if (size < 0) {
00061       buffer.writeInt(value.length);
00062     }
00063     for (long v : value) {
00064       type.serialize(v, buffer);
00065     }
00066   }
00067 
00068   @Override
00069   public void deserialize(ChannelBuffer buffer) {
00070     int currentSize = size;
00071     if (currentSize < 0) {
00072       currentSize = buffer.readInt();
00073     }
00074     value = new long[currentSize];
00075     for (int i = 0; i < currentSize; i++) {
00076       value[i] = buffer.readLong();
00077     }
00078   }
00079 
00080   @Override
00081   public String getMd5String() {
00082     return String.format("%s %s\n", type, name);
00083   }
00084 
00085   @Override
00086   public String getJavaTypeName() {
00087     return type.getJavaTypeName() + "[]";
00088   }
00089 
00090   @Override
00091   public String toString() {
00092     return "LongArrayField<" + type + ", " + name + ">";
00093   }
00094 
00095   @Override
00096   public int hashCode() {
00097     final int prime = 31;
00098     int result = super.hashCode();
00099     result = prime * result + ((value == null) ? 0 : value.hashCode());
00100     return result;
00101   }
00102 
00103   @Override
00104   public boolean equals(Object obj) {
00105     if (this == obj)
00106       return true;
00107     if (!super.equals(obj))
00108       return false;
00109     if (getClass() != obj.getClass())
00110       return false;
00111     LongArrayField other = (LongArrayField) obj;
00112     if (value == null) {
00113       if (other.value != null)
00114         return false;
00115     } else if (!Arrays.equals(value, other.value))
00116       return false;
00117     return true;
00118   }
00119 }


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