Java 6,Java,泛型,类型推断

时间:2012-06-08 08:33:07

标签: java generics guava type-inference scjp

我在Java中编写了一个实用程序方法:

public static final ImmutableSortedSet<TimeUnit> REVERSED_TIMEUNITS = ImmutableSortedSet.copyOf(
        Collections.<TimeUnit>reverseOrder(),
        EnumSet.allOf(TimeUnit.class)
);


/**
 * Get the number of ..., minutes, seconds and milliseconds
 *
 * You can specify a max unit so that you don't get days for exemple
 * and can get more than 24 hours if you want to display the result in hours
 *
 * The lowest unit is milliseconds
 * @param millies
 * @param maxTimeUnit
 * @return the result map with the higher unit first
 */
public static Map<TimeUnit,Long> getCascadingDateDiff(long millies,TimeUnit maxTimeUnit) {
    if ( maxTimeUnit == null ) {
        maxTimeUnit = TimeUnit.DAYS;
    }
    Map<TimeUnit,Long> map = new TreeMap<TimeUnit,Long>(Collections.<TimeUnit>reverseOrder());
    long restInMillies = millies;
    Iterable<TimeUnit> forUnits = REVERSED_TIMEUNITS.subSet(maxTimeUnit,TimeUnit.MICROSECONDS); // micros not included
    // compute the number of days, then number of hours, then minutes...
    for ( TimeUnit timeUnit : forUnits ) {
        long numberForUnit = timeUnit.convert(restInMillies,TimeUnit.MILLISECONDS);
        map.put(timeUnit,numberForUnit);
        restInMillies = restInMillies - timeUnit.toMillis(numberForUnit);
    }
    return map;
}

适用于:

    Map<TimeUnit,Long> map = new TreeMap<TimeUnit,Long>(Collections.reverseOrder());

但我第一次尝试

    Map<TimeUnit,Long> map = Maps.newTreeMap(Collections.reverseOrder());

我的IntelliJ没有说什么,而我的编译器说:

  

DateUtils.java:[302,48]不兼容的类型;没有类型的实例   变量K,V存在,以便java.util.TreeMap符合   java.util.Map [错误]   发现:java.util.TreeMap [ERROR]必需:   java.util.Map

没有比较器,它可以正常工作:

   Map<TimeUnit,Long> map = Maps.newTreeMap();

但我尝试过:

Map<TimeUnit,Long> map = Maps.newTreeMap(Collections.<TimeUnit>reverseOrder());

并且:

Map<TimeUnit,Long> map = Maps.newTreeMap(new Comparator<TimeUnit>() {
    @Override
    public int compare(TimeUnit timeUnit, TimeUnit timeUnit1) {
        return 0; 
    }
});

我得到了同样的错误。 所以每当我在TreeMap中使用比较器时,类型推断似乎不再起作用。 为什么呢?


番石榴方法的签名是:

  public static <C, K extends C, V> TreeMap<K, V> newTreeMap(Comparator<C> comparator)

预期的返回类型是类型,因此没有比较器,Java能够推断出K = TimeUnit和V = Long。

使用TimeUnit类型的比较器,Java知道C是TimeUnit。它还知道预期的返回类型是类型,因此K = TimeUnit,V = Long。 K扩展C受到尊重,因为TimeUnit扩展了TimeUnit(无论如何,如果你认为它是错误的话,我还尝试使用对象比较器......)

所以我只是想知道为什么类型推断在这种情况下不起作用?

1 个答案:

答案 0 :(得分:7)

像Michael Laffargue建议的那样,它是一个OpenJDK6类型的推理错误:

https://bugs.openjdk.java.net/show_bug.cgi?id=100167

http://code.google.com/p/guava-libraries/issues/detail?id=635

它在我的IntelliJ中工作正常,在版本7中使用OpenJDK,在版本6中使用其他JDK。


以下关于kutschkem的建议:

    Map<TimeUnit,Long> map = Maps.<TimeUnit,TimeUnit,Long>newTreeMap(Collections.<TimeUnit>reverseOrder());

注意<TimeUnit,TimeUnit,Long>允许显式强制输入类型参数。 请查看以下相关主题:What's this generics usage in Java? X.<Y>method()

全部谢谢