Guava Ranges.asSet listing infinite list

I am trying to get a date range using the new Range function from Guava via

Range<Date> dateRange = Ranges.range(start, BoundType.CLOSED, end, BoundType.CLOSED);

My goal is to get a watch in this date range. So I created DiscreteDomain, for example:

private static final DiscreteDomain<Date> HOURS = new DiscreteDomain<Date>() {

    public Date next(Date value) {
        return addHours(value, 1);
    }

    private Date addHours(Date value, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(value);
        cal.add(Calendar.HOUR_OF_DAY, i);
        return cal.getTime();
    }

    public Date previous(Date value) {
        return addHours(value, -1);
    }

    public long distance(Date start, Date end) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(start);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(end);

        return cal2.getTimeInMillis() - cal1.getTimeInMillis();
    }

    public Date minValue() {
        return new Date(Long.MIN_VALUE);
    }

    public Date maxValue() {
        return new Date(Long.MAX_VALUE);
    }
};

If I just exit the output, I get a closed set

[Thu Feb 24 00:00:00 EST 2011..Thu Feb 24 00:02:00 EST 2011]

I really want to see every hour in the range, so I try the for loop:

for (Date hour : hours) {
    System.out.println(hour);
}

When starting this block, I seem to get an infinite set, starting on the left side of the range, but not stopping on the right side, forcing me to kill the IDE. What am I doing wrong?

+3
source share
2 answers

I think this may be due to the behavior Iteratorbeing returned ContiguousSet(returned Range.asSet()):

  @Override public UnmodifiableIterator<C> iterator() {
    return new AbstractLinkedIterator<C>(first()) {
      final C last = last();

      @Override
      protected C computeNext(C previous) {
        return equalsOrThrow(previous, last) ? null : domain.next(previous);
      }
    };
  }

  private static boolean equalsOrThrow(Comparable<?> left,
      @Nullable Comparable<?> right) {
    return right != null && compareOrThrow(left, right) == 0;
  }

  private static int compareOrThrow(Comparable left, Comparable right) {
    return left.compareTo(right);
  }

, .

Thu Feb 24 02:00:00 Thu Feb 24 00:02:00 ?

, , , , , equalsOrThrow(), left <= right left == right


, distance() . , , .


, , , DiscreteDomain's javadoc:

; " " "" 5.

, . , , . , equalsOrThrow "" .

+6

, . @eneveu distance. , start end, , Date, end, start.

, , . Javadoc DiscreteDomain :

; , " " " 5".

A DiscreteDomain "" Date .

+3

All Articles