aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/buck-ios-support/java/com/facebook/buck/apple/xcode/GidGenerator.java
blob: 03395a05baf7018d210cbe9460b430460d9668f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
 * Copyright 2013-present Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.facebook.buck.apple.xcode;

import com.google.common.collect.Sets;

import java.util.Set;

/**
 * Generator for Global ID (GID) which are present on every xcode project object.
 *
 * The GID is a 96 bit identifier that's unique on a per-project basis.
 */
public class GidGenerator {
  private final Set<String> generatedAndReservedIds;

  public GidGenerator(Set<String> reservedIds) {
    generatedAndReservedIds = Sets.newHashSet(reservedIds);
  }

  /**
   * Generate a stable GID based on the class name and hash of some object info.
   *
   * GIDs generated this way will be in the form of
   *  {@code <class-name-hash-32> <obj-hash-32> <counter-32>}
   */
  public String generateGid(String pbxClassName, int hash) {
    int counter = 0;
    String gid;
    do {
      gid = String.format("%08X%08X%08X", pbxClassName.hashCode(), hash, counter++);
    } while (generatedAndReservedIds.contains(gid));
    generatedAndReservedIds.add(gid);
    return gid;
  }
}