|
3 | 3 | */ |
4 | 4 | package com.apideck.unify.models.components; |
5 | 5 |
|
| 6 | +import com.fasterxml.jackson.annotation.JsonCreator; |
6 | 7 | import com.fasterxml.jackson.annotation.JsonValue; |
| 8 | +import java.lang.Override; |
7 | 9 | import java.lang.String; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.LinkedHashMap; |
| 12 | +import java.util.Map; |
8 | 13 | import java.util.Objects; |
9 | 14 | import java.util.Optional; |
10 | 15 |
|
11 | | -public enum ConnectorSettingType { |
12 | | - TEXT("text"), |
13 | | - CHECKBOX("checkbox"), |
14 | | - TEL("tel"), |
15 | | - EMAIL("email"), |
16 | | - URL("url"), |
17 | | - TEXTAREA("textarea"), |
18 | | - SELECT("select"), |
19 | | - FILTERED_SELECT("filtered-select"), |
20 | | - MULTI_SELECT("multi-select"), |
21 | | - DATETIME("datetime"), |
22 | | - DATE("date"), |
23 | | - TIME("time"), |
24 | | - NUMBER("number"), |
25 | | - PASSWORD("password"); |
| 16 | +/** |
| 17 | + * Wrapper for an "open" enum that can handle unknown values from API responses |
| 18 | + * without runtime errors. Instances are immutable singletons with reference equality. |
| 19 | + * Use {@code asEnum()} for switch expressions. |
| 20 | + */ |
| 21 | +public class ConnectorSettingType { |
| 22 | + |
| 23 | + public static final ConnectorSettingType TEXT = new ConnectorSettingType("text"); |
| 24 | + public static final ConnectorSettingType CHECKBOX = new ConnectorSettingType("checkbox"); |
| 25 | + public static final ConnectorSettingType TEL = new ConnectorSettingType("tel"); |
| 26 | + public static final ConnectorSettingType EMAIL = new ConnectorSettingType("email"); |
| 27 | + public static final ConnectorSettingType URL = new ConnectorSettingType("url"); |
| 28 | + public static final ConnectorSettingType TEXTAREA = new ConnectorSettingType("textarea"); |
| 29 | + public static final ConnectorSettingType SELECT = new ConnectorSettingType("select"); |
| 30 | + public static final ConnectorSettingType FILTERED_SELECT = new ConnectorSettingType("filtered-select"); |
| 31 | + public static final ConnectorSettingType MULTI_SELECT = new ConnectorSettingType("multi-select"); |
| 32 | + public static final ConnectorSettingType DATETIME = new ConnectorSettingType("datetime"); |
| 33 | + public static final ConnectorSettingType DATE = new ConnectorSettingType("date"); |
| 34 | + public static final ConnectorSettingType TIME = new ConnectorSettingType("time"); |
| 35 | + public static final ConnectorSettingType NUMBER = new ConnectorSettingType("number"); |
| 36 | + public static final ConnectorSettingType PASSWORD = new ConnectorSettingType("password"); |
| 37 | + |
| 38 | + // This map will grow whenever a Color gets created with a new |
| 39 | + // unrecognized value (a potential memory leak if the user is not |
| 40 | + // careful). Keep this field lower case to avoid clashing with |
| 41 | + // generated member names which will always be upper cased (Java |
| 42 | + // convention) |
| 43 | + private static final Map<String, ConnectorSettingType> values = createValuesMap(); |
| 44 | + private static final Map<String, ConnectorSettingTypeEnum> enums = createEnumsMap(); |
26 | 45 |
|
27 | | - @JsonValue |
28 | 46 | private final String value; |
29 | 47 |
|
30 | | - ConnectorSettingType(String value) { |
| 48 | + private ConnectorSettingType(String value) { |
31 | 49 | this.value = value; |
32 | 50 | } |
33 | | - |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns a ConnectorSettingType with the given value. For a specific value the |
| 54 | + * returned object will always be a singleton so reference equality |
| 55 | + * is satisfied when the values are the same. |
| 56 | + * |
| 57 | + * @param value value to be wrapped as ConnectorSettingType |
| 58 | + */ |
| 59 | + @JsonCreator |
| 60 | + public static ConnectorSettingType of(String value) { |
| 61 | + synchronized (ConnectorSettingType.class) { |
| 62 | + return values.computeIfAbsent(value, v -> new ConnectorSettingType(v)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @JsonValue |
34 | 67 | public String value() { |
35 | 68 | return value; |
36 | 69 | } |
| 70 | + |
| 71 | + public Optional<ConnectorSettingTypeEnum> asEnum() { |
| 72 | + return Optional.ofNullable(enums.getOrDefault(value, null)); |
| 73 | + } |
| 74 | + |
| 75 | + public boolean isKnown() { |
| 76 | + return asEnum().isPresent(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public int hashCode() { |
| 81 | + return Objects.hash(value); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(java.lang.Object obj) { |
| 86 | + if (this == obj) |
| 87 | + return true; |
| 88 | + if (obj == null) |
| 89 | + return false; |
| 90 | + if (getClass() != obj.getClass()) |
| 91 | + return false; |
| 92 | + ConnectorSettingType other = (ConnectorSettingType) obj; |
| 93 | + return Objects.equals(value, other.value); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String toString() { |
| 98 | + return "ConnectorSettingType [value=" + value + "]"; |
| 99 | + } |
| 100 | + |
| 101 | + // return an array just like an enum |
| 102 | + public static ConnectorSettingType[] values() { |
| 103 | + synchronized (ConnectorSettingType.class) { |
| 104 | + return values.values().toArray(new ConnectorSettingType[] {}); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static final Map<String, ConnectorSettingType> createValuesMap() { |
| 109 | + Map<String, ConnectorSettingType> map = new LinkedHashMap<>(); |
| 110 | + map.put("text", TEXT); |
| 111 | + map.put("checkbox", CHECKBOX); |
| 112 | + map.put("tel", TEL); |
| 113 | + map.put("email", EMAIL); |
| 114 | + map.put("url", URL); |
| 115 | + map.put("textarea", TEXTAREA); |
| 116 | + map.put("select", SELECT); |
| 117 | + map.put("filtered-select", FILTERED_SELECT); |
| 118 | + map.put("multi-select", MULTI_SELECT); |
| 119 | + map.put("datetime", DATETIME); |
| 120 | + map.put("date", DATE); |
| 121 | + map.put("time", TIME); |
| 122 | + map.put("number", NUMBER); |
| 123 | + map.put("password", PASSWORD); |
| 124 | + return map; |
| 125 | + } |
| 126 | + |
| 127 | + private static final Map<String, ConnectorSettingTypeEnum> createEnumsMap() { |
| 128 | + Map<String, ConnectorSettingTypeEnum> map = new HashMap<>(); |
| 129 | + map.put("text", ConnectorSettingTypeEnum.TEXT); |
| 130 | + map.put("checkbox", ConnectorSettingTypeEnum.CHECKBOX); |
| 131 | + map.put("tel", ConnectorSettingTypeEnum.TEL); |
| 132 | + map.put("email", ConnectorSettingTypeEnum.EMAIL); |
| 133 | + map.put("url", ConnectorSettingTypeEnum.URL); |
| 134 | + map.put("textarea", ConnectorSettingTypeEnum.TEXTAREA); |
| 135 | + map.put("select", ConnectorSettingTypeEnum.SELECT); |
| 136 | + map.put("filtered-select", ConnectorSettingTypeEnum.FILTERED_SELECT); |
| 137 | + map.put("multi-select", ConnectorSettingTypeEnum.MULTI_SELECT); |
| 138 | + map.put("datetime", ConnectorSettingTypeEnum.DATETIME); |
| 139 | + map.put("date", ConnectorSettingTypeEnum.DATE); |
| 140 | + map.put("time", ConnectorSettingTypeEnum.TIME); |
| 141 | + map.put("number", ConnectorSettingTypeEnum.NUMBER); |
| 142 | + map.put("password", ConnectorSettingTypeEnum.PASSWORD); |
| 143 | + return map; |
| 144 | + } |
37 | 145 |
|
38 | | - public static Optional<ConnectorSettingType> fromValue(String value) { |
39 | | - for (ConnectorSettingType o: ConnectorSettingType.values()) { |
40 | | - if (Objects.deepEquals(o.value, value)) { |
41 | | - return Optional.of(o); |
42 | | - } |
| 146 | + |
| 147 | + public enum ConnectorSettingTypeEnum { |
| 148 | + |
| 149 | + TEXT("text"), |
| 150 | + CHECKBOX("checkbox"), |
| 151 | + TEL("tel"), |
| 152 | + EMAIL("email"), |
| 153 | + URL("url"), |
| 154 | + TEXTAREA("textarea"), |
| 155 | + SELECT("select"), |
| 156 | + FILTERED_SELECT("filtered-select"), |
| 157 | + MULTI_SELECT("multi-select"), |
| 158 | + DATETIME("datetime"), |
| 159 | + DATE("date"), |
| 160 | + TIME("time"), |
| 161 | + NUMBER("number"), |
| 162 | + PASSWORD("password"),; |
| 163 | + |
| 164 | + private final String value; |
| 165 | + |
| 166 | + private ConnectorSettingTypeEnum(String value) { |
| 167 | + this.value = value; |
| 168 | + } |
| 169 | + |
| 170 | + public String value() { |
| 171 | + return value; |
43 | 172 | } |
44 | | - return Optional.empty(); |
45 | 173 | } |
46 | 174 | } |
47 | 175 |
|
0 commit comments