| OLD | NEW |
| (Empty) | |
| 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.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; |
| 26 |
| 27 import java.util.ArrayList; |
| 28 import java.util.Collection; |
| 29 |
| 30 /** |
| 31 * Utilities to convert between {@link com.google.ipc.invalidation.util.ProtoWra
pper ProtoWrapper} |
| 32 * wrappers and externally-exposed types in the Ticl. |
| 33 */ |
| 34 public class ProtoWrapperConverter { |
| 35 |
| 36 /** |
| 37 * Converts an object id protocol buffer {@code objectId} to the |
| 38 * corresponding external type and returns it. |
| 39 */ |
| 40 public static ObjectId convertFromObjectIdProto(ObjectIdP objectIdProto) { |
| 41 Preconditions.checkNotNull(objectIdProto); |
| 42 return ObjectId.newInstance(objectIdProto.getSource(), objectIdProto.getName
().getByteArray()); |
| 43 } |
| 44 |
| 45 /** |
| 46 * Converts an object id {@code objectId} to the corresponding protocol buffer |
| 47 * and returns it. |
| 48 */ |
| 49 public static ObjectIdP convertToObjectIdProto(ObjectId objectId) { |
| 50 Preconditions.checkNotNull(objectId); |
| 51 return ObjectIdP.create(objectId.getSource(), new Bytes(objectId.getName()))
; |
| 52 } |
| 53 |
| 54 /** |
| 55 * Returns a list of {@link ObjectIdP} by converting each element of {@code ob
jectIds} to |
| 56 * an {@code ObjectIdP}. |
| 57 */ |
| 58 public static Collection<ObjectIdP> convertToObjectIdProtoCollection( |
| 59 Iterable<ObjectId> objectIds) { |
| 60 int expectedSize = (objectIds instanceof Collection) ? ((Collection<?>) obje
ctIds).size() : 1; |
| 61 ArrayList<ObjectIdP> objectIdPs = new ArrayList<ObjectIdP>(expectedSize); |
| 62 for (ObjectId objectId : objectIds) { |
| 63 objectIdPs.add(convertToObjectIdProto(objectId)); |
| 64 } |
| 65 return objectIdPs; |
| 66 } |
| 67 |
| 68 /** |
| 69 * Returns a list of {@link ObjectId} by converting each element of {@code obj
ectIdProtos} to |
| 70 * an {@code ObjectId}. |
| 71 */ |
| 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)); |
| 77 } |
| 78 return objectIds; |
| 79 } |
| 80 |
| 81 /** |
| 82 * Converts an invalidation protocol buffer {@code invalidation} to the |
| 83 * corresponding external object and returns it |
| 84 */ |
| 85 public static Invalidation convertFromInvalidationProto(InvalidationP invalida
tion) { |
| 86 Preconditions.checkNotNull(invalidation); |
| 87 ObjectId objectId = convertFromObjectIdProto(invalidation.getObjectId()); |
| 88 |
| 89 // No bridge arrival time in invalidation. |
| 90 return Invalidation.newInstance(objectId, invalidation.getVersion(), |
| 91 invalidation.hasPayload() ? invalidation.getPayload().getByteArray() : n
ull, |
| 92 invalidation.getIsTrickleRestart()); |
| 93 } |
| 94 |
| 95 /** |
| 96 * Converts an invalidation {@code invalidation} to the corresponding protocol |
| 97 * buffer and returns it. |
| 98 */ |
| 99 public static InvalidationP convertToInvalidationProto(Invalidation invalidati
on) { |
| 100 Preconditions.checkNotNull(invalidation); |
| 101 ObjectIdP objectId = convertToObjectIdProto(invalidation.getObjectId()); |
| 102 |
| 103 // Invalidations clients do not know about trickle restarts. Every invalidat
ion is allowed |
| 104 // to suppress earlier invalidations and acks implicitly acknowledge all pre
vious |
| 105 // invalidations. Therefore the correct semanantics are provided by setting
isTrickleRestart to |
| 106 // true. |
| 107 return CommonProtos.newInvalidationP(objectId, invalidation.getVersion(), |
| 108 invalidation.getIsTrickleRestartForInternalUse(), invalidation.getPayloa
d()); |
| 109 } |
| 110 |
| 111 private ProtoWrapperConverter() { // To prevent instantiation. |
| 112 } |
| 113 } |
| OLD | NEW |