Java - Same Enum for different Types for DAO

deadfire19

I'm trying to build a DAO which will handle different setting types. I was wondering if there is a neat way to do this with no chance of runtime errors.

public interface ChannelSettingDAO {
    Integer getIntegerSetting(ChannelSettingInteger channelSettingInteger);
    String getStringSetting(ChannelSettingString channelSettingString);
    Double getDoubleSetting(ChannelSettingDouble channelSettingDouble);
    void setIntegerSetting(ChannelSettingInteger channelSettingInteger, Integer value);
    void setStringSetting(ChannelSettingString channelSettingString, String value);
    void setDoubleSetting(ChannelSettingDouble channelSettingDouble, Double value);
}

public enum ChannelSettingInteger {
    CHANNEL_LOOKBACK(50);

    private Integer defaultValue;

    ChannelSettingInteger(Integer defaultValue) {
        this.defaultValue = defaultValue;
    }

    public Integer getDefaultValue() {
        return defaultValue;
    }
}

etc.. for every type of enum.

Is there any neater way to do this. I feel like I'm missing out on something, some way to give a type to an enum maybe, or some pattern I've missed out on.

At least a way to enforce that the getDefault name is the same.

Any tips?

davidxxx

Is there any neater way to do this. I feel like I'm missing out on something, some way to give a type to an enum maybe, or some pattern I've missed out on. At least a way to enforce that the getDefault name is the same.

You are right, you can use another design to better address your need.
you could introduce an interface that each enum will have to implement to have the getDefault() method. In this way, you are sure that each enum has the same base type (which one of the interface) and that getDefault() is provided since declared in the interface. By using a generic type on the interface, you allow each enum to have its own data type for getDefaultValue

Sample code :

public interface IChannelSetting<T> {

    public T getDefaultValue();

}


public enum ChannelSettingInteger implements IChannelSetting<Integer> {

    CHANNEL_LOOKBACK(50);

    private Integer defaultValue;

    ChannelSettingInteger(Integer defaultValue) {
      this.defaultValue = defaultValue;
    }

    @Override
    public Integer getDefaultValue() {
      return defaultValue;
    }
}

I don't know how you will use your DAO, but just for your personal information, if relevant for your need, you could go further by taking advantage of the common base interface to have a symmetric logic and less code in your DAO.

Indeed, you could declare just two generic methods in your DAO such as :

public interface ChannelSettingDAO {
    <T> T getSetting(IChannelSetting<T> channelSetting);
    <T> void setSetting(IChannelSetting<T> channelSetting, T value);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

getCode of Enum of different types in JAVA

Java same enum with different values

Swift - Passing different enum types for same variable to a class

Is this enum of two different types?

Java: Is there a way to get methods with different return types in a enum?

Is this possible to generically convert enum of the same type from different classes in java?

Putting enum types in different class

Is there an enum with MIME Types in Java?

Working with enum types in java

Different types of the same object

Java - Comparator Class for same attributes from different object types

Implementing multiple instances of the same generic Java interface with different generic types?

java take different list types with same method name

Java Templates, how to use two classes with the same name and different types

different size of the same enum type

Using nested enum types in Java

Java enum with multiple value types

Swift enum associated value with different types

how to pass different enum types to a method?

Map with enum key and different value types

Autofac - Resolve Service on Enum and return different types

Different input types in the same method

Comparing same types with different names

XSD different types same name

Register same class for different types

Different types of objects in the same column

Implementing Same Function for Different Types

Serializing a class with a generic Enum that can be different Enum types

Java - Two methods with same name, same argument of different type, but the types are related hierarchically