Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import static org.junit.Assert.assertEquals;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.Iterator;
  6. import java.util.List;
  7.  
  8. import org.junit.Test;
  9.  
  10. public class LexicographicListComparator {
  11.  
  12. static class LexicographicComparator<T extends Comparable<T>> implements Comparator<List<T>> {
  13.  
  14. private final Comparator<T> elementComparator;
  15.  
  16. public LexicographicComparator(final Comparator<T> elementComparator) {
  17. this.elementComparator = elementComparator;
  18. }
  19.  
  20. public LexicographicComparator() {
  21. this(new Comparator<T>() {
  22. @Override
  23. public int compare(T o1, T o2) {
  24. return o1.compareTo(o2);
  25. }
  26. });
  27. }
  28.  
  29. @Override
  30. public int compare(List<T> l1, List<T> l2) {
  31. final Iterator<T> i1 = l1.iterator(), i2 = l2.iterator();
  32. while (i1.hasNext() && i2.hasNext()) {
  33. final int cmp = elementComparator.compare(i1.next(),i2.next());
  34. if (cmp != 0) return cmp;
  35. }
  36. if (i1.hasNext() && !i2.hasNext()) return 1;
  37. if (!i1.hasNext() && i2.hasNext()) return -1;
  38. return 0;
  39. }
  40. }
  41.  
  42. final static Comparator<Integer> DESCENDING_INTEGER_COMPARATOR = new Comparator<Integer>() {
  43. public int compare(Integer o1, Integer o2) {
  44. return -o1.compareTo(o2);
  45. }
  46. };
  47.  
  48. @Test
  49. public void equal() {
  50. final List<Integer> l1 = Arrays.asList(1, 2, 3);
  51. final List<Integer> l2 = Arrays.asList(1, 2, 3);
  52. final LexicographicComparator<Integer> c = new LexicographicComparator<Integer>();
  53. assertEquals(0,c.compare(l1, l2));
  54. }
  55.  
  56. @Test
  57. public void sameLengthAscending() {
  58. final List<Integer> l1 = Arrays.asList(1, 2, 3);
  59. final List<Integer> l2 = Arrays.asList(1, 2, 4);
  60. final LexicographicComparator<Integer> c = new LexicographicComparator<Integer>();
  61. assertEquals(-1,c.compare(l1, l2));
  62. }
  63.  
  64. @Test
  65. public void sameLengthDescending() {
  66. final List<Integer> l1 = Arrays.asList(1, 2, 3);
  67. final List<Integer> l2 = Arrays.asList(1, 2, 4);
  68. final LexicographicComparator<Integer> c = new LexicographicComparator<Integer>(DESCENDING_INTEGER_COMPARATOR);
  69. assertEquals(1,c.compare(l1, l2));
  70. }
  71.  
  72. @Test
  73. public void differentLengthAscending() {
  74. final List<Integer> l1 = Arrays.asList(1, 2, 3);
  75. final List<Integer> l2 = Arrays.asList(1, 2);
  76. final LexicographicComparator<Integer> c = new LexicographicComparator<Integer>();
  77. assertEquals(1,c.compare(l1, l2));
  78. }
  79.  
  80. @Test
  81. public void differentLengthAndValuesDescending() {
  82. final List<Integer> l1 = Arrays.asList(1, 3, 3); // we need different values, the shorter will always come first otherwise
  83. final List<Integer> l2 = Arrays.asList(1, 2);
  84. final LexicographicComparator<Integer> c = new LexicographicComparator<Integer>(DESCENDING_INTEGER_COMPARATOR);
  85. assertEquals(-1,c.compare(l1, l2));
  86. }
  87.  
  88. @Test
  89. public void differentLengthDescending() {
  90. final List<Integer> l1 = Arrays.asList(1, 2, 3);
  91. final List<Integer> l2 = Arrays.asList(1, 2);
  92. final LexicographicComparator<Integer> c = new LexicographicComparator<Integer>(DESCENDING_INTEGER_COMPARATOR);
  93. assertEquals(1,c.compare(l1, l2)); // this is counterintuitive: the shortest comes first event in this case!
  94. }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement