Holder.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.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 }


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