Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6806847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:45:37+00:00 2026-05-26T19:45:37+00:00

I’ve read some topics about this, but still can’t figure out how I can

  • 0

I’ve read some topics about this, but still can’t figure out how I can do the thing I want to do:

I have a number of enums that extend a particular interface (HasCode), which declares one function, code(), which returns the int code of this enum. All the extending enums have private int vars and parameters to their constructors (etc).

I would like to have a function that would accept an enum class as a parameter and return a hashtable of the names of the enum’s values and the int values of those enum values.

Here is an example of my enums:

public enum InvoiceStatuses implements HasCode<InvoiceStatuses> {
    NONE(0), REQUESTED(10), RECEIVED(20), APPROVED(30), PAID(40), ERROR(-100);
    private int code;
    private InvoiceStatuses(int a) {
        code = a;
    }
    public int code() {
        return code;
    }
    public static InvoiceStatuses invoiceStatusByCode(int aCode) {
        switch (aCode) {
        case 0:
            return NONE;
        case 10:
            return REQUESTED;
        case 20:
            return RECEIVED;
        case 30:
            return APPROVED;
        case 40:
            return PAID;
        }
        return ERROR;
    }
    public int compare(InvoiceStatuses anotherLevel) {
        if (code > anotherLevel.code()) {
            return 1;
        } else if (code < anotherLevel.code()) {
            return -1;
        }
        return 0;
    }
    @Override
    public InvoiceStatuses[] enumValues() {
        return InvoiceStatuses.values();
    }
}

HasCode:

public interface HasCode <T extends Enum<?>>{
    public int code();
    public T[] enumValues();
}

And here is the class with that magic function (that iterates over the values of the enum)

import java.util.Hashtable;
import java.util.LinkedList;
public class SelectFromEnum&lt;T extends Enum<?>> {
    public SelectFromEnum() {}
    public Hashtable<String, String> createSelect(HasCode<T> anEnum) throws Exception{
        if (! (anEnum instanceof Enum)) {
            throw new Exception ("I only accept enums...");
        }
        T[] values = anEnum.enumValues();
        for (T value : values) {
            System.out.println(value.name() + " __ " + ((HasCode)value).code());
        }
        return null;
    }
}

What i don’t like about this:

1) I have to pass a concrete enum member as the parameter to the createSelect function, and I would like to pass the (enum).class;

2) I would like the parameter of the createSelect to specify that it is an Enum that extends a given interface, but i can’t quite figure out how to do that.

Update:

In accordance with Ingo Kegel’s comment I modified the SelectFromEnum the following way (see below), and now value.name() doesn’t compile…

import java.util.Hashtable;
import java.util.LinkedList;

public class SelectFromEnum<C extends HasCode> {
    public SelectFromEnum() {
    }
    public Hashtable&lt;String, String> createSelect(Class<C> anEnum) throws Exception {
        if (!anEnum.isEnum()) { 
            throw new Exception("I only accept enums...");
        }
        C[] values = anEnum.getEnumConstants();
        for (C value : values) {
            System.out.println(value.name() + " __ " + value.code());
        }
        return null;
    }
}

I can add name() to the HasCode interface, but is there a way to persuade the compiler that value is an enum member?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:45:38+00:00Added an answer on May 26, 2026 at 7:45 pm

    You don’t have to use an instance of the enum. You can pass the class and use Class#isEnum and Class#getEnumConstants().

    Edit

    You can cast to objects returned by Class#getEnumConstants() to Enum and your code should look like this:

    public LinkedList createSelectFrom(Class anEnum) throws Exception {
        if (!anEnum.isEnum()) {
            throw new Exception("I only accept enums...");
        }
        Object[] values = anEnum.getEnumConstants();
        for (Object value : values) {
            System.out.println(((Enum)value).name() + " __ " + ((HasCode) value).code());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.