Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract Class JEnum #28

Open
nsanitate opened this issue Jul 24, 2017 · 1 comment
Open

Abstract Class JEnum #28

nsanitate opened this issue Jul 24, 2017 · 1 comment

Comments

@nsanitate
Copy link
Member

nsanitate commented Jul 24, 2017

Implement class JEnum<E>, porting of the Java class java.lang.Enum<E>.

This is the common base class of all Java language enumeration types. More information about enums, including descriptions of the implicitly declared methods synthesized by the compiler, can be found in section 8.9 of The Java™ Language Specification.
E - The enum type subclass

Expected Behavior

Sole constructor.

  • public static method valueOf<T extends Enum<T>>(enumType: JClass<T>, name: JString): T

Returns the enum constant of the specified enum type with the specified name.

  • public method compareTo(E o): Jint

Compares this enum with the specified object for order.

  • public method equals(other: JObject): Jboolean

Returns true if the specified object is equal to this enum constant.

  • public method hashCode(): Jint

Returns a hash code for this enum constant.

  • public method name(): JString

Returns the name of this enum constant, exactly as declared in its enum declaration.

  • public method ordinal(): Jint

Returns the ordinal of this enumeration constant (its position in its enum declaration, where the initial constant is assigned an ordinal of zero).

  • public method toString(): JString

Returns the name of this enum constant, as contained in the declaration.

  • protected method clone(): JObject

Throws CloneNotSupportedException.

Do it later

  • public method getDeclaringClass(): JClass<E>
  • protected method finalize(): void
@dammafra
Copy link

dammafra commented Aug 4, 2017

In Java the Enum class is never used directly, but it's internally instantiated when used the enum keyword. Moreover, Enum class represents only an enum constant and not an enum type, that is represented by a Class object, in fact an enumeration could have members or methods.

If we have this Java code:

enum Mobile {
    Samsung(400) , Nokia(250), Motorola(325);

    int price;

    Mobile(int p) {
        price = p;
    }

    int showPrice() {
        return price;
    }
}

public class EnumDemo {

    public static void main(String args[]) {

        System.out.println("CellPhone List:");
        for (Mobile m : Mobile.values()) {
            System.out.println(m + " costs " + m.showPrice() + " dollars");
            System.out.println("Name: " + m.name());
            System.out.println("Ordinal: " + m.ordinal());
            System.out.println("Declaring Class: " + m.getDeclaringClass());
            System.out.println();
        }

        Mobile ret;
        ret = Mobile.valueOf("Samsung");
        System.out.println("Selected : " + ret);
    }
}

/* output
CellPhone List:
Samsung costs 400 dollars
Name: Samsung
Ordinal: 0
Declaring Class: Mobile

Nokia costs 250 dollars
Name: Nokia
Ordinal: 1
Declaring Class: class Mobile

Motorola costs 325 dollars
Name: Motorola
Ordinal: 2
Declaring Class: class Mobile 
*/

The Mobile enumeration type will have three static members for its three constants (that are themselves instances of "class" Mobile) and also implicit methods values() and valueOf(String).

Enum constants could be classes too:

enum Operation {
    PLUS {
        double eval(double x, double y) { return x + y; }
    },
    MINUS {
        double eval(double x, double y) { return x - y; }
    },
    TIMES {
        double eval(double x, double y) { return x * y; }
    },
    DIVIDED_BY {
        double eval(double x, double y) { return x / y; }
    };

    // Each constant supports an arithmetic operation
    abstract double eval(double x, double y);

    public static void main(String args[]) {
        double x = Double.parseDouble(args[0]);
        double y = Double.parseDouble(args[1]);
        for (Operation op : Operation.values())
            System.out.println(x + " " + op + " " + y + " = " + op.eval(x, y));
    }
}

/* output
java Operation 2.0 4.0
2.0 PLUS 4.0 = 6.0
2.0 MINUS 4.0 = -2.0
2.0 TIMES 4.0 = 8.0
2.0 DIVIDED_BY 4.0 = 0.5
*/

What should be the corresponding code in our framework?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants