0

I ran into the following code recently:

public interface Filter {
      Filter NULL_FILTER = new Filter() {
            @Override
            public Query getFilterCriteria() {
              return null;
            }
       ...
            @Override
            public void setArrayClause(ArrayClause arrayClause) {}
          };
      /** @return Filter criteria or null if not set */
      Query getFilterCriteria();
       ...
  default Filter withProjection(Set<Field> projection) {
    this.setFields(projection);
    return this;
  }
}

It is confusing to me what the purpose of this could be. Can someone explain why someone would write this code?

Joel
  • 572
  • 5
  • 18
  • 1
    Does it help if I tell you that `NULL_FILTER` is public static field? – smac89 Sep 08 '20 at 23:34
  • no. what does it mean to instantiate (new Filter()) an interface? – Joel Sep 12 '20 at 20:52
  • The interface is not being instantiated. This creates an anonymous class which implements the interface, then this class is instantiated. See https://stackoverflow.com/questions/9157784/java-interface-with-new-keyword-how-is-that-possible – smac89 Sep 12 '20 at 22:17

2 Answers2

2

Every field in an interface is implicitly static, so this isn't defining something that lives in every Filter -- it's defining one common Filter that is stored in the Filter interface's namespace, so you can just write

Filter defaultFilter = Filter.NULL_FILTER;

Nothing more complicated than that. It's not uncommon to have factory methods or constant values of an interface defined in that interface -- e.g. Comparator.naturalOrder() in Java 8.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

Previous answers have already mentioned that NULL_FILTER is static, but it is also final. Meaning NULL_FILTER is a handy constant you can use anywhere. While doing such a thing the author should ensure that it indeed behaves as a constant by making the object immutable. From the code snippet you have shared it does look immutable as neither getFilterCriteria nor setArrayClause mutates it's state. Although it would have been better if setArrayClause had thrown something like UnsupportedOperationException.

joy
  • 327
  • 4
  • 11
  • 1
    This is more likely an attempt to implement https://en.m.wikipedia.org/wiki/Null_object_pattern so having empty methods is the main idea – Ivan Sep 09 '20 at 03:36
  • That indeed might be the case so if there a method called doFiltering which does nothing (is empty) it would make sense, but for me it makes it more intuitive and perhaps more in line with https://en.wikipedia.org/wiki/Principle_of_least_astonishment if the setter here throws exception. – joy Sep 09 '20 at 07:08