View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.ssl;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * Utility class for application protocol common operations.
23   */
24  final class ApplicationProtocolUtil {
25      private static final int DEFAULT_LIST_SIZE = 2;
26  
27      private ApplicationProtocolUtil() {
28      }
29  
30      static List<String> toList(Iterable<String> protocols) {
31          return toList(DEFAULT_LIST_SIZE, protocols);
32      }
33  
34      static List<String> toList(int initialListSize, Iterable<String> protocols) {
35          if (protocols == null) {
36              return null;
37          }
38  
39          List<String> result = new ArrayList<String>(initialListSize);
40          for (String p : protocols) {
41              if (p == null || p.isEmpty()) {
42                  throw new IllegalArgumentException("protocol cannot be null or empty");
43              }
44              result.add(p);
45          }
46  
47          if (result.isEmpty()) {
48              throw new IllegalArgumentException("protocols cannot empty");
49          }
50  
51          return result;
52      }
53  
54      static List<String> toList(String... protocols) {
55          return toList(DEFAULT_LIST_SIZE, protocols);
56      }
57  
58      static List<String> toList(int initialListSize, String... protocols) {
59          if (protocols == null) {
60              return null;
61          }
62  
63          List<String> result = new ArrayList<String>(initialListSize);
64          for (String p : protocols) {
65              if (p == null || p.isEmpty()) {
66                  throw new IllegalArgumentException("protocol cannot be null or empty");
67              }
68              result.add(p);
69          }
70  
71          if (result.isEmpty()) {
72              throw new IllegalArgumentException("protocols cannot empty");
73          }
74  
75          return result;
76      }
77  }