1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.resolver.dns;
18
19 import static io.netty.util.internal.ObjectUtil.checkNotNull;
20 import static io.netty.util.internal.ObjectUtil.checkNonEmpty;
21
22 import java.net.InetSocketAddress;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26
27
28
29
30 @SuppressWarnings("IteratorNextCanNotThrowNoSuchElementException")
31 public abstract class DnsServerAddresses {
32
33
34
35
36
37
38
39 @Deprecated
40 public static List<InetSocketAddress> defaultAddressList() {
41 return DefaultDnsServerAddressStreamProvider.defaultAddressList();
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 @Deprecated
58 public static DnsServerAddresses defaultAddresses() {
59 return DefaultDnsServerAddressStreamProvider.defaultAddresses();
60 }
61
62
63
64
65
66 public static DnsServerAddresses sequential(Iterable<? extends InetSocketAddress> addresses) {
67 return sequential0(sanitize(addresses));
68 }
69
70
71
72
73
74 public static DnsServerAddresses sequential(InetSocketAddress... addresses) {
75 return sequential0(sanitize(addresses));
76 }
77
78 private static DnsServerAddresses sequential0(final List<InetSocketAddress> addresses) {
79 if (addresses.size() == 1) {
80 return singleton(addresses.get(0));
81 }
82
83 return new DefaultDnsServerAddresses("sequential", addresses) {
84 @Override
85 public DnsServerAddressStream stream() {
86 return new SequentialDnsServerAddressStream(addresses, 0);
87 }
88 };
89 }
90
91
92
93
94
95 public static DnsServerAddresses shuffled(Iterable<? extends InetSocketAddress> addresses) {
96 return shuffled0(sanitize(addresses));
97 }
98
99
100
101
102
103 public static DnsServerAddresses shuffled(InetSocketAddress... addresses) {
104 return shuffled0(sanitize(addresses));
105 }
106
107 private static DnsServerAddresses shuffled0(List<InetSocketAddress> addresses) {
108 if (addresses.size() == 1) {
109 return singleton(addresses.get(0));
110 }
111
112 return new DefaultDnsServerAddresses("shuffled", addresses) {
113 @Override
114 public DnsServerAddressStream stream() {
115 return new ShuffledDnsServerAddressStream(addresses);
116 }
117 };
118 }
119
120
121
122
123
124
125
126 public static DnsServerAddresses rotational(Iterable<? extends InetSocketAddress> addresses) {
127 return rotational0(sanitize(addresses));
128 }
129
130
131
132
133
134
135
136 public static DnsServerAddresses rotational(InetSocketAddress... addresses) {
137 return rotational0(sanitize(addresses));
138 }
139
140 private static DnsServerAddresses rotational0(List<InetSocketAddress> addresses) {
141 if (addresses.size() == 1) {
142 return singleton(addresses.get(0));
143 }
144
145 return new RotationalDnsServerAddresses(addresses);
146 }
147
148
149
150
151 public static DnsServerAddresses singleton(final InetSocketAddress address) {
152 checkNotNull(address, "address");
153 if (address.isUnresolved()) {
154 throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + address);
155 }
156
157 return new SingletonDnsServerAddresses(address);
158 }
159
160 private static List<InetSocketAddress> sanitize(Iterable<? extends InetSocketAddress> addresses) {
161 checkNotNull(addresses, "addresses");
162
163 final List<InetSocketAddress> list;
164 if (addresses instanceof Collection) {
165 list = new ArrayList<InetSocketAddress>(((Collection<?>) addresses).size());
166 } else {
167 list = new ArrayList<InetSocketAddress>(4);
168 }
169
170 for (InetSocketAddress a : addresses) {
171 if (a == null) {
172 break;
173 }
174 if (a.isUnresolved()) {
175 throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + a);
176 }
177 list.add(a);
178 }
179
180 return checkNonEmpty(list, "list");
181 }
182
183 private static List<InetSocketAddress> sanitize(InetSocketAddress[] addresses) {
184 checkNotNull(addresses, "addresses");
185
186 List<InetSocketAddress> list = new ArrayList<InetSocketAddress>(addresses.length);
187 for (InetSocketAddress a: addresses) {
188 if (a == null) {
189 break;
190 }
191 if (a.isUnresolved()) {
192 throw new IllegalArgumentException("cannot use an unresolved DNS server address: " + a);
193 }
194 list.add(a);
195 }
196
197 if (list.isEmpty()) {
198 return DefaultDnsServerAddressStreamProvider.defaultAddressList();
199 }
200
201 return list;
202 }
203
204
205
206
207
208 public abstract DnsServerAddressStream stream();
209 }