English 中文(简体)
Java, Google Collections Library; problem with AbstractIterator?
原标题:

I am using the Google Collections library AbstractIterator to implement a generator. I ran across a problem while doing so; I ve reduced it to a more basic type and reproduced the problem. This reduction is obviously overkill for what it does, counting from 1 to numelements via an Iterable.

Essentially in the following code, the uncommented version works, and the commented one does not (provides a null element last, instead of ending on the last number).

Am I doing something wrong, or is this a problem with the library?

private Iterable<Integer> elementGenerator(final int numelements) {
  return new Iterable<Integer>() {
    @Override public Iterator<Integer> iterator() {
      return new AbstractIterator<Integer>(){
        int localcount=0;
        @Override protected Integer computeNext() {
          if (localcount++ == numelements) return endOfData();
          return localcount;
          // return (localcount++ == numelements) ? endOfData() : localcount;
        }
      };
    }
  };
}

I also tried fiddling around with the ?: arrangement (e.g., prefixing the return and comparing to +1 instead), to no avail. I poked around a bit looking for documentation about this, but didn t find anything. Obviously, the ?: syntax is only a convenience, not a necessity, but still...

最佳回答

You get a NullPointerException because of the use of the ternary operator, conditional expression, with different numerical types. Java has complex rules when mixing numerical values of different types in ternary expression: JLS Section 15.25.

Given that endOfData() is preceived to return Integer, while localcount is an int, Java unboxes the value of endOfData(). However, given that endOfData() returns a null, the unboxing operation results into a null pointer exception.

You can either continue using the if statement, or declare localcount as Integer.

问题回答

I expect the problem is with your use of the postincrement operator, in conjunction with the ternary operator as well. Because asides from that, the two snippets should be entirely equivalent - and it s hardly the AbstractIterator s fault if they re not as none of its code is being called at that point.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签