/** * Disposes the underlying disposable. * * @see nju.seg.zhangyf.base.IDisposable#DisposeIfNotNull() */ @Override public void Dispose() { IDisposable disposable = this._Current.getAndSet(CLEARED_DISPOSABLE); Disposables.DisposeIfNonnull(disposable); // if (disposable != null) { // disposable.Dispose(); // } }
/** * Sets the underlying disposable. If the FutureDisposable has already been assigned then it will * throw an InvalidOperationException. * * @param value * @throws IllegalStateException Thrown if the SingleAssignmentDisposable has already been * assigned to. */ public void SetDisposable(IDisposable value) { if (this._Current.compareAndSet(NOT_SET_DISPOSABLE, value)) { // Dispose尚未执行,且尚未设置过value,则仅将_Current设为value return; } else { if (this._Current.get() != CLEARED_DISPOSABLE) { throw new IllegalStateException( "The SingleAssignmentDisposable has already been assigned to"); } else { Disposables.DisposeIfNonnull(value); // if (value != null) { // value.Dispose(); // } } } }
@Test(expected = NoSuchElementException.class) public void testDispose() { final Object obj = new Object(); final UnmodifiableDisposableIterator<Object> iter = LazyIterators.NewAutoDisposeLazyIterator( new Func1<Object, Tuple2<Object, Object>>() { @Override public Tuple2<Object, Object> DoFunc(final Object input) { return Tuple2.NewTuple(obj, obj); } }, obj, Disposables.CreateDisposable( new Action0() { @Override public void accept() { System.out.println("Dispose methods executed"); } })); iter.next(); iter.next(); System.out.println("Do next."); iter.Dispose(); System.out.println("Dispose."); // throw NoSuchElementException; iter.next(); System.out.println("Next after Dispose."); }