directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fel...@apache.org
Subject svn commit: r1037653 - in /directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp: messages/MessageType.java service/AbstractDhcpService.java
Date Mon, 22 Nov 2010 09:54:34 GMT
Author: felixk
Date: Mon Nov 22 09:54:34 2010
New Revision: 1037653

URL: http://svn.apache.org/viewvc?rev=1037653&view=rev
Log:
Transform to enum

Modified:
    directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java
    directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/AbstractDhcpService.java

Modified: directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java?rev=1037653&r1=1037652&r2=1037653&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java
(original)
+++ directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/messages/MessageType.java
Mon Nov 22 09:54:34 2010
@@ -21,76 +21,43 @@
 package org.apache.directory.server.dhcp.messages;
 
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
-
-
 /**
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  */
-public final class MessageType implements Comparable
+public enum MessageType
 {
-    // FIXME: does this class make a lot of sense in absence of real (1.5)
-    // enums?
-    // The DOCPDISCOVER et. al. constants can't be used conveniently in
-    // switches,
-    // therefore the byte constants fpr the opcodes are duplicated here.
-    public static final byte CODE_DHCPINFORM = 8;
-
-    public static final byte CODE_DHCPRELEASE = 7;
-
-    public static final byte CODE_DHCPNAK = 6;
+    DHCPUNRECOGNIZED(( byte ) -1, "unrecognized"),
 
-    public static final byte CODE_DHCPACK = 5;
+    DHCPDISCOVER(( byte ) 1, "DHCP Discover"),
 
-    public static final byte CODE_DHCPDECLINE = 4;
+    DHCPOFFER(( byte ) 2, "DHCP Offer"),
 
-    public static final byte CODE_DHCPREQUEST = 3;
+    DHCPREQUEST(( byte ) 3, "DHCP Request"),
 
-    public static final byte CODE_DHCPOFFER = 2;
-
-    public static final byte CODE_DHCPDISCOVER = 1;
-
-    /**
-     * Enumeration elements are constructed once upon class loading. Order of
-     * appearance here determines the order of compareTo.
-     */
-    public static final MessageType DHCPDISCOVER = new MessageType( CODE_DHCPDISCOVER, "DHCP
Discover" );
+    DHCPDECLINE(( byte ) 4, "DHCP Decline"),
 
-    public static final MessageType DHCPOFFER = new MessageType( CODE_DHCPOFFER, "DHCP Offer"
);
+    DHCPACK(( byte ) 5, "DHCP Acknowledge"),
 
-    public static final MessageType DHCPREQUEST = new MessageType( CODE_DHCPREQUEST, "DHCP
Request" );
+    DHCPNAK(( byte ) 6, "DHCP Not Acknowledge"),
 
-    public static final MessageType DHCPDECLINE = new MessageType( CODE_DHCPDECLINE, "DHCP
Decline" );
+    DHCPRELEASE(( byte ) 7, "DHCP Release"),
 
-    public static final MessageType DHCPACK = new MessageType( CODE_DHCPACK, "DHCP Acknowledge"
);
+    DHCPINFORM(( byte ) 8, "DHCP Inform");
 
-    public static final MessageType DHCPNAK = new MessageType( CODE_DHCPNAK, "DHCP Not Acknowledge"
);
-
-    public static final MessageType DHCPRELEASE = new MessageType( CODE_DHCPRELEASE, "DHCP
Release" );
-
-    public static final MessageType DHCPINFORM = new MessageType( CODE_DHCPINFORM, "DHCP
Inform" );
-
-
-    public String toString()
-    {
-        return name;
-    }
-
-
-    public int compareTo( Object that )
-    {
-        return ordinal - ( ( MessageType ) that ).ordinal;
-    }
+    private String name;
+    private byte ordinal;
 
 
     public static MessageType getTypeByCode( byte type )
     {
-        for ( int ii = 0; ii < values.length; ii++ )
-            if ( values[ii].ordinal == type )
-                return values[ii];
-        return new MessageType( type, "Unrecognized" );
+        for ( MessageType mt : MessageType.values() )
+        {
+            if ( type == mt.getCode() )
+            {
+                return mt;
+            }
+        }
+        return DHCPUNRECOGNIZED;
     }
 
 
@@ -99,27 +66,20 @@ public final class MessageType implement
         return ordinal;
     }
 
-    // / PRIVATE /////
-    private final String name;
-
-    private final byte ordinal;
-
 
     /**
      * Private constructor prevents construction outside of this class.
      */
-    private MessageType(byte ordinal, String name)
+    private MessageType( byte ordinal, String name )
     {
         this.ordinal = ordinal;
         this.name = name;
     }
 
-    /**
-     * These two lines are all that's necessary to export a List of VALUES.
-     */
-    private static final MessageType[] values =
-        { DHCPDISCOVER, DHCPOFFER, DHCPREQUEST, DHCPDECLINE, DHCPACK, DHCPNAK, DHCPRELEASE,
DHCPINFORM };
 
-    // VALUES needs to be located here, otherwise illegal forward reference
-    public static final List VALUES = Collections.unmodifiableList( Arrays.asList( values
) );
+    public String toString()
+    {
+        return name;
+    }
+
 }

Modified: directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/AbstractDhcpService.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/AbstractDhcpService.java?rev=1037653&r1=1037652&r2=1037653&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/AbstractDhcpService.java
(original)
+++ directory/apacheds/trunk/protocol-dhcp/src/main/java/org/apache/directory/server/dhcp/service/AbstractDhcpService.java
Mon Nov 22 09:54:34 2010
@@ -24,7 +24,6 @@ import java.util.Iterator;
 
 import org.apache.directory.server.dhcp.DhcpException;
 import org.apache.directory.server.dhcp.messages.DhcpMessage;
-import org.apache.directory.server.dhcp.messages.MessageType;
 import org.apache.directory.server.dhcp.options.DhcpOption;
 import org.apache.directory.server.dhcp.options.OptionsField;
 import org.apache.directory.server.dhcp.options.dhcp.ParameterRequestList;
@@ -64,24 +63,24 @@ public abstract class AbstractDhcpServic
         }
 
         // dispatch based on the message type
-        switch (request.getMessageType().getCode()){
+        switch (request.getMessageType()){
             // client-to-server messages
-            case MessageType.CODE_DHCPDISCOVER :
+            case DHCPDISCOVER :
                 return handleDISCOVER(localAddress, clientAddress, request);
-            case MessageType.CODE_DHCPREQUEST :
+            case DHCPREQUEST :
                 return handleREQUEST(localAddress, clientAddress, request);
-            case MessageType.CODE_DHCPRELEASE :
+            case DHCPRELEASE :
                 return handleRELEASE(localAddress, clientAddress, request);
-            case MessageType.CODE_DHCPINFORM :
+            case DHCPINFORM :
                 return handleINFORM(localAddress, clientAddress, request);
 
-            case MessageType.CODE_DHCPOFFER :
+            case DHCPOFFER :
                 return handleOFFER(localAddress, clientAddress, request);
 
                 // server-to-client messages
-            case MessageType.CODE_DHCPDECLINE :
-            case MessageType.CODE_DHCPACK :
-            case MessageType.CODE_DHCPNAK :
+            case DHCPDECLINE :
+            case DHCPACK :
+            case DHCPNAK :
                 return null; // just ignore them
 
             default :



Mime
View raw message