LCOV - code coverage report
Current view: top level - server/cache/serialize - ObjectIdConverter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 9 9 100.0 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 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;
      16             : 
      17             : import static com.google.common.base.Preconditions.checkArgument;
      18             : 
      19             : import com.google.gerrit.git.ObjectIds;
      20             : import com.google.protobuf.ByteString;
      21             : import org.eclipse.jgit.lib.ObjectId;
      22             : 
      23             : /**
      24             :  * Helper for serializing {@link ObjectId} instances to/from protobuf fields.
      25             :  *
      26             :  * <p>Reuse a single instance's {@link #toByteString(ObjectId)} and {@link
      27             :  * #fromByteString(ByteString)} within a single {@link CacheSerializer#serialize} or {@link
      28             :  * CacheSerializer#deserialize} method body to minimize allocation of temporary buffers.
      29             :  *
      30             :  * <p><strong>Note:</strong> This class is not threadsafe. Instances must not be stored in {@link
      31             :  * CacheSerializer} fields if the serializer instances will be used from multiple threads.
      32             :  */
      33             : public class ObjectIdConverter {
      34             :   public static ObjectIdConverter create() {
      35         154 :     return new ObjectIdConverter();
      36             :   }
      37             : 
      38         154 :   private final byte[] buf = new byte[ObjectIds.LEN];
      39             : 
      40         154 :   private ObjectIdConverter() {}
      41             : 
      42             :   public ByteString toByteString(ObjectId id) {
      43         154 :     id.copyRawTo(buf, 0);
      44         154 :     return ByteString.copyFrom(buf);
      45             :   }
      46             : 
      47             :   public ObjectId fromByteString(ByteString in) {
      48         154 :     checkArgument(
      49         154 :         in.size() == ObjectIds.LEN, "expected ByteString of length %s: %s", ObjectIds.LEN, in);
      50         154 :     in.copyTo(buf, 0);
      51         154 :     return ObjectId.fromRaw(buf);
      52             :   }
      53             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750