aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/java/buck-ios-support/java/com/facebook/buck/apple/xcode/xcodeproj/PBXGroup.java
blob: dc636b7969cc9139b5767d01aac05e19826e7ea4 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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.xcodeproj;

import com.facebook.buck.apple.xcode.XcodeprojSerializer;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import com.google.common.base.Preconditions;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.annotation.Nullable;

/**
 * A collection of files in Xcode's virtual filesystem hierarchy.
 */
public class PBXGroup extends PBXReference {
  /**
   * Method by which group contents will be sorted.
   */
  public enum SortPolicy {
      /**
       * By name, in default Java sort order.
       */
      BY_NAME,

      /**
       * Group contents will not be sorted, and will remain in the
       * order they were added.
       */
      UNSORTED;
  }

  // Unfortunately, we can't determine this at constructor time, because CacheBuilder
  // calls our constructor and it's not easy to pass arguments to it.
  private SortPolicy sortPolicy;

  private final List<PBXReference> children;

  private final LoadingCache<String, PBXGroup> childGroupsByName;
  private final LoadingCache<String, PBXVariantGroup> childVariantGroupsByName;
  private final LoadingCache<SourceTreePath, PBXFileReference> fileReferencesBySourceTreePath;
  private final LoadingCache<SourceTreePath, XCVersionGroup> childVersionGroupsBySourceTreePath;

  public PBXGroup(String name, @Nullable String path, SourceTree sourceTree) {
    super(name, path, sourceTree);

    sortPolicy = SortPolicy.BY_NAME;
    children = Lists.newArrayList();

    childGroupsByName = CacheBuilder.newBuilder().build(
        new CacheLoader<String, PBXGroup>() {
          @Override
          public PBXGroup load(String key) throws Exception {
            PBXGroup group = new PBXGroup(key, null, SourceTree.GROUP);
            children.add(group);
            return group;
          }
        });

    childVariantGroupsByName = CacheBuilder.newBuilder().build(
        new CacheLoader<String, PBXVariantGroup>() {
          @Override
          public PBXVariantGroup load(String key) throws Exception {
            PBXVariantGroup group = new PBXVariantGroup(key, null, SourceTree.GROUP);
            children.add(group);
            return group;
          }
        });

    fileReferencesBySourceTreePath = CacheBuilder.newBuilder().build(
        new CacheLoader<SourceTreePath, PBXFileReference>() {
          @Override
          public PBXFileReference load(SourceTreePath key) throws Exception {
            PBXFileReference ref = new PBXFileReference(
                key.getPath().getFileName().toString(),
                key.getPath().toString(),
                key.getSourceTree());
            children.add(ref);
            return ref;
          }
        });

    childVersionGroupsBySourceTreePath = CacheBuilder.newBuilder().build(
        new CacheLoader<SourceTreePath, XCVersionGroup>() {
          @Override
          public XCVersionGroup load(SourceTreePath key) throws Exception {
            XCVersionGroup ref = new XCVersionGroup(
                key.getPath().getFileName().toString(),
                key.getPath().toString(),
                key.getSourceTree());
            children.add(ref);
            return ref;
          }
        });
  }

  public PBXGroup getOrCreateChildGroupByName(String name) {
    return childGroupsByName.getUnchecked(name);
  }

  public PBXVariantGroup getOrCreateChildVariantGroupByName(String name) {
    return childVariantGroupsByName.getUnchecked(name);
  }

  public PBXFileReference getOrCreateFileReferenceBySourceTreePath(SourceTreePath sourceTreePath) {
    return fileReferencesBySourceTreePath.getUnchecked(sourceTreePath);
  }

  public XCVersionGroup getOrCreateChildVersionGroupsBySourceTreePath(
      SourceTreePath sourceTreePath) {
    return childVersionGroupsBySourceTreePath.getUnchecked(sourceTreePath);
  }

  public List<PBXReference> getChildren() {
    return children;
  }

  public void setSortPolicy(SortPolicy sortPolicy) {
    this.sortPolicy = Preconditions.checkNotNull(sortPolicy);
  }

  public SortPolicy getSortPolicy() {
    return sortPolicy;
  }

  @Override
  public String isa() {
    return "PBXGroup";
  }

  @Override
  public void serializeInto(XcodeprojSerializer s) {
    super.serializeInto(s);

    if (sortPolicy == SortPolicy.BY_NAME) {
      Collections.sort(children, new Comparator<PBXReference>() {
          @Override
          public int compare(PBXReference o1, PBXReference o2) {
            return o1.getName().compareTo(o2.getName());
          }
        });
    }

    s.addField("children", children);
  }
}