harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r412109 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/lang/Enum.java main/java/org/apache/harmony/luni/util/ExternalMessages.properties test/java/org/apache/harmony/tests/java/lang/EnumTest.java
Date Tue, 06 Jun 2006 12:52:31 GMT
Author: tellison
Date: Tue Jun  6 05:52:30 2006
New Revision: 412109

URL: http://svn.apache.org/viewvc?rev=412109&view=rev
Log:
Complete the implementation of Enum

Added:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
  (with props)
Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Enum.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Enum.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Enum.java?rev=412109&r1=412108&r2=412109&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Enum.java
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/lang/Enum.java
Tue Jun  6 05:52:30 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,65 +16,101 @@
 package java.lang;
 
 import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.security.AccessController;
+import java.security.PrivilegedExceptionAction;
+
+import org.apache.harmony.luni.util.Msg;
 
 /**
- * Stub for Enum 
+ * TODO Enum doc
  */
-public class Enum <E extends Enum<E>> implements Serializable, Comparable<E>
{
-
-    private String name;
+public class Enum<E extends Enum<E>> implements Serializable, Comparable<E>
{
 
+    private static final long serialVersionUID = 0L;
 
-    private int ordinal;
+    private final String name;
 
+    private final int ordinal;
 
     protected Enum(String name, int ordinal) {
         this.name = name;
         this.ordinal = ordinal;
     }
 
-
     public final String name() {
         return name;
     }
 
-
     public final int ordinal() {
         return ordinal;
     }
 
-
     public String toString() {
         return "Enum:" + name;
     }
 
-
     public final boolean equals(Object other) {
-        throw new RuntimeException("not yet implemented");
+        return this == other;
     }
 
-
     public final int hashCode() {
         return ordinal + (name == null ? 0 : name.hashCode());
     }
 
-
+    /**
+     * Enums are singletons, they may not be cloned.
+     */
     protected final Object clone() throws CloneNotSupportedException {
-        return new Enum<E> (name, ordinal);
+        // KA004=Enums may not be cloned
+        throw new CloneNotSupportedException(Msg.getString("KA004"));
     }
 
-
     public final int compareTo(E o) {
         return ordinal - o.ordinal;
     }
 
-
     public final Class<E> getDeclaringClass() {
-        throw new RuntimeException("not yet implemented");
+        Class myClass = getClass();
+        Class mySuperClass = myClass.getSuperclass();
+        if (Enum.class == mySuperClass) {
+            return myClass;
+        }
+        return mySuperClass;
     }
 
-
     public static <T extends Enum<T>> T valueOf(Class<T> enumType, String
name) {
-        throw new RuntimeException("not yet implemented");
+        T[] values = getValues(enumType);
+        if (values == null) {
+            // KA005={0} is not an enum type
+            throw new IllegalArgumentException(Msg.getString("KA005", enumType));
+        }
+        for (T enumConst : values) {
+            if (enumConst.name.equals(name)) {
+                return enumConst;
+            }
+        }
+        // KA006={0} is not a constant in the enum type {1}
+        throw new IllegalArgumentException(Msg.getString("KA006", name, enumType));
+    }
+
+    /*
+     * Helper to invoke the values() static method on T and answer
+     * the result.  Returns null if there is a problem.
+     */
+    static <T extends Enum<T>> T[] getValues(final Class<T> enumType) {
+        try {
+            Method values = AccessController.doPrivileged(
+                new PrivilegedExceptionAction<Method>() {
+                    public Method run() throws Exception {
+                        Method valsMethod = enumType.getMethod("values", (Class[])null);
+                        valsMethod.setAccessible(true);
+                        return valsMethod;
+                    }
+                });
+            return (T[]) values.invoke(enumType, null);
+        } catch (Exception e) {
+            return null;
+        }
     }
 }

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties?rev=412109&r1=412108&r2=412109&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
(original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/util/ExternalMessages.properties
Tue Jun  6 05:52:30 2006
@@ -288,4 +288,9 @@
 KA000=Line too long
 KA001=Argument must not be null
 KA002=Unshared read of back reference
-KA003=different mode already set
\ No newline at end of file
+KA003=different mode already set
+KA004=Enums may not be cloned
+KA005={0} is not an enum type
+KA006={0} is not a constant in the enum type {1}
+
+

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java?rev=412109&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
(added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
Tue Jun  6 05:52:30 2006
@@ -0,0 +1,97 @@
+package org.apache.harmony.tests.java.lang;
+
+import junit.framework.TestCase;
+
+public class EnumTest extends TestCase {
+
+    enum Sample {
+        LARRY, MOE, CURLY
+    }
+
+    Sample larry = Sample.LARRY;
+
+    Sample moe = Sample.MOE;
+
+    /**
+     * @tests java.lang.Enum#compareTo(java.lang.Enum) 
+     */
+    public void test_compareToLjava_lang_Enum() {
+        assertTrue(0 < Sample.MOE.compareTo(Sample.LARRY));
+        assertEquals(0, Sample.MOE.compareTo(Sample.MOE));
+        assertTrue(0 > Sample.MOE.compareTo(Sample.CURLY));
+    }
+
+    /**
+     * @tests java.lang.Enum#equals(Object)
+     */
+    public void test_equalsLjava_lang_Object() {
+        assertFalse(moe.equals("bob"));
+        assertTrue(moe.equals(Sample.MOE));
+        assertFalse(Sample.LARRY.equals(Sample.CURLY));
+        assertTrue(Sample.LARRY.equals(larry));
+    }
+
+    /**
+     * @tests java.lang.Enum#getDeclaringClass()
+     */
+    public void test_getDeclaringClass() {
+        assertEquals(Sample.class, moe.getDeclaringClass());
+    }
+
+    /**
+     * @tests java.lang.Enum#hashCode()
+     */
+    public void test_hashCode() {
+        assertEquals (moe.hashCode(), moe.hashCode());
+    }
+
+    /**
+     * @tests java.lang.Enum#name()
+     */
+    public void test_name() {
+        assertEquals("MOE", moe.name());
+    }
+
+    /**
+     * @tests java.lang.Enum#ordinal()
+     */
+    public void test_ordinal() {
+        assertEquals(0, larry.ordinal());
+        assertEquals(1, moe.ordinal());
+        assertEquals(2, Sample.CURLY.ordinal());
+    }
+
+    /**
+     * @tests java.lang.Enum#toString()
+     */
+    public void test_toString() {
+        assertTrue(moe.toString().contains("MOE"));
+    }
+
+    /**
+     * @tests java.lang.Enum#valueOf(Class, String)
+     */
+    public void test_valueOfLjava_lang_String() {
+        assertSame(Sample.CURLY, Sample.valueOf("CURLY"));
+        assertSame(Sample.LARRY, Sample.valueOf("LARRY"));
+        assertSame(moe, Sample.valueOf("MOE"));
+        try {
+            Sample.valueOf("non-existant");
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e){
+            // Expected
+        }
+    }
+
+    /**
+     * @tests java.lang.Enum#values
+     */
+    public void test_values() {
+        Sample[] myValues = Sample.values();
+        assertEquals(3, myValues.length);
+
+        assertEquals(Sample.LARRY, myValues[0]);
+        assertEquals(Sample.MOE, myValues[1]);
+        assertEquals(Sample.CURLY, myValues[2]);
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/org/apache/harmony/tests/java/lang/EnumTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message