1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.util;
18
19 import java.util.Collections;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22 import java.util.Set;
23
24 import static java.util.Objects.requireNonNull;
25
26
27
28
29
30
31
32 @Deprecated
33 public final class DomainNameMappingBuilder<V> {
34
35 private final V defaultValue;
36 private final Map<String, V> map;
37
38
39
40
41
42
43
44 public DomainNameMappingBuilder(V defaultValue) {
45 this(4, defaultValue);
46 }
47
48
49
50
51
52
53
54
55 public DomainNameMappingBuilder(int initialCapacity, V defaultValue) {
56 this.defaultValue = requireNonNull(defaultValue, "defaultValue");
57 map = new LinkedHashMap<>(initialCapacity);
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public DomainNameMappingBuilder<V> add(String hostname, V output) {
73 map.put(requireNonNull(hostname, "hostname"), requireNonNull(output, "output"));
74 return this;
75 }
76
77
78
79
80
81
82
83 public DomainNameMapping<V> build() {
84 return new ImmutableDomainNameMapping<>(defaultValue, map);
85 }
86
87
88
89
90
91
92
93 private static final class ImmutableDomainNameMapping<V> extends DomainNameMapping<V> {
94 private static final String REPR_HEADER = "ImmutableDomainNameMapping(default: ";
95 private static final String REPR_MAP_OPENING = ", map: {";
96 private static final String REPR_MAP_CLOSING = "})";
97 private static final int REPR_CONST_PART_LENGTH =
98 REPR_HEADER.length() + REPR_MAP_OPENING.length() + REPR_MAP_CLOSING.length();
99
100 private final String[] domainNamePatterns;
101 private final V[] values;
102 private final Map<String, V> map;
103
104 @SuppressWarnings("unchecked")
105 private ImmutableDomainNameMapping(V defaultValue, Map<String, V> map) {
106 super(null, defaultValue);
107
108 Set<Map.Entry<String, V>> mappings = map.entrySet();
109 int numberOfMappings = mappings.size();
110 domainNamePatterns = new String[numberOfMappings];
111 values = (V[]) new Object[numberOfMappings];
112
113 final Map<String, V> mapCopy = new LinkedHashMap<>(map.size());
114 int index = 0;
115 for (Map.Entry<String, V> mapping : mappings) {
116 final String hostname = normalizeHostname(mapping.getKey());
117 final V value = mapping.getValue();
118 domainNamePatterns[index] = hostname;
119 values[index] = value;
120 mapCopy.put(hostname, value);
121 ++index;
122 }
123
124 this.map = Collections.unmodifiableMap(mapCopy);
125 }
126
127 @Override
128 @Deprecated
129 public DomainNameMapping<V> add(String hostname, V output) {
130 throw new UnsupportedOperationException(
131 "Immutable DomainNameMapping does not support modification after initial creation");
132 }
133
134 @Override
135 public V map(String hostname) {
136 if (hostname != null) {
137 hostname = normalizeHostname(hostname);
138
139 int length = domainNamePatterns.length;
140 for (int index = 0; index < length; ++index) {
141 if (matches(domainNamePatterns[index], hostname)) {
142 return values[index];
143 }
144 }
145 }
146
147 return defaultValue;
148 }
149
150 @Override
151 public Map<String, V> asMap() {
152 return map;
153 }
154
155 @Override
156 public String toString() {
157 String defaultValueStr = defaultValue.toString();
158
159 int numberOfMappings = domainNamePatterns.length;
160 if (numberOfMappings == 0) {
161 return REPR_HEADER + defaultValueStr + REPR_MAP_OPENING + REPR_MAP_CLOSING;
162 }
163
164 String pattern0 = domainNamePatterns[0];
165 String value0 = values[0].toString();
166 int oneMappingLength = pattern0.length() + value0.length() + 3;
167 int estimatedBufferSize = estimateBufferSize(defaultValueStr.length(), numberOfMappings, oneMappingLength);
168
169 StringBuilder sb = new StringBuilder(estimatedBufferSize)
170 .append(REPR_HEADER).append(defaultValueStr).append(REPR_MAP_OPENING);
171
172 appendMapping(sb, pattern0, value0);
173 for (int index = 1; index < numberOfMappings; ++index) {
174 sb.append(", ");
175 appendMapping(sb, index);
176 }
177
178 return sb.append(REPR_MAP_CLOSING).toString();
179 }
180
181
182
183
184
185
186
187
188
189
190
191 private static int estimateBufferSize(int defaultValueLength,
192 int numberOfMappings,
193 int estimatedMappingLength) {
194 return REPR_CONST_PART_LENGTH + defaultValueLength
195 + (int) (estimatedMappingLength * numberOfMappings * 1.10);
196 }
197
198 private StringBuilder appendMapping(StringBuilder sb, int mappingIndex) {
199 return appendMapping(sb, domainNamePatterns[mappingIndex], values[mappingIndex].toString());
200 }
201
202 private static StringBuilder appendMapping(StringBuilder sb, String domainNamePattern, String value) {
203 return sb.append(domainNamePattern).append('=').append(value);
204 }
205 }
206 }