MessageImpl.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;
00018 
00019 import org.jboss.netty.buffer.ChannelBuffer;
00020 import org.ros.exception.RosMessageRuntimeException;
00021 import org.ros.internal.message.context.MessageContext;
00022 import org.ros.internal.message.field.Field;
00023 import org.ros.internal.message.field.MessageFieldType;
00024 import org.ros.internal.message.field.MessageFields;
00025 import org.ros.message.Duration;
00026 import org.ros.message.MessageIdentifier;
00027 import org.ros.message.Time;
00028 
00029 import java.util.List;
00030 
00034 class MessageImpl implements RawMessage, GetInstance {
00035 
00036   private final MessageContext messageContext;
00037   private final MessageFields messageFields;
00038 
00039   public MessageImpl(MessageContext messageContext) {
00040     this.messageContext = messageContext;
00041     messageFields = new MessageFields(messageContext);
00042   }
00043 
00044   public MessageContext getMessageContext() {
00045     return messageContext;
00046   }
00047 
00048   public MessageFields getMessageFields() {
00049     return messageFields;
00050   }
00051 
00052   @Override
00053   public RawMessage toRawMessage() {
00054     return (RawMessage) this;
00055   }
00056 
00057   @Override
00058   public MessageIdentifier getIdentifier() {
00059     return messageContext.getMessageIdentifer();
00060   }
00061 
00062   @Override
00063   public String getType() {
00064     return messageContext.getType();
00065   }
00066 
00067   @Override
00068   public String getPackage() {
00069     return messageContext.getPackage();
00070   }
00071 
00072   @Override
00073   public String getName() {
00074     return messageContext.getName();
00075   }
00076 
00077   @Override
00078   public String getDefinition() {
00079     return messageContext.getDefinition();
00080   }
00081 
00082   @Override
00083   public List<Field> getFields() {
00084     return messageFields.getFields();
00085   }
00086 
00087   @Override
00088   public boolean getBool(String name) {
00089     return (Boolean) messageFields.getFieldValue(name);
00090   }
00091 
00092   @Override
00093   public boolean[] getBoolArray(String name) {
00094     return (boolean[]) messageFields.getFieldValue(name);
00095   }
00096 
00097   @Override
00098   public Duration getDuration(String name) {
00099     return (Duration) messageFields.getFieldValue(name);
00100   }
00101 
00102   @SuppressWarnings("unchecked")
00103   @Override
00104   public List<Duration> getDurationList(String name) {
00105     return (List<Duration>) messageFields.getFieldValue(name);
00106   }
00107 
00108   @Override
00109   public float getFloat32(String name) {
00110     return (Float) messageFields.getFieldValue(name);
00111   }
00112 
00113   @Override
00114   public float[] getFloat32Array(String name) {
00115     return (float[]) messageFields.getFieldValue(name);
00116   }
00117 
00118   @Override
00119   public double getFloat64(String name) {
00120     return (Double) messageFields.getFieldValue(name);
00121   }
00122 
00123   @Override
00124   public double[] getFloat64Array(String name) {
00125     return (double[]) messageFields.getFieldValue(name);
00126   }
00127 
00128   @Override
00129   public short getInt16(String name) {
00130     return (Short) messageFields.getFieldValue(name);
00131   }
00132 
00133   @Override
00134   public short[] getInt16Array(String name) {
00135     return (short[]) messageFields.getFieldValue(name);
00136   }
00137 
00138   @Override
00139   public int getInt32(String name) {
00140     return (Integer) messageFields.getFieldValue(name);
00141   }
00142 
00143   @Override
00144   public int[] getInt32Array(String name) {
00145     return (int[]) messageFields.getFieldValue(name);
00146   }
00147 
00148   @Override
00149   public long getInt64(String name) {
00150     return (Long) messageFields.getFieldValue(name);
00151   }
00152 
00153   @Override
00154   public long[] getInt64Array(String name) {
00155     return (long[]) messageFields.getFieldValue(name);
00156   }
00157 
00158   @Override
00159   public byte getInt8(String name) {
00160     return (Byte) messageFields.getFieldValue(name);
00161   }
00162 
00163   @Override
00164   public byte[] getInt8Array(String name) {
00165     return (byte[]) messageFields.getFieldValue(name);
00166   }
00167 
00168   @Override
00169   public <T extends Message> T getMessage(String name) {
00170     if (messageFields.getField(name).getType() instanceof MessageFieldType) {
00171       return messageFields.getField(name).<T>getValue();
00172     }
00173     throw new RosMessageRuntimeException("Failed to access message field: " + name);
00174   }
00175 
00176   @Override
00177   public <T extends Message> List<T> getMessageList(String name) {
00178     if (messageFields.getField(name).getType() instanceof MessageFieldType) {
00179       return messageFields.getField(name).<List<T>>getValue();
00180     }
00181     throw new RosMessageRuntimeException("Failed to access list field: " + name);
00182   }
00183 
00184   @Override
00185   public String getString(String name) {
00186     return (String) messageFields.getFieldValue(name);
00187   }
00188 
00189   @SuppressWarnings("unchecked")
00190   @Override
00191   public List<String> getStringList(String name) {
00192     return (List<String>) messageFields.getFieldValue(name);
00193   }
00194 
00195   @Override
00196   public Time getTime(String name) {
00197     return (Time) messageFields.getFieldValue(name);
00198   }
00199 
00200   @SuppressWarnings("unchecked")
00201   @Override
00202   public List<Time> getTimeList(String name) {
00203     return (List<Time>) messageFields.getFieldValue(name);
00204   }
00205 
00206   @Override
00207   public short getUInt16(String name) {
00208     return (Short) messageFields.getFieldValue(name);
00209   }
00210 
00211   @Override
00212   public short[] getUInt16Array(String name) {
00213     return (short[]) messageFields.getFieldValue(name);
00214   }
00215 
00216   @Override
00217   public int getUInt32(String name) {
00218     return (Integer) messageFields.getFieldValue(name);
00219   }
00220 
00221   @Override
00222   public int[] getUInt32Array(String name) {
00223     return (int[]) messageFields.getFieldValue(name);
00224   }
00225 
00226   @Override
00227   public long getUInt64(String name) {
00228     return (Long) messageFields.getFieldValue(name);
00229   }
00230 
00231   @Override
00232   public long[] getUInt64Array(String name) {
00233     return (long[]) messageFields.getFieldValue(name);
00234   }
00235 
00236   @Override
00237   public short getUInt8(String name) {
00238     return (Short) messageFields.getFieldValue(name);
00239   }
00240 
00241   @Override
00242   public short[] getUInt8Array(String name) {
00243     return (short[]) messageFields.getFieldValue(name);
00244   }
00245 
00246   @Override
00247   public void setBool(String name, boolean value) {
00248     messageFields.setFieldValue(name, value);
00249   }
00250 
00251   @Override
00252   public void setBoolArray(String name, boolean[] value) {
00253     messageFields.setFieldValue(name, value);
00254   }
00255 
00256   @Override
00257   public void setDurationList(String name, List<Duration> value) {
00258     messageFields.setFieldValue(name, value);
00259   }
00260 
00261   @Override
00262   public void setDuration(String name, Duration value) {
00263     messageFields.setFieldValue(name, value);
00264   }
00265 
00266   @Override
00267   public void setFloat32(String name, float value) {
00268     messageFields.setFieldValue(name, value);
00269   }
00270 
00271   @Override
00272   public void setFloat32Array(String name, float[] value) {
00273     messageFields.setFieldValue(name, value);
00274   }
00275 
00276   @Override
00277   public void setFloat64(String name, double value) {
00278     messageFields.setFieldValue(name, value);
00279   }
00280 
00281   @Override
00282   public void setFloat64Array(String name, double[] value) {
00283     messageFields.setFieldValue(name, value);
00284   }
00285 
00286   @Override
00287   public void setInt16(String name, short value) {
00288     messageFields.setFieldValue(name, value);
00289   }
00290 
00291   @Override
00292   public void setInt16Array(String name, short[] value) {
00293     messageFields.setFieldValue(name, value);
00294   }
00295 
00296   @Override
00297   public void setInt32(String name, int value) {
00298     messageFields.setFieldValue(name, value);
00299   }
00300 
00301   @Override
00302   public void setInt32Array(String name, int[] value) {
00303     messageFields.setFieldValue(name, value);
00304   }
00305 
00306   @Override
00307   public void setInt64(String name, long value) {
00308     messageFields.setFieldValue(name, value);
00309   }
00310 
00311   @Override
00312   public void setInt64Array(String name, long[] value) {
00313     messageFields.setFieldValue(name, value);
00314   }
00315 
00316   @Override
00317   public void setInt8(String name, byte value) {
00318     messageFields.setFieldValue(name, value);
00319   }
00320 
00321   @Override
00322   public void setInt8Array(String name, byte[] value) {
00323     messageFields.setFieldValue(name, value);
00324   }
00325 
00326   @Override
00327   public void setMessage(String name, Message value) {
00328     // TODO(damonkohler): Verify the type of the provided Message?
00329     messageFields.setFieldValue(name, value);
00330   }
00331 
00332   @Override
00333   public void setMessageList(String name, List<Message> value) {
00334     // TODO(damonkohler): Verify the type of all Messages in the provided list?
00335     messageFields.setFieldValue(name, value);
00336   }
00337 
00338   @Override
00339   public void setString(String name, String value) {
00340     messageFields.setFieldValue(name, value);
00341   }
00342 
00343   @Override
00344   public void setStringList(String name, List<String> value) {
00345     messageFields.setFieldValue(name, value);
00346   }
00347 
00348   @Override
00349   public void setTime(String name, Time value) {
00350     messageFields.setFieldValue(name, value);
00351   }
00352 
00353   @Override
00354   public void setTimeList(String name, List<Time> value) {
00355     messageFields.setFieldValue(name, value);
00356   }
00357 
00358   @Override
00359   public void setUInt16(String name, short value) {
00360     messageFields.setFieldValue(name, value);
00361   }
00362 
00363   @Override
00364   public void setUInt16Array(String name, short[] value) {
00365     messageFields.setFieldValue(name, value);
00366   }
00367 
00368   @Override
00369   public void setUInt32(String name, int value) {
00370     messageFields.setFieldValue(name, value);
00371   }
00372 
00373   @Override
00374   public void setUInt32Array(String name, int[] value) {
00375     messageFields.setFieldValue(name, value);
00376   }
00377 
00378   @Override
00379   public void setUInt64(String name, long value) {
00380     messageFields.setFieldValue(name, value);
00381   }
00382 
00383   @Override
00384   public void setUInt64Array(String name, long[] value) {
00385     messageFields.setFieldValue(name, value);
00386   }
00387 
00388   @Override
00389   public void setUInt8(String name, byte value) {
00390     messageFields.setFieldValue(name, value);
00391   }
00392 
00393   @Override
00394   public void setUInt8Array(String name, byte[] value) {
00395     messageFields.setFieldValue(name, value);
00396   }
00397 
00398   @Override
00399   public byte getByte(String name) {
00400     return (Byte) messageFields.getFieldValue(name);
00401   }
00402 
00403   @Override
00404   public short getChar(String name) {
00405     return (Short) messageFields.getFieldValue(name);
00406   }
00407 
00408   @Override
00409   public void setByte(String name, byte value) {
00410     messageFields.setFieldValue(name, value);
00411   }
00412 
00413   @Override
00414   public void setChar(String name, short value) {
00415     messageFields.setFieldValue(name, value);
00416   }
00417 
00418   @Override
00419   public void setByteArray(String name, byte[] value) {
00420     messageFields.setFieldValue(name, value);
00421   }
00422 
00423   @Override
00424   public void setCharArray(String name, short[] value) {
00425     messageFields.setFieldValue(name, value);
00426   }
00427 
00428   @Override
00429   public byte[] getByteArray(String name) {
00430     return (byte[]) messageFields.getFieldValue(name);
00431   }
00432 
00433   @Override
00434   public short[] getCharArray(String name) {
00435     return (short[]) messageFields.getFieldValue(name);
00436   }
00437   
00438   @Override
00439   public ChannelBuffer getChannelBuffer(String name) {
00440     return (ChannelBuffer) messageFields.getFieldValue(name);
00441   }
00442 
00443   @Override
00444   public void setChannelBuffer(String name, ChannelBuffer value) {
00445     messageFields.setFieldValue(name, value);
00446   }
00447   
00448   @Override
00449   public Object getInstance() {
00450     return this;
00451   }
00452 
00453   @Override
00454   public String toString() {
00455     return String.format("MessageImpl<%s>", getType());
00456   }
00457 
00458   @Override
00459   public int hashCode() {
00460     final int prime = 31;
00461     int result = 1;
00462     result = prime * result + ((messageContext == null) ? 0 : messageContext.hashCode());
00463     result = prime * result + ((messageFields == null) ? 0 : messageFields.hashCode());
00464     return result;
00465   }
00466 
00467   @Override
00468   public boolean equals(Object obj) {
00469     if (this == obj)
00470       return true;
00471     if (obj == null)
00472       return false;
00473     if (!(obj instanceof GetInstance))
00474       return false;
00475     obj = ((GetInstance) obj).getInstance();
00476     if (getClass() != obj.getClass())
00477       return false;
00478     MessageImpl other = (MessageImpl) obj;
00479     if (messageContext == null) {
00480       if (other.messageContext != null)
00481         return false;
00482     } else if (!messageContext.equals(other.messageContext))
00483       return false;
00484     if (messageFields == null) {
00485       if (other.messageFields != null)
00486         return false;
00487     } else if (!messageFields.equals(other.messageFields))
00488       return false;
00489     return true;
00490   }
00491 }


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