View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   * 
19   */
20  
21  package org.apache.directory.server.dhcp.messages;
22  
23  
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.List;
27  
28  
29  /**
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   * @version $Rev: 638228 $, $Date: 2008-03-18 07:12:41 +0100 (Di, 18 Mär 2008) $
32   */
33  public final class MessageType implements Comparable
34  {
35      // FIXME: does this class make a lot of sense in absence of real (1.5)
36      // enums?
37      // The DOCPDISCOVER et. al. constants can't be used conveniently in
38      // switches,
39      // therefore the byte constants fpr the opcodes are duplicated here.
40      public static final byte CODE_DHCPINFORM = 8;
41  
42      public static final byte CODE_DHCPRELEASE = 7;
43  
44      public static final byte CODE_DHCPNAK = 6;
45  
46      public static final byte CODE_DHCPACK = 5;
47  
48      public static final byte CODE_DHCPDECLINE = 4;
49  
50      public static final byte CODE_DHCPREQUEST = 3;
51  
52      public static final byte CODE_DHCPOFFER = 2;
53  
54      public static final byte CODE_DHCPDISCOVER = 1;
55  
56      /**
57       * Enumeration elements are constructed once upon class loading. Order of
58       * appearance here determines the order of compareTo.
59       */
60      public static final MessageType DHCPDISCOVER = new MessageType( CODE_DHCPDISCOVER, "DHCP Discover" );
61  
62      public static final MessageType DHCPOFFER = new MessageType( CODE_DHCPOFFER, "DHCP Offer" );
63  
64      public static final MessageType DHCPREQUEST = new MessageType( CODE_DHCPREQUEST, "DHCP Request" );
65  
66      public static final MessageType DHCPDECLINE = new MessageType( CODE_DHCPDECLINE, "DHCP Decline" );
67  
68      public static final MessageType DHCPACK = new MessageType( CODE_DHCPACK, "DHCP Acknowledge" );
69  
70      public static final MessageType DHCPNAK = new MessageType( CODE_DHCPNAK, "DHCP Not Acknowledge" );
71  
72      public static final MessageType DHCPRELEASE = new MessageType( CODE_DHCPRELEASE, "DHCP Release" );
73  
74      public static final MessageType DHCPINFORM = new MessageType( CODE_DHCPINFORM, "DHCP Inform" );
75  
76  
77      public String toString()
78      {
79          return name;
80      }
81  
82  
83      public int compareTo( Object that )
84      {
85          return ordinal - ( ( MessageType ) that ).ordinal;
86      }
87  
88  
89      public static MessageType getTypeByCode( byte type )
90      {
91          for ( int ii = 0; ii < values.length; ii++ )
92              if ( values[ii].ordinal == type )
93                  return values[ii];
94          return new MessageType( type, "Unrecognized" );
95      }
96  
97  
98      public byte getCode()
99      {
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 }