Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / external / client / SystemResourcesBuilder.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.external.client;
18
19 import com.google.ipc.invalidation.external.client.SystemResources.Logger;
20 import com.google.ipc.invalidation.external.client.SystemResources.NetworkChannel;
21 import com.google.ipc.invalidation.external.client.SystemResources.Scheduler;
22 import com.google.ipc.invalidation.external.client.SystemResources.Storage;
23 import com.google.ipc.invalidation.ticl.BasicSystemResources;
24 import com.google.ipc.invalidation.util.Preconditions;
25
26
27 /**
28  * A builder to override some or all resource components in {@code SystemResources} . See
29  * discussion in {@code ResourceComponent} as well.
30  *
31  */
32
33   // The resources used for constructing the SystemResources in builder.
34 public class SystemResourcesBuilder {
35   private Scheduler internalScheduler;
36   private Scheduler listenerScheduler;
37   private Logger logger;
38   private NetworkChannel network;
39   private Storage storage;
40   private String platform;
41
42   /** If the build method has been called on this builder. */
43   private boolean sealed;
44
45   /** See specs at {@code DefaultResourcesFactory.createDefaultResourcesBuilder}. */
46   public SystemResourcesBuilder(Logger logger, Scheduler internalScheduler,
47       Scheduler listenerScheduler, NetworkChannel network, Storage storage) {
48     this.logger = logger;
49     this.internalScheduler = internalScheduler;
50     this.listenerScheduler = listenerScheduler;
51     this.network = network;
52     this.storage = storage;
53   }
54
55   /** Returns a new builder that shares all the resources of {@code builder} but is not sealed. */
56   public SystemResourcesBuilder(SystemResourcesBuilder builder) {
57     this.logger = builder.logger;
58     this.internalScheduler = builder.internalScheduler;
59     this.listenerScheduler = builder.listenerScheduler;
60     this.network = builder.network;
61     this.storage = builder.storage;
62     this.sealed = false;
63   }
64
65   /** Returns the internal scheduler. */
66   public Scheduler getInternalScheduler() {
67     return internalScheduler;
68   }
69
70   /** Returns the listener scheduler. */
71   public Scheduler getListenerScheduler() {
72     return listenerScheduler;
73   }
74
75   /** Returns the network channel. */
76   public NetworkChannel getNetwork() {
77     return network;
78   }
79
80   /** Returns the logger. */
81   public Logger getLogger() {
82     return logger;
83   }
84
85   /** Returns the storage. */
86   public Storage getStorage() {
87     return storage;
88   }
89
90   /**
91    * Sets the scheduler for scheduling internal events to be {@code internalScheduler}.
92    * <p>
93    * REQUIRES: {@link #build} has not been called.
94    */
95   public SystemResourcesBuilder setInternalScheduler(Scheduler internalScheduler) {
96     Preconditions.checkState(!sealed, "Builder's build method has already been called");
97     this.internalScheduler = internalScheduler;
98     return this;
99   }
100
101   /**
102    * Sets the scheduler for scheduling listener events to be {@code listenerScheduler}.
103    * <p>
104    * REQUIRES: {@link #build} has not been called.
105    */
106   public SystemResourcesBuilder setListenerScheduler(Scheduler listenerScheduler) {
107     Preconditions.checkState(!sealed, "Builder's build method has already been called");
108     this.listenerScheduler = listenerScheduler;
109     return this;
110   }
111
112   /**
113    * Sets the logger to be {@code logger}.
114    * <p>
115    * REQUIRES: {@link #build} has not been called.
116    */
117   public SystemResourcesBuilder setLogger(Logger logger) {
118     Preconditions.checkState(!sealed, "Builder's build method has already been called");
119     this.logger = logger;
120     return this;
121   }
122
123   /**
124    * Sets the network channel for communicating with the server to be {@code network}.
125    * <p>
126    * REQUIRES: {@link #build} has not been called.
127    */
128   public SystemResourcesBuilder setNetwork(NetworkChannel network) {
129     Preconditions.checkState(!sealed, "Builder's build method has already been called");
130     this.network = network;
131     return this;
132   }
133
134   /**
135    * Sets the persistence layer to be {@code storage}.
136    * <p>
137    * REQUIRES: {@link #build} has not been called.
138    */
139   public SystemResourcesBuilder setStorage(Storage storage) {
140     Preconditions.checkState(!sealed, "Builder's build method has already been called");
141     this.storage = storage;
142     return this;
143   }
144
145   /**
146    * Sets the platform to be {@code platform}.
147    * <p>
148    * REQUIRES: {@link #build} has not been called.
149    */
150   public SystemResourcesBuilder setPlatform(String platform) {
151     Preconditions.checkState(!sealed, "Builder's build method has already been called");
152     this.platform = platform;
153     return this;
154   }
155
156   /**
157    * Builds the {@code SystemResources} object with the given resource components and returns it.
158    * <p>
159    * Caller must not call any mutation method (on this SystemResourcesBuilder) after
160    * {@code build} has been called (i.e., build and the set* methods)
161    */
162   public SystemResources build() {
163     Preconditions.checkState(!sealed, "Builder's build method has already been called");
164     seal();
165     return new BasicSystemResources(logger, internalScheduler, listenerScheduler, network, storage,
166         platform);
167   }
168
169   /** Seals the builder so that no mutation method can be called on this. */
170   protected void seal() {
171     Preconditions.checkState(!sealed, "Builder's already sealed");
172     sealed = true;
173   }
174 }