Java ee interface conditional input

I have the following interface:

public interface ResultEvaluationInterface {
    public void evaluateResults(Event e);
}

      

and I want to add to my class depending on my Event.type

different classes with the same implementation. Something like that:

@Stateless
@LocalBean    
public class ResultEvaluation implements ResultEvaluationInterface {

    @Override
    public void evaluateResults(Event e) {
        switch (e.getType()) {
            case Type.Running:
               // inject and call ResultEvaluationRunningEJB.evaluateResults(e)
            case Type.Swimming:
               // inject and call ResultEvaluationSwimmingEJB.evaluateResults(e)
            default:
               throw new UnsupportedOperationException("Not supported yet.");
        }
    }

}

      

ResultEvaluationRunningEJB

and ResultEvaluationSwimmingEJB

implement the interface. Does anyone have a good idea how to do this in a good way?

+3


source to share


2 answers


If you really want to use a hardcoded if statement to switch between prod and dev events, you can use CDI Qualifiers to just insert two implementations into the Facade:

@Stateless
@LocalBean    
public class ResultEvaluationFacade {

    @Inject
    @Development
    private ResultEvalutationInterface dev;

    @Inject
    @Production
    private ResultEvalutionInterface prod;

    @Override
    public void evaluateResults(Event e) {
        switch (e.getType()) {
            case Type.Production:
               prod.evaluteResult(e);
               break;
            case Type.Development:
               dev.evaluteResult(e);
               break;
            default:
               throw new UnsupportedOperationException("Not supported yet.");
        }
    }

}

      

And define two implementations:

@Development
public class ResultEvaluationDevelopment implements ResultEvaluationInterface {
   ...
}

@Production
public class ResultEvaluationDevelopment implements ResultEvaluationInterface {
   ...
}

      

However, I would consider using a mock maven project to host two separate implementations.

Alternatively, you can use different types of CDI events, something like this.

public void observeDevEvent(@Observe DevEvent event) {
   //do stuff.
}

public void observeProdEvent(@Observe ProdEvent event) {
   //do stuff
}

      

Firing an event would look something like this:



@Inject
private Event<ProdEvent> prodEvent;

public void someMethod() {
   ProdEvent pe = new ProdEvent()
   // set some data on ProdEvent
   prodEvent.fire(pe);
}

      

Noticeable events can also work with qualifiers, so you can also add the Qualifier annotation to an event instead of implementing two different types of events.

@Inject
@Production
private Event<MyEvent> event;

      

And listen to @Prodcution events;

public void handleProdEvent(@Observer @Production MyEvent myEvent) {
    // do Stuff.
}

      

For lazy creation of beans, you can use CDI instance injection.

@Inject
private Instance<BeanA> beanA;

....

public void doStuff(Event e) {
   ...
   case Type.Production:
            //lazily evaluates and instantiatiates bean.
            beanA.get().evaluateResult(e);
}

      

+2


source


Note. I have not confirmed that this works, but you have to deal with it.

You can use the dynamic CDI event manager:

public class EventDispatcher {

    @Inject
    BeanManager beanManager;

    public void handle(MyEvents mytype) {
        beanManager.fireEvent(mytype, mytype.getQualifiyer());
    }
}

      

You can refer to your qualifiers in your event enumeration something like this:

public enum MyEvents {

    EVENTA(new EventA() {
        @Override
        public Class<? extends Annotation> annotationType() {
            return this.getClass();
        }
    }),
    EVENTB (new EventB() {
        @Override
        public Class<? extends Annotation> annotationType() {
            return this.getClass();
        }
    });

    private final Annotation annotation;

    MyEvents(Annotation annotation) {
        this.annotation = annotation;
    }
    public Annotation getQualifiyer() {
        return annotation;
    }

};

      



Qualifiers look something like this:

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER,ElementType.FIELD})
public @interface EventA {

}

      

This way, you can simply add observer methods to the beans event handling:

public class EventProcessorA {
   ...
   public void handleEvent(@Observer @BeanA MyEvents myevent) {
       ...
   }
}

      

Instead of putting 20-30 into one dispatcher with a giant switch operator.

0


source







All Articles