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 8236573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:10:38+00:00 2026-06-07T19:10:38+00:00

I have enum which corresponds to encodings.I need to check that enum don’t have

  • 0

I have enum which corresponds to encodings.I need to check that enum don’t have repeating number values for encodings.

public enum EncodingsEnum
{

ISO8859_1("ISO-8859-1",0), ISO8859_2("ISO-8859-2",1),
    ISO8859_3("ISO-8859-3",2), ISO8859_4("ISO-8859-4",3),
    ISO8859_5("ISO-8859-5",4), ISO8859_6("ISO-8859-6",5),
    ISO8859_7("ISO-8859-7",6), ISO8859_8("ISO-8859-8",7),
    ISO8859_9("ISO-8859-9",8), ISO8859_11("ISO-8859-11",9),
    ISO8859_13("ISO-8859-13",10),ISO8859_15("ISO-8859-15",11),
    UTF_8("UTF-8",11);

    public static final int ENCODINGS_COUNT = EncodingsEnum.values().length;
    private final String encodingName;
    private final int encodingNumber;

    EncodingsEnum(final String encodingName,int encodingNumber)
    {
        ReferenceChecker.checkReferenceNotNull(encodingName);

        this.encodingName = encodingName;
        this.encodingNumber = encodingNumber;
    }

    public static String getEncodingNameByNumber(int number)
    {
        for(EncodingsEnum encoding : EncodingsEnum.values())
        {
            if(encoding.encodingNumber == number)
            {
                return encoding.getEncodingName();
            }
        }
        throw new RuntimeException("Encoding with this number isn't supported:" + number);

    }

    public static int getEncodingNumberByName(final String name)
    {
        for(EncodingsEnum encoding : EncodingsEnum.values())
        {
            if(encoding.encodingName.equals(name))
            {
                return encoding.getEncodingNumber();
            }
        }
        throw new RuntimeException("Encoding with this name isn't supported:" + name);
    }

    public String getEncodingName()
    {
        return this.encodingName;
    }

    public int getEncodingNumber()
    {
        return this.encodingNumber;
    }
}

There is a problem that I can create encoding with the same number as one of the existing encodings,so I need to check that enum contains element with this number and throw exception.But I dont know how to do that.Any idea?Thanks.

  • 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-06-07T19:10:39+00:00Added an answer on June 7, 2026 at 7:10 pm

    This shouldn’t be checked at runtime: it would be too late. Add a unit test that iterates through the enum values, and check that they all have a different number. And make sure to always execute the unit tests, and check that they pass, before generating a new version of your application/library.

    @Test
    public void encodingNumbersMustBeUnique() {
        Set<Integer> numbers = new HashSet<Integer>();
        for (EncodingsEnum e : EncodingsEnum.values()) {
            assertFalse(numbers.contains(e.getEncodingNumber()));
            numbers.add(e.getEncodingNumber());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an enum, which looks like public enum Animal { ELEPHANT, GIRAFFE, TURTLE,
I have in my database a coloumn say agent which can have enum values
I have the following enum which represents a CardRank public enum CardRank { DEUCE('2'),
Can I have an enum which acts as a key value pair. public enum
I have an enum which is defined like this: public enum eRat { A
Say I have an enum which is just public enum Blah { A, B,
need help I have this enum which sets the PayClassNo to Direct and Indirect.
I have my enum, which I don't want to share between layers. So I
If I have an enum with multiple values which can be present at the
how do i switch on an enum which have the flags attribute set (or

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.