Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.cache.serialize.entities; 16 : 17 : import static com.google.common.collect.ImmutableSet.toImmutableSet; 18 : 19 : import com.google.gerrit.entities.Account; 20 : import com.google.gerrit.entities.AccountGroup; 21 : import com.google.gerrit.entities.InternalGroup; 22 : import com.google.gerrit.server.cache.proto.Cache; 23 : import com.google.gerrit.server.cache.serialize.ObjectIdConverter; 24 : import java.time.Instant; 25 : 26 : /** Helper to (de)serialize values for caches. */ 27 : public class InternalGroupSerializer { 28 : public static InternalGroup deserialize(Cache.InternalGroupProto proto) { 29 : InternalGroup.Builder builder = 30 16 : InternalGroup.builder() 31 16 : .setId(AccountGroup.id(proto.getId())) 32 16 : .setNameKey(AccountGroup.nameKey(proto.getName())) 33 16 : .setOwnerGroupUUID(AccountGroup.uuid(proto.getOwnerGroupUuid())) 34 16 : .setVisibleToAll(proto.getIsVisibleToAll()) 35 16 : .setGroupUUID(AccountGroup.uuid(proto.getGroupUuid())) 36 16 : .setCreatedOn(Instant.ofEpochMilli(proto.getCreatedOn())) 37 16 : .setMembers( 38 16 : proto.getMembersIdsList().stream() 39 16 : .map(a -> Account.id(a)) 40 16 : .collect(toImmutableSet())) 41 16 : .setSubgroups( 42 16 : proto.getSubgroupUuidsList().stream() 43 16 : .map(s -> AccountGroup.uuid(s)) 44 16 : .collect(toImmutableSet())); 45 : 46 16 : if (!proto.getDescription().isEmpty()) { 47 16 : builder.setDescription(proto.getDescription()); 48 : } 49 : 50 16 : if (!proto.getRefState().isEmpty()) { 51 16 : builder.setRefState(ObjectIdConverter.create().fromByteString(proto.getRefState())); 52 : } 53 : 54 16 : return builder.build(); 55 : } 56 : 57 : public static Cache.InternalGroupProto serialize(InternalGroup autoValue) { 58 : Cache.InternalGroupProto.Builder builder = 59 16 : Cache.InternalGroupProto.newBuilder() 60 16 : .setId(autoValue.getId().get()) 61 16 : .setName(autoValue.getName()) 62 16 : .setOwnerGroupUuid(autoValue.getOwnerGroupUUID().get()) 63 16 : .setIsVisibleToAll(autoValue.isVisibleToAll()) 64 16 : .setGroupUuid(autoValue.getGroupUUID().get()) 65 16 : .setCreatedOn(autoValue.getCreatedOn().toEpochMilli()); 66 : 67 16 : autoValue.getMembers().stream().forEach(m -> builder.addMembersIds(m.get())); 68 16 : autoValue.getSubgroups().stream().forEach(s -> builder.addSubgroupUuids(s.get())); 69 : 70 16 : if (autoValue.getDescription() != null) { 71 16 : builder.setDescription(autoValue.getDescription()); 72 : } 73 : 74 16 : if (autoValue.getRefState() != null) { 75 16 : builder.setRefState(ObjectIdConverter.create().toByteString(autoValue.getRefState())); 76 : } 77 : 78 16 : return builder.build(); 79 : } 80 : 81 : private InternalGroupSerializer() {} 82 : }