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 com.google.gerrit.entities.ConfiguredMimeTypes; 18 : import com.google.gerrit.server.cache.proto.Cache; 19 : import java.util.regex.PatternSyntaxException; 20 : import org.eclipse.jgit.errors.InvalidPatternException; 21 : 22 0 : public class ConfiguredMimeTypeSerializer { 23 : public static ConfiguredMimeTypes.TypeMatcher deserialize(Cache.ConfiguredMimeTypeProto proto) { 24 : try { 25 1 : return proto.getIsRegularExpression() 26 1 : ? new ConfiguredMimeTypes.ReType(proto.getType(), proto.getPattern()) 27 1 : : new ConfiguredMimeTypes.FnType(proto.getType(), proto.getPattern()); 28 0 : } catch (PatternSyntaxException | InvalidPatternException e) { 29 0 : throw new IllegalStateException(e); 30 : } 31 : } 32 : 33 : public static Cache.ConfiguredMimeTypeProto serialize(ConfiguredMimeTypes.TypeMatcher value) { 34 1 : return Cache.ConfiguredMimeTypeProto.newBuilder() 35 1 : .setType(value.getType()) 36 1 : .setPattern(value.getPattern()) 37 1 : .setIsRegularExpression(value instanceof ConfiguredMimeTypes.ReType) 38 1 : .build(); 39 : } 40 : }