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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:01:21+00:00 2026-06-15T06:01:21+00:00

I am searching for a way to build a wrapper for multiple enums. Say

  • 0

I am searching for a way to build a wrapper for multiple enums. Say you have

public enum Enum1 {
    A,B,C
}

public enum Enum2 {
    ONE,TWO,THREE
}

I want to have a new enum with the literals

(A,ONE), (A,TWO), (A,THREE), (B,ONE), ...

The whole thing generic so that I don’t have to know Enum1 and Enum2. Is there a way to build that or even extend it to n Enums?

Or should I look to other general ways to model that?

  • 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-15T06:01:22+00:00Added an answer on June 15, 2026 at 6:01 am

    And here’s one that will wrap any number of enums. It comes as an Iterator but combine it with either or both of the other solutions and I think you’ve got all you asked for.

    public class Test {
      public static void main(String args[]) {
        new Test().test();
      }
    
      public static class EnumIterator implements Iterator<Enum[]> {
        // The enums
        private final Enum[][] enums;
        // Where we are in each column.
        private final int[] is;
        // Which column to increment next.
        private int i = 0;
    
        // Construct from Enum[]s.
        public EnumIterator(Enum[]... enums) {
          // Grab the enums.
          this.enums = enums;
          // Start all ordinals at zero.
          is = new int[enums.length];
          // Next one to increment is the last one.
          i = enums.length - 1;
        }
    
        // Construct from classes.
        public EnumIterator(Class<? extends Enum>... classes) {
          this(enumsFromClasses(classes));
        }
    
        // Factory to build the Enum[] array from an array of classes.
        private static Enum[][] enumsFromClasses(Class<? extends Enum>[] classes) {
          Enum[][] theEnums = new Enum[classes.length][];
          for ( int j = 0; j < classes.length; j++ ) {
            theEnums[j] = classes[j].getEnumConstants();
          }
          return theEnums;
        }
    
        @Override
        public boolean hasNext() {
          // We stop when we are about to increment col 0 and we are at its end.
          return (i > 0 || is[0] < enums[0].length);
        }
    
        @Override
        public Enum[] next() {
          if (hasNext()) {
            // One from each.
            Enum[] next = new Enum[enums.length];
            for (int j = 0; j < next.length; j++) {
              next[j] = enums[j][is[j]];
            }
            // Step - Kinda like incrementing a number with each digit in a different base.
            // Walk back past '9's setting them to 0.
            for (i = is.length - 1; i > 0 && is[i] == enums[i].length - 1; i--) {
              // Back one.
              is[i] = 0;
            }
            // Step that one up one.
            is[i] += 1;
            return next;
          } else {
            throw new NoSuchElementException();
          }
        }
    
        @Override
        public void remove() {
          throw new UnsupportedOperationException("Not supported.");
        }
      }
    
      enum ABC {
        A, B, C;
      }
    
      enum XY {
        X, Y;
      }
    
      enum IJ {
        I, J;
      }
    
      private void test() {
        // Also works - but constructing from classes is cleaner.
        //Iterator<Enum[]> i = new EnumIterator(ABC.values(), XY.values(), IJ.values());
        Iterator<Enum[]> i = new EnumIterator(ABC.class, XY.class, IJ.class);
        for (Enum[] e : Iterables.in(i)) {
          System.out.println(Arrays.toString(e));
        }
      }
    }
    

    Prints:

    [A, X, I]
    [A, X, J]
    [A, Y, I]
    [A, Y, J]
    [B, X, I]
    [B, X, J]
    [B, Y, I]
    [B, Y, J]
    [C, X, I]
    [C, X, J]
    [C, Y, I]
    [C, Y, J]
    

    Note that Iterables.in merely wraps an Iterator<E> in an Iterable<E> like this (not my code – I found it here on SO).

    public class Iterables {
      /**
       * Adapts an {@link Iterator} to an {@link Iterable} for use in enhanced for loops.
       *
       * If {@link Iterable#iterator()} is invoked more than once, an
       * {@link IllegalStateException} is thrown.
       */
      public static <T> java.lang.Iterable<T> in(final Iterator<T> iterator) {
        assert iterator != null;
        class SingleUseIterable implements java.lang.Iterable<T> {
          private boolean used = false;
    
          @Override
          public Iterator<T> iterator() {
            if (used) {
              throw new IllegalStateException("SingleUseIterable already invoked");
            }
            used = true;
            return iterator;
          }
        }
        return new SingleUseIterable();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to build an ASP.NET MVC application where many screens will have multiple
Is there a way to build three or four parts of a site (three
I'm searching a way to avoid systematic dynamic cast in the following problem. I
I'm searching a way to detect mouse and keyboards events in C# and read
i am searching a way to get Numerical gradient from the matrix. The same
You can get underground processes by ps ux I am searching a way to
I'm searching for a way to extract all text elements from a matplotlibfigure including
I am searching for a way to use git blame through smartgit UI. Is
I'm searching for a way to auto compare an object propriety to a list
I'm searching for a way to create a menu in an iPhone app that

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.