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.collect.ImmutableList.toImmutableList; 18 : 19 : import com.google.common.base.Converter; 20 : import com.google.common.base.Enums; 21 : import com.google.common.base.Strings; 22 : import com.google.common.collect.ImmutableList; 23 : import com.google.common.primitives.Shorts; 24 : import com.google.gerrit.entities.LabelFunction; 25 : import com.google.gerrit.entities.LabelType; 26 : import com.google.gerrit.server.cache.proto.Cache; 27 : import java.util.Optional; 28 : 29 : /** Helper to (de)serialize values for caches. */ 30 : public class LabelTypeSerializer { 31 16 : private static final Converter<String, LabelFunction> FUNCTION_CONVERTER = 32 16 : Enums.stringConverter(LabelFunction.class); 33 : 34 : public static LabelType deserialize(Cache.LabelTypeProto proto) { 35 16 : return LabelType.builder( 36 16 : proto.getName(), 37 16 : proto.getValuesList().stream() 38 16 : .map(LabelValueSerializer::deserialize) 39 16 : .collect(toImmutableList())) 40 16 : .setDescription(Optional.of(proto.getDescription())) 41 16 : .setFunction(FUNCTION_CONVERTER.convert(proto.getFunction())) 42 16 : .setAllowPostSubmit(proto.getAllowPostSubmit()) 43 16 : .setIgnoreSelfApproval(proto.getIgnoreSelfApproval()) 44 16 : .setDefaultValue(Shorts.saturatedCast(proto.getDefaultValue())) 45 16 : .setCopyCondition(Strings.emptyToNull(proto.getCopyCondition())) 46 16 : .setMaxNegative(Shorts.saturatedCast(proto.getMaxNegative())) 47 16 : .setMaxPositive(Shorts.saturatedCast(proto.getMaxPositive())) 48 16 : .setRefPatterns(proto.getRefPatternsList()) 49 16 : .setCanOverride(proto.getCanOverride()) 50 16 : .build(); 51 : } 52 : 53 : public static Cache.LabelTypeProto serialize(LabelType autoValue) { 54 16 : return Cache.LabelTypeProto.newBuilder() 55 16 : .setName(autoValue.getName()) 56 16 : .addAllValues( 57 16 : autoValue.getValues().stream() 58 16 : .map(LabelValueSerializer::serialize) 59 16 : .collect(toImmutableList())) 60 16 : .setDescription(autoValue.getDescription().orElse("")) 61 16 : .setFunction(FUNCTION_CONVERTER.reverse().convert(autoValue.getFunction())) 62 16 : .setCopyCondition(autoValue.getCopyCondition().orElse("")) 63 16 : .setAllowPostSubmit(autoValue.isAllowPostSubmit()) 64 16 : .setIgnoreSelfApproval(autoValue.isIgnoreSelfApproval()) 65 16 : .setDefaultValue(Shorts.saturatedCast(autoValue.getDefaultValue())) 66 16 : .setMaxNegative(Shorts.saturatedCast(autoValue.getMaxNegative())) 67 16 : .setMaxPositive(Shorts.saturatedCast(autoValue.getMaxPositive())) 68 16 : .addAllRefPatterns( 69 16 : autoValue.getRefPatterns() == null ? ImmutableList.of() : autoValue.getRefPatterns()) 70 16 : .setCanOverride(autoValue.isCanOverride()) 71 16 : .build(); 72 : } 73 : 74 : private LabelTypeSerializer() {} 75 : }