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.concurrent;
00018
00019 import com.google.common.base.Preconditions;
00020
00021 import java.util.concurrent.CountDownLatch;
00022 import java.util.concurrent.TimeUnit;
00023 import java.util.concurrent.atomic.AtomicReference;
00024
00039 public class Holder<T> {
00040
00041 private final CountDownLatch latch;
00042
00043 private T value;
00044
00045 public static <T> Holder<T> newEmpty() {
00046 return new Holder<T>();
00047 }
00048
00049 private Holder() {
00050 latch = new CountDownLatch(1);
00051 value = null;
00052 }
00053
00054 public T set(T value) {
00055 Preconditions.checkState(this.value == null);
00056 this.value = value;
00057 latch.countDown();
00058 return value;
00059 }
00060
00061 public T get() {
00062 Preconditions.checkNotNull(value);
00063 return value;
00064 }
00065
00066 public void await() throws InterruptedException {
00067 latch.await();
00068 }
00069
00070 public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
00071 return latch.await(timeout, unit);
00072 }
00073
00074 @Override
00075 public boolean equals(Object obj) {
00076 if (this == obj)
00077 return true;
00078 return false;
00079 }
00080 }