apache / apache/lucenenet

Replace all "functional interfaces" with delegates and their anonymous classes with lambdas

Open
#1,062 6 comments 0 reactions 0 assignees View on GitHub
is:task
Dominant language
C#
Stars
2.4k
Forks
658
Avg merge
3d 5h
Merged PRs (30d)
9

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Task description

From reviewing code as part of fixing #715. In Java 8+, you can pass a lambda for an argument that is a "functional interface" type, which is an interface with a single abstract method (SAM). This is roughly analogous to delegates in .NET, although not as powerful as delegates. In Java, lambdas are basically just a simpler way of implementing an anonymous class for a functional interface. But since Lucene at the time targeted Java 7, they could not take advantage of this feature like we can today.

Aside: Functional interfaces in modern Java _should_ have a `@FunctionalInterface` annotation added, although this is not a requirement. SAM is sufficient.

We should review to see if I've missed any below, and compare to the latest Lucene code any interfaces or abstract classes that are still functional (have not had other methods added) and consider converting those to delegate types where it makes sense. They should still be named delegates, rather than i.e. `Action`, just without the `I` convention for interfaces. Then, we can replace many "AnonymousClass" implementations in Lucene.NET with lambdas instead.

As an example, this entire anonymous class could be replaced with `_ => true`:

```c#
private sealed class PredicateAnonymousClass : IPredicate
{
public bool Apply(object[] args)
{
return true;
}
}
```

Candidates:
- ~`ITestPoint` ([SAM in latest Lucene](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/test-framework/src/java/org/apache/lucene/tests/index/RandomIndexWriter.java#L601))~ (cannot be done due to implementations in TestIndexWriterExceptions having fields)
- `IReaderDisposedListener` ([`@FunctionalInterface` in latest Lucene](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/core/src/java/org/apache/lucene/index/IndexReader.java#L141))
- `SegmentReader.ICoreDisposedListener` ([`@FunctionalInterface` in latest Lucene as ClosedListener](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/core/src/java/org/apache/lucene/index/IndexReader.java#L141))
- `IAttributeReflector` (if generic overload is moved to an extension/external method; [`@FunctionalInterface` in latest Lucene](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/core/src/java/org/apache/lucene/util/AttributeReflector.java#L22))
- `TestRandomChains.IPredicate` (could be replaced with .NET's built-in `Predicate` delegate, as this was [changed to use `java.util.function.Predicate` in latest Lucene](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/analysis.tests/src/test/org/apache/lucene/analysis/tests/TestRandomChains.java#L43))
- `IAutomatonProvider` ([SAM in latest Lucene](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/core/src/java/org/apache/lucene/util/automaton/AutomatonProvider.java#L39))
- ~`IndexReaderWarmer` (abstract class in Lucene.NET; [`@FunctionalInterface` in latest Lucene](https://github.com/apache/lucene/blob/deae39b01dec5d212b365afd220f6df000899bf3/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java#L5717))~ (cannot be done due to SimpleMergedSegmentWarmer)

This will set us up for porting future `@FunctionalInterface`s in post-4.8 Lucene as delegates and lambdas instead of interfaces and anonymous classes.

Contributor guide

Open the contributing guide

Research direction

Start by locating the listed interfaces and their anonymous-class implementations, including IReaderDisposedListener, SegmentReader.ICoreDisposedListener, IAttributeReflector, TestRandomChains.IPredicate, and IAutomatonProvider. Compare them with the linked latest Lucene interfaces, keeping the noted ITestPoint and IndexReaderWarmer exclusions in mind. Done means applicable functional interfaces use named delegates and applicable anonymous implementations use lambdas.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
search
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.