2 * Copyright 2011 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.ipc.invalidation.ticl;
19 import static com.google.ipc.invalidation.external.client.SystemResources.Scheduler.NO_DELAY;
21 import com.google.ipc.invalidation.external.client.SystemResources;
22 import com.google.ipc.invalidation.external.client.SystemResources.Scheduler;
23 import com.google.ipc.invalidation.external.client.SystemResources.Storage;
24 import com.google.ipc.invalidation.external.client.types.Callback;
25 import com.google.ipc.invalidation.external.client.types.SimplePair;
26 import com.google.ipc.invalidation.external.client.types.Status;
27 import com.google.ipc.invalidation.util.NamedRunnable;
28 import com.google.ipc.invalidation.util.Preconditions;
31 * An implementation of the Storage resource that schedules the callbacks on the given scheduler
35 public class SafeStorage implements Storage {
37 /** The delegate to which the calls are forwarded. */
38 private final Storage delegate;
40 /** The scheduler on which the callbacks are scheduled. */
41 private Scheduler scheduler;
43 SafeStorage(Storage delegate) {
44 this.delegate = Preconditions.checkNotNull(delegate);
48 public void setSystemResources(SystemResources resources) {
49 this.scheduler = resources.getInternalScheduler();
53 public void writeKey(String key, byte[] value, final Callback<Status> done) {
54 delegate.writeKey(key, value, new Callback<Status>() {
56 public void accept(final Status status) {
57 scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.writeKey") {
68 public void readKey(String key, final Callback<SimplePair<Status, byte[]>> done) {
69 delegate.readKey(key, new Callback<SimplePair<Status, byte[]>>() {
71 public void accept(final SimplePair<Status, byte[]> result) {
72 scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.readKey") {
83 public void deleteKey(String key, final Callback<Boolean> done) {
84 delegate.deleteKey(key, new Callback<Boolean>() {
86 public void accept(final Boolean success) {
87 scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.deleteKey") {
98 public void readAllKeys(final Callback<SimplePair<Status, String>> keyCallback) {
99 delegate.readAllKeys(new Callback<SimplePair<Status, String>>() {
101 public void accept(final SimplePair<Status, String> keyResult) {
102 scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.readAllKeys") {
105 keyCallback.accept(keyResult);