Line data Source code
1 : // Copyright (C) 2019 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.entities.converter; 16 : 17 : import com.google.errorprone.annotations.Immutable; 18 : import com.google.gerrit.proto.Entities; 19 : import com.google.protobuf.Parser; 20 : import org.eclipse.jgit.lib.ObjectId; 21 : 22 : /** 23 : * Proto converter for {@code ObjectId}s. 24 : * 25 : * <p>This converter uses the hex representation of object IDs embedded in a wrapper proto type, 26 : * rather than a more parsimonious implementation (e.g. a raw byte array), for two reasons: 27 : * 28 : * <ul> 29 : * <li>Hex strings are easier to read and work with when reading and writing protos in text 30 : * formats, for example in test failure messages, or when using command-line tools. 31 : * <li>This maintains backwards wire compatibility with a pre-NoteDb implementation. 32 : * </ul> 33 : */ 34 104 : @Immutable 35 : public enum ObjectIdProtoConverter implements ProtoConverter<Entities.ObjectId, ObjectId> { 36 104 : INSTANCE; 37 : 38 : @Override 39 : public Entities.ObjectId toProto(ObjectId objectId) { 40 104 : return Entities.ObjectId.newBuilder().setName(objectId.name()).build(); 41 : } 42 : 43 : @Override 44 : public ObjectId fromProto(Entities.ObjectId proto) { 45 101 : return ObjectId.fromString(proto.getName()); 46 : } 47 : 48 : @Override 49 : public Parser<Entities.ObjectId> getParser() { 50 0 : return Entities.ObjectId.parser(); 51 : } 52 : }