简体   繁体   English

是可以在java中使用具有多个可选参数的泛型方法

[英]is to possible to have generic method in java with multiple optional parameters

My understanding is this ask of mine is NOT possible in a straight forward way.我的理解是,我的这个要求不可能直截了当。 but I want to find a solution that works.但我想找到一个有效的解决方案。

Here is how I get an Iterable for NamedNodeMap(javax package);这是我为NamedNodeMap(javax package);获取 Iterable 的方法NamedNodeMap(javax package);

private static Iterable<Node> iterableNamedNodeMap(NamedNodeMap namedNodeMap) {
        return () -> new Iterator<Node>() {

            private int index = 0;

            @Override
            public boolean hasNext() {
                return index < namedNodeMap.getLength();
            }

            @Override
            public Node next() {
                if (!hasNext())
                    throw new NoSuchElementException();
                return namedNodeMap.item(index++);
            }
        };
}

And here is the iterable for NodeList(javax)这是NodeList(javax)的可迭代对象

 private static Iterable<Node> iterableNamedNodeMap(NodeList nodeList) {
            return () -> new Iterator<Node>() {

                private int index = 0;

                @Override
                public boolean hasNext() {
                    return index < nodeList.getLength();
                }

                @Override
                public Node next() {
                    if (!hasNext())
                        throw new NoSuchElementException();
                    return nodeList.item(index++);
                }
            };
    }

Since they are pretty much identical except for the parameters, I was hoping for something like this, which of-course is not right.由于除了参数之外它们几乎相同,我希望有这样的东西,这当然是不对的。 Both NodeList and NamedNodeMap does not implement a common interface. NodeList 和 NamedNodeMap 都没有实现一个公共接口。 so what is the best way to do here.那么在这里做的最好方法是什么。

private static <T extends NodeList | NamedNodeMap> Iterable<Node> iterableNamedNodeMap(T in) {
        return () -> new Iterator<Node>() {

            private int index = 0;

            @Override
            public boolean hasNext() {
                return index < in.getLength();
            }

            @Override
            public Node next() {
                if (!hasNext())
                    throw new NoSuchElementException();
                return in.item(index++);
            }
        };

You could reduce some of the boilerplate by creating a factory method that accepts two functional interfaces, taken from NodeList or NamedNodeMap using method references:您可以通过创建一个工厂方法来减少一些样板文件,该方法接受两个函数式接口,使用方法引用从NodeListNamedNodeMap

private static Iterable<Node> iterableNodes(
    Supplier<int> lengthGetter,
    Function<int, Node> itemGetter
) {
     return () -> new Iterator<Node>() {
        private int index = 0;

        @Override
        public boolean hasNext() {
            return index < lengthGetter.get();
        }

        @Override
        public Node next() {
            if (!hasNext())
                throw new NoSuchElementException();
            return itemGetter.apply(index++);
        }
    };
}

private static Iterable<Node> iterableNamedNodeMap(NamedNodeMap namedNodeMap) {
    return iterableNodes(namedNodeMap::getLength, namedNodeMap::item);
}

private static Iterable<Node> iterableNodeList(NodeList nodeList) {
    return iterableNodes(nodeList::getLength, nodeList::item);
}

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

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