Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / SafeStorage.java
1 /*
2  * Copyright 2011 Google Inc.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.ipc.invalidation.ticl;
18
19 import static com.google.ipc.invalidation.external.client.SystemResources.Scheduler.NO_DELAY;
20
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;
29
30 /**
31  * An implementation of the Storage resource that schedules the callbacks on the given scheduler
32  * thread.
33  *
34  */
35 public class SafeStorage implements Storage {
36
37   /** The delegate to which the calls are forwarded. */
38   private final Storage delegate;
39
40   /** The scheduler on which the callbacks are scheduled. */
41   private Scheduler scheduler;
42
43   SafeStorage(Storage delegate) {
44     this.delegate = Preconditions.checkNotNull(delegate);
45   }
46
47   @Override
48   public void setSystemResources(SystemResources resources) {
49     this.scheduler = resources.getInternalScheduler();
50   }
51
52   @Override
53   public void writeKey(String key, byte[] value, final Callback<Status> done) {
54     delegate.writeKey(key, value, new Callback<Status>() {
55       @Override
56       public void accept(final Status status) {
57         scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.writeKey") {
58           @Override
59           public void run() {
60             done.accept(status);
61           }
62         });
63       }
64     });
65   }
66
67   @Override
68   public void readKey(String key, final Callback<SimplePair<Status, byte[]>> done) {
69     delegate.readKey(key, new Callback<SimplePair<Status, byte[]>>() {
70       @Override
71       public void accept(final SimplePair<Status, byte[]> result) {
72         scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.readKey") {
73           @Override
74           public void run() {
75             done.accept(result);
76           }
77         });
78       }
79     });
80   }
81
82   @Override
83   public void deleteKey(String key, final Callback<Boolean> done) {
84     delegate.deleteKey(key, new Callback<Boolean>() {
85       @Override
86       public void accept(final Boolean success) {
87         scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.deleteKey") {
88           @Override
89           public void run() {
90             done.accept(success);
91           }
92         });
93       }
94     });
95   }
96
97   @Override
98   public void readAllKeys(final Callback<SimplePair<Status, String>> keyCallback) {
99     delegate.readAllKeys(new Callback<SimplePair<Status, String>>() {
100       @Override
101       public void accept(final SimplePair<Status, String> keyResult) {
102         scheduler.schedule(NO_DELAY, new NamedRunnable("SafeStorage.readAllKeys") {
103           @Override
104           public void run() {
105             keyCallback.accept(keyResult);
106           }
107         });
108       }
109     });
110   }
111 }