1

A predicate (an object that is a boolean-valued function which tests its input for a condition) is generally assumed to be stateless.

What's the most appropriate name for an object which has a testing function with state?

e.g. in Java, the CountTrigger class below returns true only on the Nth time it is tested against a value that matches a desired value, and false otherwise.

 interface QuasiPredicate<T>  // what should this be renamed to?
 {
      public boolean test(T value);
 }

 class CountTrigger<T> implements QuasiPredicate<T>
 {
      // for simplicity, ignore synchronization + null-value issues
      private int remainingTriggers = 0;
      final private T testValue;

      public CountTrigger(T testValue, int count)
      {
          this.remainingTriggers = count;
          this.testValue = testValue;
      }
      @Override public boolean test(T value)
      {
          if (!this.testValue.equals(value))
              return false;
          if (this.remainingTriggers == 0)
              return false;
          if (--this.remainingTriggers == 0)
              return true;                  
      }
 }
Jason S
  • 184,598
  • 164
  • 608
  • 970

1 Answers1

1

Considering it's an interface and interfaces are implemented and not extended then I don't see the problem in your object implementing a predicate.

If you're going to put public CountTrigger(T testValue, int count) in the interface as well then maybe you need a different name. Perhaps IFiniteRule or another suitable synonym. Maybe ask at https://english.stackexchange.com/ ;-)

Community
  • 1
  • 1