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 com.google.ipc.invalidation.external.client.types.Invalidation;
20 import com.google.ipc.invalidation.external.client.types.ObjectId;
21 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.InvalidationP;
22 import com.google.ipc.invalidation.ticl.proto.ClientProtocol.ObjectIdP;
23 import com.google.ipc.invalidation.ticl.proto.CommonProtos;
24 import com.google.ipc.invalidation.util.Bytes;
25 import com.google.ipc.invalidation.util.Preconditions;
27 import java.util.ArrayList;
28 import java.util.Collection;
31 * Utilities to convert between {@link com.google.ipc.invalidation.util.ProtoWrapper ProtoWrapper}
32 * wrappers and externally-exposed types in the Ticl.
34 public class ProtoWrapperConverter {
37 * Converts an object id protocol buffer {@code objectId} to the
38 * corresponding external type and returns it.
40 public static ObjectId convertFromObjectIdProto(ObjectIdP objectIdProto) {
41 Preconditions.checkNotNull(objectIdProto);
42 return ObjectId.newInstance(objectIdProto.getSource(), objectIdProto.getName().getByteArray());
46 * Converts an object id {@code objectId} to the corresponding protocol buffer
49 public static ObjectIdP convertToObjectIdProto(ObjectId objectId) {
50 Preconditions.checkNotNull(objectId);
51 return ObjectIdP.create(objectId.getSource(), new Bytes(objectId.getName()));
55 * Returns a list of {@link ObjectIdP} by converting each element of {@code objectIds} to
56 * an {@code ObjectIdP}.
58 public static Collection<ObjectIdP> convertToObjectIdProtoCollection(
59 Iterable<ObjectId> objectIds) {
60 int expectedSize = (objectIds instanceof Collection) ? ((Collection<?>) objectIds).size() : 1;
61 ArrayList<ObjectIdP> objectIdPs = new ArrayList<ObjectIdP>(expectedSize);
62 for (ObjectId objectId : objectIds) {
63 objectIdPs.add(convertToObjectIdProto(objectId));
69 * Returns a list of {@link ObjectId} by converting each element of {@code objectIdProtos} to
70 * an {@code ObjectId}.
72 public static Collection<ObjectId> convertFromObjectIdProtoCollection(
73 Collection<ObjectIdP> objectIdProtos) {
74 ArrayList<ObjectId> objectIds = new ArrayList<ObjectId>(objectIdProtos.size());
75 for (ObjectIdP objectIdProto : objectIdProtos) {
76 objectIds.add(convertFromObjectIdProto(objectIdProto));
82 * Converts an invalidation protocol buffer {@code invalidation} to the
83 * corresponding external object and returns it
85 public static Invalidation convertFromInvalidationProto(InvalidationP invalidation) {
86 Preconditions.checkNotNull(invalidation);
87 ObjectId objectId = convertFromObjectIdProto(invalidation.getObjectId());
89 // No bridge arrival time in invalidation.
90 return Invalidation.newInstance(objectId, invalidation.getVersion(),
91 invalidation.hasPayload() ? invalidation.getPayload().getByteArray() : null,
92 invalidation.getIsTrickleRestart());
96 * Converts an invalidation {@code invalidation} to the corresponding protocol
97 * buffer and returns it.
99 public static InvalidationP convertToInvalidationProto(Invalidation invalidation) {
100 Preconditions.checkNotNull(invalidation);
101 ObjectIdP objectId = convertToObjectIdProto(invalidation.getObjectId());
103 // Invalidations clients do not know about trickle restarts. Every invalidation is allowed
104 // to suppress earlier invalidations and acks implicitly acknowledge all previous
105 // invalidations. Therefore the correct semanantics are provided by setting isTrickleRestart to
107 return CommonProtos.newInvalidationP(objectId, invalidation.getVersion(),
108 invalidation.getIsTrickleRestartForInternalUse(), invalidation.getPayload());
111 private ProtoWrapperConverter() { // To prevent instantiation.