1/**
2 *
3 */
4package javax.jmdns.impl.constants;
5
6import java.util.logging.Level;
7import java.util.logging.Logger;
8
9/**
10 * DNS Record Class
11 *
12 * @author Arthur van Hoff, Jeff Sonstein, Werner Randelshofer, Pierre Frisch, Rick Blair
13 */
14public enum DNSRecordClass {
15    /**
16     *
17     */
18    CLASS_UNKNOWN("?", 0),
19    /**
20     * static final Internet
21     */
22    CLASS_IN("in", 1),
23    /**
24     * CSNET
25     */
26    CLASS_CS("cs", 2),
27    /**
28     * CHAOS
29     */
30    CLASS_CH("ch", 3),
31    /**
32     * Hesiod
33     */
34    CLASS_HS("hs", 4),
35    /**
36     * Used in DNS UPDATE [RFC 2136]
37     */
38    CLASS_NONE("none", 254),
39    /**
40     * Not a DNS class, but a DNS query class, meaning "all classes"
41     */
42    CLASS_ANY("any", 255);
43
44    private static Logger       logger       = Logger.getLogger(DNSRecordClass.class.getName());
45
46    /**
47     * Multicast DNS uses the bottom 15 bits to identify the record class...<br/>
48     * Except for pseudo records like OPT.
49     */
50    public static final int     CLASS_MASK   = 0x7FFF;
51
52    /**
53     * For answers the top bit indicates that all other cached records are now invalid.<br/>
54     * For questions it indicates that we should send a unicast response.
55     */
56    public static final int     CLASS_UNIQUE = 0x8000;
57
58    /**
59     *
60     */
61    public static final boolean UNIQUE       = true;
62
63    /**
64     *
65     */
66    public static final boolean NOT_UNIQUE   = false;
67
68    private final String        _externalName;
69
70    private final int           _index;
71
72    DNSRecordClass(String name, int index) {
73        _externalName = name;
74        _index = index;
75    }
76
77    /**
78     * Return the string representation of this type
79     *
80     * @return String
81     */
82    public String externalName() {
83        return _externalName;
84    }
85
86    /**
87     * Return the numeric value of this type
88     *
89     * @return String
90     */
91    public int indexValue() {
92        return _index;
93    }
94
95    /**
96     * Checks if the class is unique
97     *
98     * @param index
99     * @return <code>true</code> is the class is unique, <code>false</code> otherwise.
100     */
101    public boolean isUnique(int index) {
102        return (this != CLASS_UNKNOWN) && ((index & CLASS_UNIQUE) != 0);
103    }
104
105    /**
106     * @param name
107     * @return class for name
108     */
109    public static DNSRecordClass classForName(String name) {
110        if (name != null) {
111            String aName = name.toLowerCase();
112            for (DNSRecordClass aClass : DNSRecordClass.values()) {
113                if (aClass._externalName.equals(aName)) return aClass;
114            }
115        }
116        logger.log(Level.WARNING, "Could not find record class for name: " + name);
117        return CLASS_UNKNOWN;
118    }
119
120    /**
121     * @param index
122     * @return class for name
123     */
124    public static DNSRecordClass classForIndex(int index) {
125        int maskedIndex = index & CLASS_MASK;
126        for (DNSRecordClass aClass : DNSRecordClass.values()) {
127            if (aClass._index == maskedIndex) return aClass;
128        }
129        logger.log(Level.WARNING, "Could not find record class for index: " + index);
130        return CLASS_UNKNOWN;
131    }
132
133    @Override
134    public String toString() {
135        return this.name() + " index " + this.indexValue();
136    }
137
138}
139