Line data Source code
1 : // Copyright (C) 2020 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.base.Strings.emptyToNull; 18 : import static com.google.common.base.Strings.nullToEmpty; 19 : import static com.google.common.collect.ImmutableList.toImmutableList; 20 : import static com.google.common.collect.ImmutableSet.toImmutableSet; 21 : 22 : import com.google.common.base.Converter; 23 : import com.google.common.base.Enums; 24 : import com.google.gerrit.entities.NotifyConfig; 25 : import com.google.gerrit.server.cache.proto.Cache; 26 : 27 : /** Helper to (de)serialize values for caches. */ 28 : public class NotifyConfigSerializer { 29 1 : private static final Converter<String, NotifyConfig.Header> HEADER_CONVERTER = 30 1 : Enums.stringConverter(NotifyConfig.Header.class); 31 : 32 1 : private static final Converter<String, NotifyConfig.NotifyType> NOTIFY_TYPE_CONVERTER = 33 1 : Enums.stringConverter(NotifyConfig.NotifyType.class); 34 : 35 : public static NotifyConfig deserialize(Cache.NotifyConfigProto proto) { 36 : NotifyConfig.Builder builder = 37 1 : NotifyConfig.builder() 38 1 : .setName(emptyToNull(proto.getName())) 39 1 : .setNotify( 40 1 : proto.getTypeList().stream() 41 1 : .map(t -> NOTIFY_TYPE_CONVERTER.convert(t)) 42 1 : .collect(toImmutableSet())) 43 1 : .setFilter(emptyToNull(proto.getFilter())) 44 1 : .setHeader( 45 1 : proto.getHeader().isEmpty() ? null : HEADER_CONVERTER.convert(proto.getHeader())); 46 1 : proto.getGroupsList().stream() 47 1 : .map(GroupReferenceSerializer::deserialize) 48 1 : .forEach(g -> builder.addGroup(g)); 49 1 : proto.getAddressesList().stream() 50 1 : .map(AddressSerializer::deserialize) 51 1 : .forEach(a -> builder.addAddress(a)); 52 1 : return builder.build(); 53 : } 54 : 55 : public static Cache.NotifyConfigProto serialize(NotifyConfig autoValue) { 56 1 : return Cache.NotifyConfigProto.newBuilder() 57 1 : .setName(nullToEmpty(autoValue.getName())) 58 1 : .addAllType( 59 1 : autoValue.getNotify().stream() 60 1 : .map(t -> NOTIFY_TYPE_CONVERTER.reverse().convert(t)) 61 1 : .collect(toImmutableSet())) 62 1 : .setFilter(nullToEmpty(autoValue.getFilter())) 63 1 : .setHeader( 64 1 : autoValue.getHeader() == null 65 1 : ? "" 66 1 : : HEADER_CONVERTER.reverse().convert(autoValue.getHeader())) 67 1 : .addAllGroups( 68 1 : autoValue.getGroups().stream() 69 1 : .map(GroupReferenceSerializer::serialize) 70 1 : .collect(toImmutableSet())) 71 1 : .addAllAddresses( 72 1 : autoValue.getAddresses().stream() 73 1 : .map(AddressSerializer::serialize) 74 1 : .collect(toImmutableList())) 75 1 : .build(); 76 : } 77 : 78 : private NotifyConfigSerializer() {} 79 : }