This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake.rx.impl;
020
021import org.apache.reef.wake.rx.Observer;
022import org.apache.reef.wake.rx.Subject;
023
024import java.util.concurrent.TimeoutException;
025
026/**
027 * A class implementing {@code Subject<T>} with timeout.
028 *
029 * @param <T> a type of subject
030 * @deprecated in 0.14 as unused
031 */
032@Deprecated
033public class TimeoutSubject<T> implements Subject<T, T> {
034  private Thread timeBomb;
035  private Observer<T> destination;
036  private boolean finished;
037
038  public TimeoutSubject(final long timeout, final Observer<T> handler) {
039    this.finished = false;
040    this.destination = handler;
041    final TimeoutSubject<T> outer = this;
042    this.timeBomb = new Thread(new Runnable() {
043      @Override
044      public void run() {
045        final boolean finishedCopy;
046        synchronized (outer) {
047          if (!finished) {
048            try {
049              outer.wait(timeout);
050            } catch (final InterruptedException e) {
051              return;
052            }
053          }
054          finishedCopy = finished;
055          finished = true; // lock out the caller from putting event through now
056        }
057        if (!finishedCopy) {
058          destination.onError(new TimeoutException("TimeoutSubject expired"));
059        }
060      }
061    });
062    this.timeBomb.start();
063  }
064
065  @Override
066  public void onNext(final T value) {
067    final boolean wasFinished;
068    synchronized (this) {
069      wasFinished = finished;
070      if (!finished) {
071        this.notify();
072        finished = true;
073      }
074    }
075    if (!wasFinished) {
076      // TODO[JIRA unneeded due to deprecation]: change Subject to specify conversion to T
077      destination.onNext(value);
078      destination.onCompleted();
079    }
080  }
081
082  @Override
083  public void onError(final Exception error) {
084    this.timeBomb.interrupt();
085    destination.onError(error);
086  }
087
088  @Override
089  public void onCompleted() {
090    throw new IllegalStateException("Should not be called directly");
091  }
092
093}