8 Replies Latest reply on Mar 30, 2010 10:46 PM by nickarls

    Qualifier with one or more values

    awestwel

      Hey All


      I want to use the @Observes with a qualifier but have it fire the event if one of the list values match the event value.


      For example


      If I have a qualifier that stores a list of strings and the value that is fired is listed then fire the event.


      Qualifier values First, Second, Forth


      if the event fired Second the @Observer event would get executed. But if the event value that was fired was Third it would not be fired.


      Is this possible anyone have an example?


      Thanks


        • 1. Re: Qualifier with one or more values
          awestwel

          Just to clarify I have a qualifier hold a single string value. But I want the event to fire if the value matches one of the values. So in the example if the qualifier values is First or Second


          Thanks

          • 2. Re: Qualifier with one or more values
            nickarls

            I'm not following, could you please write some code and explain what is happening and what you expected to happen. If you want to control what is fired, wrap the firing in a method that checks the incoming data if to fire?

            • 3. Re: Qualifier with one or more values
              awestwel

              Hey Nicklas


              Sorry for the confusion regarding my question :) So if I have the following qualifier




              @Retention(RUNTIME)
              @Target({METHOD, TYPE})
              @Documented
              @Qualifier
              public @interface TestQualifier {
                 String value();
              }



              Then in my manager class I want to observer an event when value has one or more values that match. Is this possible




              @Singleton
              public class TestManager {
              
                  private void transactionEvent(@Observes @TestQualifier({"First", Second"}) {
                  }
              
              }



              Is this possible with the CDI event bus?



              • 4. Re: Qualifier with one or more values
                nickarls

                For qualifiers with binding members the equals() is called for the value. If you want more sophisticated stuff, you can fire or observe conditionally by examining the value and determine what to do yourself.

                • 5. Re: Qualifier with one or more values
                  awestwel

                  How would I observe conditionally?

                  • 6. Re: Qualifier with one or more values
                    nickarls

                    Normal observe but you stick an if around the stuff you are about to do and check for a more specific condition. Or just exit early if the conditions are not met.

                    • 7. Re: Qualifier with one or more values
                      awestwel

                      I did not know you could add code conditions around an argument? Code you provide a sample on how this would look based on the code below




                      private void transactionEvent(@Observes @TestQualifier({"First", Second"}) {
                      }
                      



                      • 8. Re: Qualifier with one or more values
                        nickarls

                        not around the argument. see the difference:


                        private void doStuff(String weekday) 
                        {
                          allWorkAndNoPlay();
                        }
                        



                        private void doStuff(String weekday) 
                        {
                          if ("monday".equals(weekday))
                          {
                             allWorkAndNoPlay();
                          }
                        }
                        



                        An observer method is just a method, a parameter is just a parameter