aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/xcode-common/java/com/google/devtools/build/xcode/common/TargetDeviceFamily.java
blob: 3d369a15d1ebcfd1448f0faf55da370df4084bf4 (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
// Copyright 2014 Google Inc. All rights reserved.
//
// 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.google.devtools.build.xcode.common;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Possible values in the {@code TARGETED_DEVICE_FAMILY} build setting.
 */
public enum TargetDeviceFamily {
  IPAD, IPHONE;

  /**
   * Contains the values of the UIDeviceFamily plist info setting for each valid set of
   * TargetDeviceFamilies.
   */
  public static final Map<Set<TargetDeviceFamily>, List<Integer>> UI_DEVICE_FAMILY_VALUES =
      ImmutableMap.<Set<TargetDeviceFamily>, List<Integer>>builder()
          .put(ImmutableSet.of(TargetDeviceFamily.IPHONE), ImmutableList.of(1))
          .put(ImmutableSet.of(TargetDeviceFamily.IPAD), ImmutableList.of(2))
          .put(ImmutableSet.of(TargetDeviceFamily.IPHONE, TargetDeviceFamily.IPAD),
              ImmutableList.of(1, 2))
          .build();

  /**
   * Returns the name of the family as it appears in build rules.
   */
  public String getNameInRule() {
    return BY_NAME_IN_RULE.get(this);
  }

  /**
   * Returns the name of family which should be used in the bundlemerge control proto.
   */
  public String getBundleMergeName() {
    return BY_BUNDLE_MERGE_NAME.get(this);
  }

  private static final ImmutableBiMap<TargetDeviceFamily, String> BY_NAME_IN_RULE =
      ImmutableBiMap.<TargetDeviceFamily, String>of(IPAD, "ipad", IPHONE, "iphone");

  private static final ImmutableBiMap<TargetDeviceFamily, String> BY_BUNDLE_MERGE_NAME =
      ImmutableBiMap.<TargetDeviceFamily, String>of(IPAD, "IPAD", IPHONE, "IPHONE");

  private static Set<TargetDeviceFamily> fromNames(
      Iterable<String> names, Map<String, TargetDeviceFamily> mapping) {
    Set<TargetDeviceFamily> families = EnumSet.noneOf(TargetDeviceFamily.class);
    for (String name : names) {
      TargetDeviceFamily family = mapping.get(name);
      if (family == null) {
        throw new InvalidFamilyNameException(name);
      }
      if (!families.add(family)) {
        throw new RepeatedFamilyNameException(name);
      }
    }
    return families;
  }

  /**
   * Converts a sequence containing the strings returned by {@link #getBundleMergeName()} to a set
   * of instances of this enum.
   *
   * <p>If there are multiple items in the returned set, they are in enumeration order.
   *
   * @param names the names of the families
   * @throws InvalidFamilyNameException if some family name in the sequence was not recognized
   * @throws RepeatedFamilyNameException if some family name appeared in the sequence twice
   */
  public static Set<TargetDeviceFamily> fromBundleMergeNames(Iterable<String> names) {
    return fromNames(names, BY_BUNDLE_MERGE_NAME.inverse());
  }

  /**
   * Converts a sequence containing the strings returned by {@link #getNameInRule()} to a set of
   * instances of this enum.
   *
   * <p>If there are multiple items in the returned set, they are in enumeration order.
   *
   * @param names the names of the families
   * @throws InvalidFamilyNameException if some family name in the sequence was not recognized
   * @throws RepeatedFamilyNameException if some family name appeared in the sequence twice
   */
  public static Set<TargetDeviceFamily> fromNamesInRule(Iterable<String> names) {
    return fromNames(names, BY_NAME_IN_RULE.inverse());
  }

  /**
   * Converts the {@code TARGETED_DEVICE_FAMILY} setting in build settings to a set of
   * {@code TargetedDevice}s.
   */
  public static Set<TargetDeviceFamily> fromBuildSetting(String targetedDevice) {
    ImmutableSet.Builder<TargetDeviceFamily> result = ImmutableSet.builder();
    for (String numericSetting : Splitter.on(",").split(targetedDevice)) {
      numericSetting = numericSetting.trim();
      switch (numericSetting) {
        case "1":
          result.add(IPHONE);
          break;
        case "2":
          result.add(IPAD);
          break;
        default:
          throw new IllegalArgumentException(
              "Expect comma-separated list containing only '1' and/or '2' for "
              + "TARGETED_DEVICE_FAMILY: " + targetedDevice);
      }
    }
    return result.build();
  }
}