mobile/android/base/sync/repositories/delegates/DeferredRepositorySessionStoreDelegate.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/sync/repositories/delegates/DeferredRepositorySessionStoreDelegate.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,57 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.sync.repositories.delegates;
     1.9 +
    1.10 +import java.util.concurrent.ExecutorService;
    1.11 +
    1.12 +public class DeferredRepositorySessionStoreDelegate implements
    1.13 +    RepositorySessionStoreDelegate {
    1.14 +  protected final RepositorySessionStoreDelegate inner;
    1.15 +  protected final ExecutorService                executor;
    1.16 +
    1.17 +  public DeferredRepositorySessionStoreDelegate(
    1.18 +      RepositorySessionStoreDelegate inner, ExecutorService executor) {
    1.19 +    this.inner = inner;
    1.20 +    this.executor = executor;
    1.21 +  }
    1.22 +
    1.23 +  @Override
    1.24 +  public void onRecordStoreSucceeded(final String guid) {
    1.25 +    executor.execute(new Runnable() {
    1.26 +      @Override
    1.27 +      public void run() {
    1.28 +        inner.onRecordStoreSucceeded(guid);
    1.29 +      }
    1.30 +    });
    1.31 +  }
    1.32 +
    1.33 +  @Override
    1.34 +  public void onRecordStoreFailed(final Exception ex, final String guid) {
    1.35 +    executor.execute(new Runnable() {
    1.36 +      @Override
    1.37 +      public void run() {
    1.38 +        inner.onRecordStoreFailed(ex, guid);
    1.39 +      }
    1.40 +    });
    1.41 +  }
    1.42 +
    1.43 +  @Override
    1.44 +  public RepositorySessionStoreDelegate deferredStoreDelegate(ExecutorService newExecutor) {
    1.45 +    if (newExecutor == executor) {
    1.46 +      return this;
    1.47 +    }
    1.48 +    throw new IllegalArgumentException("Can't re-defer this delegate.");
    1.49 +  }
    1.50 +
    1.51 +  @Override
    1.52 +  public void onStoreCompleted(final long storeEnd) {
    1.53 +    executor.execute(new Runnable() {
    1.54 +      @Override
    1.55 +      public void run() {
    1.56 +        inner.onStoreCompleted(storeEnd);
    1.57 +      }
    1.58 +    });
    1.59 +  }
    1.60 +}

mercurial