001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020
021 package org.apache.directory.server.dhcp.messages;
022
023
024 import java.util.Arrays;
025 import java.util.Collections;
026 import java.util.List;
027
028
029 /**
030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
031 * @version $Rev: 638228 $, $Date: 2008-03-18 08:12:41 +0200 (Tue, 18 Mar 2008) $
032 */
033 public final class MessageType implements Comparable
034 {
035 // FIXME: does this class make a lot of sense in absence of real (1.5)
036 // enums?
037 // The DOCPDISCOVER et. al. constants can't be used conveniently in
038 // switches,
039 // therefore the byte constants fpr the opcodes are duplicated here.
040 public static final byte CODE_DHCPINFORM = 8;
041
042 public static final byte CODE_DHCPRELEASE = 7;
043
044 public static final byte CODE_DHCPNAK = 6;
045
046 public static final byte CODE_DHCPACK = 5;
047
048 public static final byte CODE_DHCPDECLINE = 4;
049
050 public static final byte CODE_DHCPREQUEST = 3;
051
052 public static final byte CODE_DHCPOFFER = 2;
053
054 public static final byte CODE_DHCPDISCOVER = 1;
055
056 /**
057 * Enumeration elements are constructed once upon class loading. Order of
058 * appearance here determines the order of compareTo.
059 */
060 public static final MessageType DHCPDISCOVER = new MessageType( CODE_DHCPDISCOVER, "DHCP Discover" );
061
062 public static final MessageType DHCPOFFER = new MessageType( CODE_DHCPOFFER, "DHCP Offer" );
063
064 public static final MessageType DHCPREQUEST = new MessageType( CODE_DHCPREQUEST, "DHCP Request" );
065
066 public static final MessageType DHCPDECLINE = new MessageType( CODE_DHCPDECLINE, "DHCP Decline" );
067
068 public static final MessageType DHCPACK = new MessageType( CODE_DHCPACK, "DHCP Acknowledge" );
069
070 public static final MessageType DHCPNAK = new MessageType( CODE_DHCPNAK, "DHCP Not Acknowledge" );
071
072 public static final MessageType DHCPRELEASE = new MessageType( CODE_DHCPRELEASE, "DHCP Release" );
073
074 public static final MessageType DHCPINFORM = new MessageType( CODE_DHCPINFORM, "DHCP Inform" );
075
076
077 public String toString()
078 {
079 return name;
080 }
081
082
083 public int compareTo( Object that )
084 {
085 return ordinal - ( ( MessageType ) that ).ordinal;
086 }
087
088
089 public static MessageType getTypeByCode( byte type )
090 {
091 for ( int ii = 0; ii < values.length; ii++ )
092 if ( values[ii].ordinal == type )
093 return values[ii];
094 return new MessageType( type, "Unrecognized" );
095 }
096
097
098 public byte getCode()
099 {
100 return ordinal;
101 }
102
103 // / PRIVATE /////
104 private final String name;
105
106 private final byte ordinal;
107
108
109 /**
110 * Private constructor prevents construction outside of this class.
111 */
112 private MessageType(byte ordinal, String name)
113 {
114 this.ordinal = ordinal;
115 this.name = name;
116 }
117
118 /**
119 * These two lines are all that's necessary to export a List of VALUES.
120 */
121 private static final MessageType[] values =
122 { DHCPDISCOVER, DHCPOFFER, DHCPREQUEST, DHCPDECLINE, DHCPACK, DHCPNAK, DHCPRELEASE, DHCPINFORM };
123
124 // VALUES needs to be located here, otherwise illegal forward reference
125 public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values ) );
126 }