|
12 | 12 | */ |
13 | 13 | package org.postgresql.pljava.annotation.processing; |
14 | 14 |
|
| 15 | +import java.util.Arrays; |
15 | 16 | import java.util.Comparator; |
| 17 | +import static java.util.Comparator.comparing; |
| 18 | +import static java.util.Comparator.naturalOrder; |
| 19 | +import static java.util.Comparator.nullsFirst; |
16 | 20 |
|
17 | | -import org.postgresql.pljava.sqlgen.Lexicals.Identifier; |
| 21 | +import org.postgresql.pljava.sqlgen.Lexicals.Identifier.Simple; |
18 | 22 |
|
19 | 23 | /** |
20 | 24 | * Resolve ties in {@code Snippet} ordering in an arbitrary but deterministic |
21 | 25 | * way, for use when {@code ddr.reproducible} is set. |
22 | 26 | */ |
23 | 27 | class SnippetTiebreaker implements Comparator<Vertex<Snippet>> |
24 | 28 | { |
| 29 | + private static final Comparator<Vertex<Snippet>> VCMP; |
| 30 | + |
| 31 | + static |
| 32 | + { |
| 33 | + Comparator<Snippet> scmp = |
| 34 | + comparing(Snippet::implementorName, |
| 35 | + nullsFirst(comparing(Simple::pgFolded, naturalOrder())) |
| 36 | + ) |
| 37 | + .thenComparing(Snippet::deployStrings, Arrays::compare) |
| 38 | + .thenComparing(Snippet::undeployStrings, Arrays::compare); |
| 39 | + |
| 40 | + VCMP = comparing(v -> v.payload, scmp); |
| 41 | + } |
| 42 | + |
25 | 43 | @Override |
26 | | - public int compare( Vertex<Snippet> o1, Vertex<Snippet> o2) |
| 44 | + public int compare(Vertex<Snippet> o1, Vertex<Snippet> o2) |
27 | 45 | { |
28 | | - Snippet s1 = o1.payload; |
29 | | - Snippet s2 = o2.payload; |
30 | | - int diff; |
31 | | - Identifier.Simple s1imp = s1.implementorName(); |
32 | | - Identifier.Simple s2imp = s2.implementorName(); |
33 | | - if ( null != s1imp && null != s2imp ) |
34 | | - { |
35 | | - diff = s1imp.pgFolded().compareTo( s2imp.pgFolded()); |
36 | | - if ( 0 != diff ) |
37 | | - return diff; |
38 | | - } |
39 | | - else |
40 | | - return null == s1imp ? -1 : 1; |
41 | | - String[] ds1 = s1.deployStrings(); |
42 | | - String[] ds2 = s2.deployStrings(); |
43 | | - diff = ds1.length - ds2.length; |
44 | | - if ( 0 != diff ) |
45 | | - return diff; |
46 | | - for ( int i = 0 ; i < ds1.length ; ++ i ) |
47 | | - { |
48 | | - diff = ds1[i].compareTo( ds2[i]); |
49 | | - if ( 0 != diff ) |
50 | | - return diff; |
51 | | - } |
52 | | - assert s1 == s2 : "Two distinct Snippets compare equal by tiebreaker"; |
53 | | - return 0; |
| 46 | + return VCMP.compare(o1, o2); |
54 | 47 | } |
55 | 48 | } |
0 commit comments