简体   繁体   English

java 8 - 如何使用方法抽象谓词的使用

[英]java 8 - how abstracts the use of predicates by using methods

I am trying to create a class that abstracts the use of predicates from its end user. 我正在尝试创建一个抽象使用最终用户谓词的类。

My app uses Guava-Retrying extension which works great. 我的应用程序使用Guava-Retrying扩展,效果很好。

Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
     .retryIfResult(Predicates.<Boolean>isNull())
     ....
     .build();
retryer.call(callable);

I can call easily call it with predicates and it polls until predicate returns false. 我可以使用谓词轻松调用它并轮询直到谓词返回false。

Now, maybe I misunderstand predicates but I am trying to create a class that will abstract them. 现在,也许我误解了谓词,但我正在尝试创建一个抽象它们的类。

I wish to call it as follows 我想称之为如下

MyPoller.poll(new PollCondition<MyClass>() {
        @Override public boolean condition() {
            return !isValid(result**.getPermissions(), permissionName);
        }
    });

So I wrote PollCondition class as follows. 所以我写了PollCondition类如下。

public abstract class PollCondition<T> {
  public Predicate<T> getCondition() {
      return result -> condition();
  }
  public abstract boolean condition();
} 

but MyPoller.poll() call fail to compile - result is not declared. 但MyPoller.poll()调用无法编译 - 未声明结果。

Any idea? 任何的想法?

It seems you don't understand predicates indeed. 看来你确实不理解谓词。 A predicate is a function that takes an input as argument, and returns a boolean value (usually based on the input, of course). 谓词是一个以输入为参数的函数,并返回一个布尔值(当然,通常基于输入)。

Let's examine your PollCondition class: 让我们来看看你的PollCondition类:

public abstract class PollCondition<T> {
  public Predicate<T> getCondition() {
      return result -> condition();
  }
  public abstract boolean condition();
} 

So it defines an abstract condition() method that doesn't take anything as argument, and returns a boolean. 所以它定义了一个抽象的condition()方法,它不接受任何参数,并返回一个布尔值。 And it can be "transformed" into a Predicate using getCondition() . 并且可以使用getCondition()将其“转换”为谓词。 This method returns a predicate which takes an input as argument (result), ignores it completely, and always returns the boolean returned by condition() . 此方法返回一个谓词,该谓词将输入作为参数(结果),完全忽略它,并始终返回condition()返回的布尔值。

You then create a PollCondition using 然后使用创建PollCondition

new PollCondition<MyClass>() {
    @Override public boolean condition() {
        return !isValid(result.getPermissions(), permissionName);
    }
}

That would be correct if, in the scope where you execute that code, there was a variable named result . 如果在执行该代码的范围内有一个名为result的变量,那将是正确的。 But there is not. 但事实并非如此。 result is in fact an input to your condition. result实际上是对你的病情的输入。 So the class should in fact defined like this: 所以这个类实际上应该像这样定义:

public abstract class PollCondition<T> {
  public Predicate<T> getCondition() {
      return result -> condition(result);
  }
  public abstract boolean condition(T result);
} 

And you would then be able to instantiate one using 然后你就可以使用它来实例化一个

new PollCondition<MyClass>() {
    @Override public boolean condition(MyClass result) {
        return !isValid(result.getPermissions(), permissionName);
    }
}

But I really, really don't see what that brings over using a simple Predicate directly. 但我真的,真的没有看到直接使用简单谓词带来的东西。

Define MyPoller.poll() like this: 像这样定义MyPoller.poll():

public poll(Predicate<T> predicate);

and use it like this: 并像这样使用它:

MyPoller.poll(result -> !isValid(result.getPermissions(), permissionName));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM