Java泛型类的设计问题

Java泛型类的设计问题,java,generics,Java,Generics,我被卡住了,不确定如何正确检查/确保我的B在集合中找到的有效类型上运行 public interface B<T> { public boolean operate(T t); } public class OperateAll<T> implements B<T> { private Collection<T> collection; public OperateAll(Collection<T> coll

我被卡住了,不确定如何正确检查/确保我的B在集合中找到的有效类型上运行

public interface B<T> {
    public boolean operate(T t);
}

public class OperateAll<T> implements B<T> {
    private Collection<T> collection;

    public OperateAll(Collection<T> collection) {
        this.collection = collection;
    }

   //I need to ensure T is of type B<T>, so I can do the described if statement
    public boolean operate(T t) {
        if (t == null) {
            return false;
        }

        for (T item : reference) {
           // if !t.operate(item) return false; 
        }
        return true;
    }

}
OperateAll应该是泛型的,接受对象的集合,并且只使用集合中找到的对象类型的B

public interface B<T> {
    public boolean operate(T t);
}

public class OperateAll<T> implements B<T> {
    private Collection<T> collection;

    public OperateAll(Collection<T> collection) {
        this.collection = collection;
    }

   //I need to ensure T is of type B<T>, so I can do the described if statement
    public boolean operate(T t) {
        if (t == null) {
            return false;
        }

        for (T item : reference) {
           // if !t.operate(item) return false; 
        }
        return true;
    }

}
公共接口B{
公共布尔运算(T);
}
公共类运算符all实现B{
私人收藏;
公共运营商(集合){
this.collection=collection;
}
//我需要确保T是类型B,这样我就可以执行所描述的if语句
公共布尔运算(T){
如果(t==null){
返回false;
}
用于(T项:参考){
//如果!t.操作(项目)返回false;
}
返回true;
}
}
有关问题的澄清: 我需要这样做:

Collection<Integer> collection = new LinkedList<>();
Integer[] numbers = new Integer[]{1, 2, 3, 4, 5};
Collections.addAll(collection, numbers);
B<Integer> op = new OperateAll<>(collection);
B<Integer> validateNumber = new ValidNumber<>();
//if B<String> validateNumber, this should not be allowed as an argument.
op.operate(validateNumber);

Collection=newlinkedlist();
整数[]个数=新整数[]{1,2,3,4,5};
集合。addAll(集合,编号);
B op=新操作所有(收集);
B validatedNumber=新的ValidNumber();
//如果B validateEnumber,则不允许将其作为参数。
操作(验证枚举器);

这样,ValidateEnumber就可以检查它是否可以对op中集合中的所有项进行操作。

如果我理解正确,您就不需要对
t
进行约束。相反,
OperateAll
应该实现
B
,而不是
B

类操作全部实现B{
私人收藏;
公共运营商(集合){
this.collection=collection;
}
公共布尔运算(bt){
如果(t==null){
返回false;
}
对于(T项:集合){
如果(!t.operate(item))返回false;
}
返回true;
}
}
这使得这样的代码成为可能:

Collection<Integer> collection = new LinkedList<>();
Integer[] numbers = new Integer[]{1, 2, 3, 4, 5};
Collections.addAll(collection, numbers);
// note the type of op
B<B<Integer>> op = new OperateAll<>(collection);
B<Integer> validateNumber = x -> x > 3;
op.operate(validateNumber);
B<String> validateString = x -> x.length() > 3;
op.operate(validateString); // error
Collection=newlinkedlist();
整数[]个数=新整数[]{1,2,3,4,5};
集合。addAll(集合,编号);
//注意op的类型
B op=新操作所有(收集);
B验证枚举数=x->x>3;
操作(验证枚举器);
B validateString=x->x.length()>3;
操作(验证试验);//错误

如果我理解正确,您不需要约束
t
。相反,
OperateAll
应该实现
B
,而不是
B

类操作全部实现B{
私人收藏;
公共运营商(集合){
this.collection=collection;
}
公共布尔运算(bt){
如果(t==null){
返回false;
}
对于(T项:集合){
如果(!t.operate(item))返回false;
}
返回true;
}
}
这使得这样的代码成为可能:

Collection<Integer> collection = new LinkedList<>();
Integer[] numbers = new Integer[]{1, 2, 3, 4, 5};
Collections.addAll(collection, numbers);
// note the type of op
B<B<Integer>> op = new OperateAll<>(collection);
B<Integer> validateNumber = x -> x > 3;
op.operate(validateNumber);
B<String> validateString = x -> x.length() > 3;
op.operate(validateString); // error
Collection=newlinkedlist();
整数[]个数=新整数[]{1,2,3,4,5};
集合。addAll(集合,编号);
//注意op的类型
B op=新操作所有(收集);
B验证枚举数=x->x>3;
操作(验证枚举器);
B validateString=x->x.length()>3;
操作(验证试验);//错误

您可以这样做:

public class OperateAll<T> implements B<B<T>> {
    private final Collection<? extends T> collection;

    // Bounded wildcard is used here because this class only reads from collection
    public OperateAll(Collection<? extends T> collection) {
        this.collection = collection;
    }

    @Override
    public boolean operate(B<T> t) {
        return t != null && collection.stream().allMatch(t::operate);
    }
}


注2:我建议使用
谓词
而不是
B

您可以这样做:

public class OperateAll<T> implements B<B<T>> {
    private final Collection<? extends T> collection;

    // Bounded wildcard is used here because this class only reads from collection
    public OperateAll(Collection<? extends T> collection) {
        this.collection = collection;
    }

    @Override
    public boolean operate(B<T> t) {
        return t != null && collection.stream().allMatch(t::operate);
    }
}



注2:我建议使用
谓词
而不是
B

,这样你的B操作符只对B进行操作?你可能确实想得太多了。从您目前所展示的内容来看,我认为您只需要创建一个静态助手方法
静态布尔运算符all(clazz:Class)
,它通过
操作符运行
input
中的相关条目。(您必须传入该类,因为泛型在运行时被擦除,因此在没有此“证据”的情况下无法执行
instanceof
检查。)是的njzk2,基本上B中声明的类型也应该是集合中的类型。但我不确定如何测试Sweeper给出的答案。@Thilo这是java问题,请使用java语法,将类型放在名称之前。所以你的B运算符ALL只对B操作?你可能确实想得太多了。从您目前所展示的内容来看,我认为您只需要创建一个静态助手方法
静态布尔运算符all(clazz:Class)
,它通过
操作符运行
input
中的相关条目。(您必须传入该类,因为泛型在运行时被擦除,因此在没有此“证据”的情况下无法执行
instanceof
检查。)是的njzk2,基本上B中声明的类型也应该是集合中的类型。但我不确定如何测试Sweeper给出的答案。@Thilo这是java问题,请使用java语法,将类型放在名称之前。是的,谢谢,你知道我如何测试这个吗?集合集合=新建LinkedList();B pred=新的(集合)接受次数//无法推断参数类型谢谢你的快速回复,清洁工。它仍然无法推断类型?@NoobieProgrammer事实上,我越是看你的代码,就越不明白你想做什么。“你能展示你的最终目标吗?”你的澄清帮助了我。现在我明白你想要什么了。我已经发布了答案。谢谢大家的帮助!是的,谢谢你,你知道我如何测试这个吗?集合集合=新建LinkedList();B pred=新的(集合)接受次数//无法推断参数类型谢谢你的快速回复,清洁工。它仍然无法推断类型?@NoobieProgrammer事实上,我越是看你的代码,就越不明白你想做什么。“你能展示你的最终目标吗?”你的澄清帮助了我。现在我明白你想要什么了。我已经发布了答案。谢谢大家的帮助!请问你是怎么知道该怎么做的