mirror of
https://github.com/tiennm99/lombok.git
synced 2026-10-14 02:16:18 +00:00
Oder equals/hashcode fields by rank or position
This commit is contained in:
1 parent
a2941041d4
commit
0fd663bb99
3 files changed
+36
-13
No files matched your search
@@ -34,6 +34,7 @@ Robbert Jan Grootjans <grootjans@gmail.com>
|
||||
Robert Wertman <robert.wertman@gmail.com>
|
||||
Roel Spilker <r.spilker@gmail.com>
|
||||
Roland Praml <pram@gmx.de>
|
||||
Samuel Pereira <samuel.p.araujo@gmail.com>
|
||||
Sander Koning <askoning@gmail.com>
|
||||
Szymon Pacanowski <spacanowski@gmail.com>
|
||||
Taiki Sugawara <buzz.taiki@gmail.com>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2009-2018 The Project Lombok Authors.
|
||||
* Copyright (C) 2009-2020 The Project Lombok Authors.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -122,5 +122,12 @@ public @interface EqualsAndHashCode {
|
||||
* @return If present, this method serves as replacement for the named field.
|
||||
*/
|
||||
String replaces() default "";
|
||||
|
||||
/**
|
||||
* Higher ranks are considered first. Members of the same rank are considered in the order they appear in the source file.
|
||||
*
|
||||
* @return ordering within the generating {@code equals} and {@code hashCode} methods; higher numbers are considered first.
|
||||
*/
|
||||
int rank() default 0;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2009-2018 The Project Lombok Authors.
|
||||
* Copyright (C) 2009-2020 The Project Lombok Authors.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -207,22 +207,37 @@ public class InclusionExclusionUtils {
|
||||
@Override public int compare(Included<L, ToString.Include> a, Included<L, ToString.Include> b) {
|
||||
int ra = a.getInc() == null ? 0 : a.getInc().rank();
|
||||
int rb = b.getInc() == null ? 0 : b.getInc().rank();
|
||||
if (ra < rb) return +1;
|
||||
if (ra > rb) return -1;
|
||||
|
||||
int pa = a.getNode().getStartPos();
|
||||
int pb = b.getNode().getStartPos();
|
||||
|
||||
if (pa < pb) return -1;
|
||||
if (pa > pb) return +1;
|
||||
|
||||
return 0;
|
||||
|
||||
return compareRankOrPosition(ra, rb, a.getNode(), b.getNode());
|
||||
}
|
||||
});
|
||||
return members;
|
||||
}
|
||||
|
||||
public static <A extends AST<A, L, N>, L extends LombokNode<A, L, N>, N> List<Included<L, EqualsAndHashCode.Include>> handleEqualsAndHashCodeMarking(LombokNode<A, L, N> typeNode, AnnotationValues<EqualsAndHashCode> annotation, LombokNode<A, L, N> annotationNode) {
|
||||
return handleIncludeExcludeMarking(EqualsAndHashCode.Include.class, "replaces", EqualsAndHashCode.Exclude.class, typeNode, annotation, annotationNode, false);
|
||||
List<Included<L, EqualsAndHashCode.Include>> members = handleIncludeExcludeMarking(EqualsAndHashCode.Include.class, "replaces", EqualsAndHashCode.Exclude.class, typeNode, annotation, annotationNode, false);
|
||||
|
||||
Collections.sort(members, new Comparator<Included<L, EqualsAndHashCode.Include>>() {
|
||||
@Override public int compare(Included<L, EqualsAndHashCode.Include> a, Included<L, EqualsAndHashCode.Include> b) {
|
||||
int ra = a.getInc() == null ? 0 : a.getInc().rank();
|
||||
int rb = b.getInc() == null ? 0 : b.getInc().rank();
|
||||
|
||||
return compareRankOrPosition(ra, rb, a.getNode(), b.getNode());
|
||||
}
|
||||
});
|
||||
return members;
|
||||
}
|
||||
|
||||
private static <A extends AST<A, L, N>, L extends LombokNode<A, L, N>, N> int compareRankOrPosition(int ra, int rb, LombokNode<A, L, N> nodeA, LombokNode<A, L, N> nodeB) {
|
||||
if (ra < rb) return +1;
|
||||
if (ra > rb) return -1;
|
||||
|
||||
int pa = nodeA.getStartPos();
|
||||
int pb = nodeB.getStartPos();
|
||||
|
||||
if (pa < pb) return -1;
|
||||
if (pa > pb) return +1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user