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.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;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
33 * Utilities to convert between protobufs and externally-exposed types in the Ticl.
37 public class ProtoConverter {
40 * Converts an object id protocol buffer {@code objectId} to the
41 * corresponding external type and returns it.
43 public static ObjectId convertFromObjectIdProto(ObjectIdP objectIdProto) {
44 Preconditions.checkNotNull(objectIdProto);
45 return ObjectId.newInstance(objectIdProto.getSource(), objectIdProto.getName().toByteArray());
49 * Converts an object id {@code objectId} to the corresponding protocol buffer
53 public static ObjectIdP convertToObjectIdProto(ObjectId objectId) {
54 Preconditions.checkNotNull(objectId);
55 return CommonProtos2.newObjectIdP(objectId.getSource(),
56 ByteString.copyFrom(objectId.getName()));
60 * Returns a list of {@link ObjectIdP} by converting each element of {@code objectIds} to
61 * an {@code ObjectIdP}.
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));
72 * Returns a list of {@link ObjectId} by converting each element of {@code oidPs} to
73 * an {@code ObjectId}.
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()));
84 * Converts an invalidation protocol buffer {@code invalidation} to the
85 * corresponding external object and returns it
87 public static Invalidation convertFromInvalidationProto(InvalidationP invalidation) {
88 Preconditions.checkNotNull(invalidation);
89 ObjectId objectId = convertFromObjectIdProto(invalidation.getObjectId());
91 // No bridge arrival time in invalidation.
92 return Invalidation.newInstance(objectId, invalidation.getVersion(),
93 invalidation.hasPayload() ? invalidation.getPayload().toByteArray() : null,
94 invalidation.getIsTrickleRestart());
98 * Converts an invalidation {@code invalidation} to the corresponding protocol
99 * buffer and returns it.
101 public static InvalidationP convertToInvalidationProto(Invalidation invalidation) {
102 Preconditions.checkNotNull(invalidation);
103 ObjectIdP objectId = convertToObjectIdProto(invalidation.getObjectId());
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
109 return CommonProtos2.newInvalidationP(objectId, invalidation.getVersion(),
110 TrickleState.fromBoolean(invalidation.getIsTrickleRestartForInternalUse()),
111 invalidation.getPayload() == null ? null : ByteString.copyFrom(invalidation.getPayload()));
114 private ProtoConverter() { // To prevent instantiation.