View Javadoc
1   /*
2    * Copyright (C) 2013 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package com.google.thirdparty.publicsuffix;
16  
17  import com.google.common.annotations.GwtCompatible;
18  
19  /**
20   * Specifies the type of a top-level domain definition.
21   */
22  @GwtCompatible
23  enum PublicSuffixType {
24  
25    /** private definition of a top-level domain */
26    PRIVATE(':', ','),
27    /** ICANN definition of a top-level domain */
28    ICANN('!', '?');
29  
30    /** The character used for an inner node in the trie encoding */
31    private final char innerNodeCode;
32  
33    /** The character used for a leaf node in the trie encoding */
34    private final char leafNodeCode;
35  
36    private PublicSuffixType(char innerNodeCode, char leafNodeCode) {
37      this.innerNodeCode = innerNodeCode;
38      this.leafNodeCode = leafNodeCode;
39    }
40  
41    char getLeafNodeCode() {
42      return leafNodeCode;
43    }
44  
45    char getInnerNodeCode() {
46      return innerNodeCode;
47    }
48  
49    /** Returns a PublicSuffixType of the right type according to the given code */
50    static PublicSuffixType fromCode(char code) {
51      for (PublicSuffixType value : values()) {
52        if (value.getInnerNodeCode() == code || value.getLeafNodeCode() == code) {
53          return value;
54        }
55      }
56      throw new IllegalArgumentException("No enum corresponding to given code: " + code);
57    }
58  
59    static PublicSuffixType fromIsPrivate(boolean isPrivate) {
60      return isPrivate ? PRIVATE : ICANN;
61    }
62  }