繁体   English   中英

为什么Java 8中的比较仍然使用int而不是Enum?

[英]Why do comparisons in java 8 still use int instead of Enum?

例如,当使用比较器时, compare方法的输出为int。 在我看来,将Enum (例如Comparison )与SmallerEqualLarger这样的值一起使用可以使代码更具可读性,并且不易出错。

// The comparison result could support methods
return compare(o1, o2).ifEqual(compare(o3, o4))

// Tests can be more readable
if(compare(o1, o2) == Equal && compare(o3, o4).isNot(Larger)) ...

// The compiler can now spot mistakes like returning a meaningless number

您仍然可以支持旧样式compare(o1, o2).toInt() <= 1compare(o1, o2).toInt() <= 1 Comparison.from(i2 - i1)

除了向后兼容性很难解决之外,是否有其他原因吗?

将返回类型从int更改为Enum将破坏所有现有的Comparators。 使用Enum代替int并没有真正的优势,尽管这可能会使您的代码更具可读性,但是如果要返回Enum值,则必须像下面这样自己做

if(o1 > o2){
  return ComparisonResult.GREATER;
}else if(o1 < o2){
  return ComparisonResult.SMALLER;
}else{
  return ComparisonResult.EQUAL;
}

否则JDK团队必须更改许多JDK类(例如String,Integer等)中的compareTo所有实现,这只是因为我们希望它更具可读性,所以这是很多不必要的工作:)

正如您和其他人已经指出的,主要原因可能是向后兼容。

为了提高可读性并避免像compare(o1, o2) < 0这样的测试代码compare(o1, o2) < 0几年前我写了一些实用程序方法。 他们没有使用枚举,也没有解决您提到的所有缺点,但是也许您仍然发现它们很有用(该库是开源的):

http://www.softsmithy.org/lib/current/docs/api/softsmithy-lib-core/org/softsmithy/lib/util/Comparables.html

例如。 给像一个参与者类:

public class Participant {
    private final String firstName;
    private final String lastName;
    private final String city;

    public Participant(String firstName, String lastName, String city) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.city = city;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getCity() {
        return city;
    }
}

然后,您可以编写这样的比较器:

public class ParticipantComparator implements Comparator<Participant> {
    @Override
    public int compare(Participant o1, Participant o2) {
        if (Comparables.isEqual(o1.getFirstName(), o2.getFirstName())) {
            if (Comparables.isEqual(o1.getLastName(), o2.getLastName())) {
                if (Comparables.isEqual(o1.getCity(), o2.getCity())) {
                    return 0;
                } else if (Comparables.isGreater(o1.getCity(), o2.getCity())) {
                    return 1;
                } else {
                    return -1;
                }
            } else {
                if (Comparables.isGreater(o1.getLastName(), o2.getLastName())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        } else {
            if (Comparables.isGreater(o1.getFirstName(), o2.getFirstName())) {
                return 1;
            } else {
                return -1;
            }
        }
    }
}

如果愿意,还可以将常量用作返回值,并将静态导入用于实用程序方法。 然后代码如下所示:

import static org.softsmithy.lib.util.Comparables.isEqual;
import static org.softsmithy.lib.util.Comparables.isGreater;

import java.util.Comparator;

public class ParticipantComparator implements Comparator<Participant> {
    private static final int EQUAL = 0;
    private static final int GREATER = 1;
    private static final int LESS = -1;
    @Override
    public int compare(Participant o1, Participant o2) {
        if (isEqual(o1.getFirstName(), o2.getFirstName())) {
            if (isEqual(o1.getLastName(), o2.getLastName())) {
                if (isEqual(o1.getCity(), o2.getCity())) {
                    return EQUAL;
                } else if (isGreater(o1.getCity(), o2.getCity())) {
                    return GREATER;
                } else {
                    return LESS;
                }
            } else {
                if (isGreater(o1.getLastName(), o2.getLastName())) {
                    return GREATER;
                } else {
                    return LESS;
                }
            }
        } else {
            if (isGreater(o1.getFirstName(), o2.getFirstName())) {
                return GREATER;
            } else {
                return LESS;
            }
        }
    }
}

您可以直接从Maven Central下载该库:

<dependency>
    <groupId>org.softsmithy.lib</groupId>
    <artifactId>softsmithy-lib-core</artifactId>
    <version>0.5</version>
</dependency>

暂无
暂无

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

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