CountDownSubscriberListener.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.node.topic;
00018 
00019 import org.ros.internal.node.topic.PublisherIdentifier;
00020 
00021 import org.ros.internal.node.CountDownRegistrantListener;
00022 
00023 import java.util.concurrent.CountDownLatch;
00024 import java.util.concurrent.TimeUnit;
00025 
00032 public class CountDownSubscriberListener<T> extends CountDownRegistrantListener<Subscriber<T>>
00033     implements SubscriberListener<T> {
00034 
00035   private final CountDownLatch shutdownLatch;
00036   private final CountDownLatch newPublisherLatch;
00037 
00041   public static <T> CountDownSubscriberListener<T> newDefault() {
00042     return newFromCounts(1, 1, 1, 1, 1);
00043   }
00044 
00057   public static <T> CountDownSubscriberListener<T> newFromCounts(
00058       int masterRegistrationSuccessCount, int masterRegistrationFailureCount,
00059       int masterUnregistrationSuccessCount, int masterUnregistrationFailureCount,
00060       int newSubscriberCount) {
00061     return new CountDownSubscriberListener<T>(new CountDownLatch(masterRegistrationSuccessCount),
00062         new CountDownLatch(masterRegistrationFailureCount), new CountDownLatch(
00063             masterUnregistrationSuccessCount),
00064         new CountDownLatch(masterUnregistrationFailureCount),
00065         new CountDownLatch(newSubscriberCount));
00066   }
00067 
00068   private CountDownSubscriberListener(CountDownLatch masterRegistrationSuccessLatch,
00069       CountDownLatch masterRegistrationFailureLatch,
00070       CountDownLatch masterUnregistrationSuccessLatch,
00071       CountDownLatch masterUnregistrationFailureLatch, CountDownLatch newPublisherLatch) {
00072     super(masterRegistrationSuccessLatch, masterRegistrationFailureLatch,
00073         masterUnregistrationSuccessLatch, masterUnregistrationFailureLatch);
00074     this.newPublisherLatch = newPublisherLatch;
00075     shutdownLatch = new CountDownLatch(1);
00076   }
00077 
00078   @Override
00079   public void onNewPublisher(Subscriber<T> subscriber, PublisherIdentifier publisherIdentifier) {
00080     newPublisherLatch.countDown();
00081   }
00082 
00083   @Override
00084   public void onShutdown(Subscriber<T> subscriber) {
00085     shutdownLatch.countDown();
00086   }
00087 
00093   public void awaitNewPublisher() throws InterruptedException {
00094     newPublisherLatch.await();
00095   }
00096 
00109   public boolean awaitNewPublisher(long timeout, TimeUnit unit) throws InterruptedException {
00110     return newPublisherLatch.await(timeout, unit);
00111   }
00112 
00118   public void awaitShutdown() throws InterruptedException {
00119     shutdownLatch.await();
00120   }
00121 
00133   public boolean awaitShutdown(long timeout, TimeUnit unit) throws InterruptedException {
00134     return shutdownLatch.await(timeout, unit);
00135   }
00136 }


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