Search code examples
javagenericsjaxbjavax.xml

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);

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)

 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. 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++);
            }
        };

Solution

  • 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:

    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);
    }