LazyMessage.java
Go to the documentation of this file.
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.transport.queue;
00018 
00019 import com.google.common.annotations.VisibleForTesting;
00020 
00021 import org.jboss.netty.buffer.ChannelBuffer;
00022 import org.ros.message.MessageDeserializer;
00023 
00035 public class LazyMessage<T> {
00036 
00037   private final ChannelBuffer buffer;
00038   private final MessageDeserializer<T> deserializer;
00039   private final Object mutex;
00040 
00041   private T message;
00042 
00049   public LazyMessage(ChannelBuffer buffer, MessageDeserializer<T> deserializer) {
00050     this.buffer = buffer;
00051     this.deserializer = deserializer;
00052     mutex = new Object();
00053   }
00054 
00055   @VisibleForTesting
00056   LazyMessage(T message) {
00057     this(null, null);
00058     this.message = message;
00059   }
00060 
00064   public T get() {
00065     synchronized (mutex) {
00066       if (message != null) {
00067         return message;
00068       }
00069       message = deserializer.deserialize(buffer);
00070     }
00071     return message;
00072   }
00073 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49