Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / SimpleRegistrationStore.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 com.google.ipc.invalidation.common.DigestFunction;
20 import com.google.ipc.invalidation.common.ObjectIdDigestUtils;
21 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP;
22 import com.google.ipc.invalidation.util.Bytes;
23 import com.google.ipc.invalidation.util.InternalBase;
24 import com.google.ipc.invalidation.util.TextBuilder;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.SortedMap;
29 import java.util.TreeMap;
30
31 /**
32  * Simple, map-based implementation of {@link DigestStore}.
33  *
34  */
35 class SimpleRegistrationStore extends InternalBase implements DigestStore<ObjectIdP> {
36
37   /** All the registrations in the store mapped from the digest to the Object Id. */
38   private final SortedMap<Bytes, ObjectIdP> registrations = new TreeMap<Bytes, ObjectIdP>();
39
40   /** The function used to compute digests of objects. */
41   private final DigestFunction digestFunction;
42
43   /** The memoized digest of all objects in registrations. */
44   private Bytes digest;
45
46   SimpleRegistrationStore(DigestFunction digestFunction) {
47     this.digestFunction = digestFunction;
48     recomputeDigest();
49   }
50
51   @Override
52   public boolean add(ObjectIdP oid) {
53     if (registrations.put(ObjectIdDigestUtils.getDigest(oid.getSource(),
54         oid.getName().getByteArray(), digestFunction), oid) == null) {
55       recomputeDigest();
56       return true;
57     }
58     return false;
59   }
60
61   @Override
62   public Collection<ObjectIdP> add(Collection<ObjectIdP> oids) {
63     Collection<ObjectIdP> addedOids = new ArrayList<ObjectIdP>();
64     for (ObjectIdP oid : oids) {
65       if (registrations.put(ObjectIdDigestUtils.getDigest(oid.getSource(),
66           oid.getName().getByteArray(), digestFunction), oid) == null) {
67         // There was no previous value, so this is a new item.
68         addedOids.add(oid);
69       }
70     }
71     if (!addedOids.isEmpty()) {
72       // Only recompute the digest if we made changes.
73       recomputeDigest();
74     }
75     return addedOids;
76   }
77
78   @Override
79   public boolean remove(ObjectIdP oid) {
80     if (registrations.remove(ObjectIdDigestUtils.getDigest(oid.getSource(),
81         oid.getName().getByteArray(), digestFunction)) != null) {
82       recomputeDigest();
83       return true;
84     }
85     return false;
86   }
87
88   @Override
89   public Collection<ObjectIdP> remove(Collection<ObjectIdP> oids) {
90     Collection<ObjectIdP> removedOids = new ArrayList<ObjectIdP>();
91     for (ObjectIdP oid : oids) {
92       if (registrations.remove(ObjectIdDigestUtils.getDigest(oid.getSource(),
93           oid.getName().getByteArray(), digestFunction)) != null) {
94         removedOids.add(oid);
95       }
96     }
97     if (!removedOids.isEmpty()) {
98       // Only recompute the digest if we made changes.
99       recomputeDigest();
100     }
101     return removedOids;
102   }
103
104   @Override
105   public Collection<ObjectIdP> removeAll() {
106     Collection<ObjectIdP> result = new ArrayList<ObjectIdP>(registrations.values());
107     registrations.clear();
108     recomputeDigest();
109     return result;
110   }
111
112   @Override
113   public boolean contains(ObjectIdP oid) {
114     return registrations.containsKey(ObjectIdDigestUtils.getDigest(oid.getSource(),
115         oid.getName().getByteArray(), digestFunction));
116   }
117
118   @Override
119   public int size() {
120     return registrations.size();
121   }
122
123   @Override
124   public byte[] getDigest() {
125     return digest.getByteArray();
126   }
127
128   @Override
129   public Collection<ObjectIdP> getElements(byte[] oidDigestPrefix, int prefixLen) {
130     // We always return all the registrations and let the Ticl sort it out.
131     return registrations.values();
132   }
133
134   /** Recomputes the digests over all objects and sets {@code this.digest}. */
135   private void recomputeDigest() {
136     this.digest = ObjectIdDigestUtils.getDigest(registrations.keySet(), digestFunction);
137   }
138
139   @Override
140   public void toCompactString(TextBuilder builder) {
141     builder
142         .append("<SimpleRegistrationStore: registrations=")
143         .append(registrations.values())
144         .append(", digest=")
145         .append(digest)
146         .append(">");
147   }
148 }