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

The Archive Base Latest Questions

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

I have the following interface public interface DeviceKey { String getKey(); } I also

  • 0

I have the following interface

public interface DeviceKey {
    String getKey();
}

I also have various enums that all extends this interface.

In a class that contains all the enums I want a method that based on a string (key), can return the enum corresponding to this string. The string is related to an enum, but it is not the name. My class and enum could look like this:

public class Settings {
    private static final Map<String, DeviceKey> lookupAll = Maps.newHashMap();
    static {
        lookupAll.putAll(SmartSetting.lookup);
        // Plus a lot more similar to these
    }

    public static DeviceKey valueOfAnyKey(String key) {
        return lookupAll.get(key);
    }

    public enum SmartSetting implements DeviceKey {
        STATUS("smart_status");

        private static final Map<String, SmartSetting> lookup = EnumUtil.addAll(SmartSetting.class);
        private final String key;

        SmartEncryptionSetting(String key) {
            this.key = key;
        }

        @Override
        public String getKey() {
            return key;
        }
    }
}

The current implementation of valueOfAnyKey() returns DeviceKey which of course is not an enum. What should I do in order to make valueOfAnyKey() return an enum of the type DeviceKey?

The EnumUtil:

private static class EnumUtil {

    public static <T extends Enum<T> & DeviceKey> Map<String, T> addAll(Class<T> theClass) {
        final Map<String, T> retval = new HashMap<String, T>();
        for(T s : EnumSet.allOf(theClass)) {
            retval.put(s.getKey(), s);
        }
        return retval;
    }
}
  • 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-26T16:45:10+00:00Added an answer on May 26, 2026 at 4:45 pm

    In Java a method can only return one type. You cannot have it return DeviceKey “and” Enum

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes public interface InterfaceBase { } public class ImplementA:InterfaceBase {
Say I have the following: namespace SomeProject { public interface ISomething { string SomeMethod();
Let's say that I have the following interface: public interface IMyService { void SimpleMethod(int
I have the following: public interface ICartItem { string Name { get; set; }
I have the following interface: public interface ILogger { void Debug(string message, params object[]
I have defined the following interface: public interface IHaveAProblem { string Issue { get;
I have following interface public interface IBuilder<T> { T Create(string param); } with many
I have the following interface: public interface IRegisterable { T Register<T>(string username, string passw)
I have the following: public interface ISubject { ... } public class Subject<T> :
I have the following class interface: class Time { public: Time( int = 0,

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.