Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / ProtoConverter.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.common.base.Preconditions;
20 import com.google.ipc.invalidation.common.CommonProtos2;
21 import com.google.ipc.invalidation.common.TrickleState;
22 import com.google.ipc.invalidation.external.client.types.Invalidation;
23 import com.google.ipc.invalidation.external.client.types.ObjectId;
24 import com.google.protobuf.ByteString;
25 import com.google.protos.ipc.invalidation.ClientProtocol.InvalidationP;
26 import com.google.protos.ipc.invalidation.ClientProtocol.ObjectIdP;
27
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31
32 /**
33  * Utilities to convert between protobufs and externally-exposed types in the Ticl.
34  *
35  */
36
37 public class ProtoConverter {
38
39   /**
40    * Converts an object id protocol buffer {@code objectId} to the
41    * corresponding external type and returns it.
42    */
43   public static ObjectId convertFromObjectIdProto(ObjectIdP objectIdProto) {
44     Preconditions.checkNotNull(objectIdProto);
45     return ObjectId.newInstance(objectIdProto.getSource(), objectIdProto.getName().toByteArray());
46   }
47
48   /**
49    * Converts an object id {@code objectId} to the corresponding protocol buffer
50    * and returns it.
51    */
52   
53   public static ObjectIdP convertToObjectIdProto(ObjectId objectId) {
54     Preconditions.checkNotNull(objectId);
55     return CommonProtos2.newObjectIdP(objectId.getSource(),
56         ByteString.copyFrom(objectId.getName()));
57   }
58
59   /**
60    * Returns a list of {@link ObjectIdP} by converting each element of {@code objectIds} to
61    * an {@code ObjectIdP}.
62    */
63   public static List<ObjectIdP> convertToObjectIdProtoList(Collection<ObjectId> objectIds) {
64     List<ObjectIdP> objectIdPs = new ArrayList<ObjectIdP>(objectIds.size());
65     for (ObjectId objectId : objectIds) {
66       objectIdPs.add(ProtoConverter.convertToObjectIdProto(objectId));
67     }
68     return objectIdPs;
69   }
70
71   /**
72    * Returns a list of {@link ObjectId} by converting each element of {@code oidPs} to
73    * an {@code ObjectId}.
74    */
75   public static List<ObjectId> convertToObjectIdList(List<ObjectIdP> oidPs) {
76     List<ObjectId> objects = new ArrayList<ObjectId>(oidPs.size());
77     for (ObjectIdP oidP : oidPs) {
78       objects.add(ObjectId.newInstance(oidP.getSource(), oidP.getName().toByteArray()));
79     }
80     return objects;
81   }
82
83   /**
84    * Converts an invalidation protocol buffer {@code invalidation} to the
85    * corresponding external object and returns it
86    */
87   public static Invalidation convertFromInvalidationProto(InvalidationP invalidation) {
88     Preconditions.checkNotNull(invalidation);
89     ObjectId objectId = convertFromObjectIdProto(invalidation.getObjectId());
90
91     // No bridge arrival time in invalidation.
92     return Invalidation.newInstance(objectId, invalidation.getVersion(),
93         invalidation.hasPayload() ? invalidation.getPayload().toByteArray() : null,
94             invalidation.getIsTrickleRestart());
95   }
96
97   /**
98    * Converts an invalidation {@code invalidation} to the corresponding protocol
99    * buffer and returns it.
100    */
101   public static InvalidationP convertToInvalidationProto(Invalidation invalidation) {
102     Preconditions.checkNotNull(invalidation);
103     ObjectIdP objectId = convertToObjectIdProto(invalidation.getObjectId());
104
105     // Invalidations clients do not know about trickle restarts. Every invalidation is allowed
106     // to suppress earlier invalidations and acks implicitly acknowledge all previous
107     // invalidations. Therefore the correct semanantics are provided by setting isTrickleRestart to
108     // true.
109     return CommonProtos2.newInvalidationP(objectId, invalidation.getVersion(),
110         TrickleState.fromBoolean(invalidation.getIsTrickleRestartForInternalUse()),
111         invalidation.getPayload() == null ? null : ByteString.copyFrom(invalidation.getPayload()));
112   }
113
114   private ProtoConverter() { // To prevent instantiation.
115   }
116 }