001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.concurrent;
016    
017    import java.util.concurrent.ExecutionException;
018    import java.util.concurrent.Future;
019    import java.util.concurrent.TimeUnit;
020    import java.util.concurrent.TimeoutException;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public abstract class NoticeableFutureConverter<T, V>
026            extends FutureConverter<T, V> implements NoticeableFuture<T> {
027    
028            public NoticeableFutureConverter(NoticeableFuture<V> noticeableFuture) {
029                    super(noticeableFuture);
030    
031                    _defaultNoticeableFuture = new DefaultNoticeableFuture<>();
032    
033                    noticeableFuture.addFutureListener(
034                            new FutureListener<V>() {
035    
036                                    @Override
037                                    public void complete(Future<V> future) {
038                                            if (future.isCancelled()) {
039                                                    _defaultNoticeableFuture.cancel(true);
040    
041                                                    return;
042                                            }
043    
044                                            try {
045                                                    _defaultNoticeableFuture.set(convert(future.get()));
046                                            }
047                                            catch (Throwable t) {
048                                                    if (t instanceof ExecutionException) {
049                                                            t = t.getCause();
050                                                    }
051    
052                                                    _defaultNoticeableFuture.setException(t);
053                                            }
054                                    }
055    
056                            });
057            }
058    
059            @Override
060            public boolean addFutureListener(FutureListener<T> futureListener) {
061                    return _defaultNoticeableFuture.addFutureListener(futureListener);
062            }
063    
064            @Override
065            public T get() throws ExecutionException, InterruptedException {
066                    return _defaultNoticeableFuture.get();
067            }
068    
069            @Override
070            public T get(long timeout, TimeUnit timeUnit)
071                    throws ExecutionException, InterruptedException, TimeoutException {
072    
073                    return _defaultNoticeableFuture.get(timeout, timeUnit);
074            }
075    
076            @Override
077            public boolean removeFutureListener(FutureListener<T> futureListener) {
078                    return _defaultNoticeableFuture.removeFutureListener(futureListener);
079            }
080    
081            private final DefaultNoticeableFuture<T> _defaultNoticeableFuture;
082    
083    }