1

So I have these classes:

public class DeviceInState implements MyInterface<Device> {

    private List<DeviceState> toStates(String statesString) {
        List<String> states = Lists.newArrayList(statesString.split(","));

        return states.stream().map(DeviceState::valueOf).collect(Collectors.toList());
    }
}

  
public class DeviceHistoryInState implements MyInterface<DeviceHistory> {
    private List<EventType> toStates(String statesString) {
        List<String> states = Lists.newArrayList(statesString.split(","));

        return states.stream().map(EventType::valueOf).collect(Collectors.toList());
    }
}

And these enums:

public enum EventType{
    NEW("N"), ACTIVE("A"), INACTIVE("I");
}
    
public enum DeviceState{
    REGISTRATED("R"), SUSPENDED("S"), DELETED("D");
}

The differences are:

  • DeviceInState implements MyInterface<Device>; but DeviceHistoryInState implements MyInterface<DeviceHistory>

  • DeviceState::valueOf is called in DeviceInState; but EventTypes::valueOf is called in DeviceHistoryInState

I have a couple of other classes like these so I would like to make a generic one. But I have no idea whether or not it is possible. How might I parameterize my classes or methods in a way that I can call the ::valueOf method?

Thanks in advance.

7
  • DeviceState and EventType are enums that are the element types of the list returned by the toStates method, which is overriden. Both classes implement the MyInterface interface. Could you show that interface? Commented Oct 1, 2020 at 15:28
  • Actually not these methods are overridden. These methods are just called from the overridden method. Well, if it's important for you... These codes are just part of the class. The interface I use is actually the Specification from Spring. I changed it because I don't think it's important. docs.spring.io/spring-data/data-jpa/docs/current/api/org/… Commented Oct 1, 2020 at 15:44
  • 1
    Pass the class as parameter stackoverflow.com/questions/4837190/java-generics-get-class and get enum using class stackoverflow.com/questions/26357132/… Commented Oct 1, 2020 at 15:48
  • Declare the enum type as the 2nd generic type parameter and grab it in the constructor as shown in the q&a linekd in the previous comment. You might need an abstract class where to inherit from Commented Oct 1, 2020 at 15:51
  • Thing is I can pass my enum to the class like this. But I can't call the valueOf() because it's not an enum (for the code) Commented Oct 1, 2020 at 15:56

1 Answer 1

0

This is how I would do it

public interface State< E extends Enum< E > > { … }

Then my enums would implement that…

…
public enum DeviceState implements State< DeviceState > { … }
…
public enum EventType implements State< EventType > { … }
…

…Actually not these methods are overridden. These methods are just called from the overridden method…

Considering that you annotated the methods in your snippet with @Overrides, I found that to be a pretty confusing comment. To make things a little less confusing for myself, my experimental implementation does actually override something…

public class DeviceInState extends ToStateable< DeviceState > implements Specification< Device > {

    @Override
    public List< DeviceState > toStates( String statesString ) {
        
        List< String > states = asList( statesString.split( "," ) );

        return states.stream( ).map( String::trim ).map( DeviceState::valueOf ).collect(Collectors.toList());
    }
}

I was able to call that successfully like…

…
ToStateable< DeviceState > deviceToStateable = new DeviceInState( );
      
List< DeviceState > deviceStates = deviceToStateable.toStates( "SUSPENDED,REGISTERED,DELETED" );
      
ToStateable< EventType > eventToStateable = new DeviceHistoryInState( );
      
List< EventType > eventTypes = eventToStateable.toStates( "INACTIVE, NEW, ACTIVE"  );
…

Printing those objects out I could see…

                                  [SUSPENDED, REGISTERED, DELETED]
                                           [INACTIVE, NEW, ACTIVE]
                                                         SUSPENDED
                                             EXPERIMENT SUCCESSFUL
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.