1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.storage.repository.git;
18  
19  final class TagUtil {
20  
21      private static final String[] TABLE;
22  
23      static {
24          TABLE = new String[256];
25          for (int i = 0; i < TABLE.length; i++) {
26              TABLE[i] = String.format("%02x/", i);
27          }
28      }
29  
30      /**
31       * Generates a string by joining hex representation for each byte in {@code majorRevision} with '/' in
32       * little endian order.
33       * <p>
34       * For example,
35       * <ul>
36       * <li>0x0d0c0b0a -> '0a/0b/0c/0d/'</li>
37       * <li>0x00000b0a -> '0a/0b/'</li>
38       * <li>0x00000001 -> '01/'</li>
39       * </ul>
40       * </p>
41       *
42       * @throws IllegalArgumentException if {@code majorRevision} is a zero or a negative value.
43       */
44      static String byteHexDirName(int majorRevision) {
45  
46          if (majorRevision <= 0) {
47              throw new IllegalArgumentException("invalid majorRevision " + majorRevision + " (expected: > 0)");
48          }
49  
50          final StringBuilder sb = new StringBuilder(16);
51          final int shift = 8;
52  
53          do {
54              sb.append(TABLE[majorRevision & 0xFF]);
55              majorRevision >>>= shift;
56          } while (majorRevision != 0);
57          return sb.toString();
58      }
59  
60      private TagUtil() {}
61  }