LCOV - code coverage report
Current view: top level - server/util - IdGenerator.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 18 18 100.0 %
Date: 2022-11-19 15:00:39 Functions: 8 8 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2009 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.util;
      16             : 
      17             : import com.google.inject.Inject;
      18             : import com.google.inject.Singleton;
      19             : import java.util.Random;
      20             : import java.util.concurrent.atomic.AtomicInteger;
      21             : 
      22             : /** Simple class to produce 4 billion keys randomly distributed. */
      23             : @Singleton
      24             : public class IdGenerator {
      25             : 
      26             :   private final AtomicInteger gen;
      27             : 
      28             :   @Inject
      29         152 :   IdGenerator() {
      30         152 :     gen = new AtomicInteger(new Random().nextInt());
      31         152 :   }
      32             : 
      33             :   /** Produce the next identifier. */
      34             :   public int next() {
      35         146 :     return mix(gen.getAndIncrement());
      36             :   }
      37             : 
      38             :   private static final int salt = 0x9e3779b9;
      39             : 
      40             :   static int mix(int in) {
      41         146 :     return mix(salt, in);
      42             :   }
      43             : 
      44             :   /** A very simple bit permutation to mask a simple incrementer. */
      45             :   public static int mix(int salt, int in) {
      46         146 :     short v0 = hi16(in);
      47         146 :     short v1 = lo16(in);
      48         146 :     v0 += (short) (((v1 << 2) + 0 ^ v1) + (salt ^ (v1 >>> 3)) + 1);
      49         146 :     v1 += (short) (((v0 << 2) + 2 ^ v0) + (salt ^ (v0 >>> 3)) + 3);
      50         146 :     return result(v0, v1);
      51             :   }
      52             : 
      53             :   /* For testing only. */
      54             :   static int unmix(int in) {
      55           1 :     short v0 = hi16(in);
      56           1 :     short v1 = lo16(in);
      57           1 :     v1 -= (short) (((v0 << 2) + 2 ^ v0) + (salt ^ (v0 >>> 3)) + 3);
      58           1 :     v0 -= (short) (((v1 << 2) + 0 ^ v1) + (salt ^ (v1 >>> 3)) + 1);
      59           1 :     return result(v0, v1);
      60             :   }
      61             : 
      62             :   private static short hi16(int in) {
      63         146 :     return (short)
      64             :         ( //
      65             :         (in >>> 24 & 0xff)
      66             :             | //
      67             :             ((in >>> 16 & 0xff) << 8) //
      68             :         );
      69             :   }
      70             : 
      71             :   private static short lo16(int in) {
      72         146 :     return (short)
      73             :         ( //
      74             :         (in >>> 8 & 0xff)
      75             :             | //
      76             :             ((in & 0xff) << 8) //
      77             :         );
      78             :   }
      79             : 
      80             :   private static int result(short v0, short v1) {
      81         146 :     return ((v0 & 0xff) << 24)
      82             :         | //
      83             :         (((v0 >>> 8) & 0xff) << 16)
      84             :         | //
      85             :         ((v1 & 0xff) << 8)
      86             :         | //
      87             :         ((v1 >>> 8) & 0xff);
      88             :   }
      89             : }

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