|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.sync.repositories.delegates; |
|
6 |
|
7 import java.util.concurrent.ExecutorService; |
|
8 |
|
9 import org.mozilla.gecko.sync.repositories.RepositorySession; |
|
10 |
|
11 public class DeferredRepositorySessionBeginDelegate implements RepositorySessionBeginDelegate { |
|
12 private RepositorySessionBeginDelegate inner; |
|
13 private ExecutorService executor; |
|
14 public DeferredRepositorySessionBeginDelegate(final RepositorySessionBeginDelegate inner, final ExecutorService executor) { |
|
15 this.inner = inner; |
|
16 this.executor = executor; |
|
17 } |
|
18 |
|
19 @Override |
|
20 public void onBeginSucceeded(final RepositorySession session) { |
|
21 executor.execute(new Runnable() { |
|
22 @Override |
|
23 public void run() { |
|
24 inner.onBeginSucceeded(session); |
|
25 } |
|
26 }); |
|
27 } |
|
28 |
|
29 @Override |
|
30 public void onBeginFailed(final Exception ex) { |
|
31 executor.execute(new Runnable() { |
|
32 @Override |
|
33 public void run() { |
|
34 inner.onBeginFailed(ex); |
|
35 } |
|
36 }); |
|
37 } |
|
38 |
|
39 @Override |
|
40 public RepositorySessionBeginDelegate deferredBeginDelegate(ExecutorService newExecutor) { |
|
41 if (newExecutor == executor) { |
|
42 return this; |
|
43 } |
|
44 throw new IllegalArgumentException("Can't re-defer this delegate."); |
|
45 } |
|
46 } |