Skip to content

Latest commit

 

History

History
75 lines (72 loc) · 2.9 KB

ExtendQueryable.md

File metadata and controls

75 lines (72 loc) · 2.9 KB

Extending Queryable

You have a function you wish was part of Queryable.

public static Queryable<String> findFirstWordsOnly(List<String> words)
{
  return Query.select(words, w -> {
    int i = w.indexOf(' ');
    if (i == -1)
    {
      return w;
    }
    else
    {
      return w.substring(0, i);
    }
  });
}

snippet source | anchor

You can add this to Queryable by implementing the com.lambda.utils.Extendable interface.

public static class CustomQuery implements Extendable<List<String>>
{
  private List<String> caller;
  @Override
  public void setCaller(List<String> caller)
  {
    this.caller = caller;
  }

snippet source | anchor

Now you can add extension methods that are not static

public Queryable<String> findFirstWordsOnly()
{
  return findFirstWordsOnly(caller);
}

snippet source | anchor

and now you can call it as such

Queryable<String> list = Queryable.as("One fish", "two fish", "red fish", "blue fish");
Queryable<String> firstWordsOnlyWithExtension = list.select(String::toUpperCase).use(CustomQuery.class)
    .findFirstWordsOnly();

snippet source | anchor

whereas previously you had to use

Queryable<String> firstWordsOnlyStatic = CustomQuery
    .findFirstWordsOnly(Query.select(list, String::toUpperCase));

snippet source | anchor

See also


Back to User Guide