test/java/util/function/BinaryOperator/BasicTest.java

Print this page
rev 7914 : 8023528: Rename Comparator combinators to disambiguate overloading methods
Reviewed-by:


  50         final String lastName;
  51         final int age;
  52 
  53         People(String first, String last, int age) {
  54             firstName = first;
  55             lastName = last;
  56             this.age = age;
  57         }
  58 
  59         String getFirstName() { return firstName; }
  60         String getLastName() { return lastName; }
  61         int getAge() { return age; }
  62     }
  63 
  64     private final People people[] = {
  65         new People("John", "Doe", 34),
  66         new People("Mary", "Doe", 30),
  67     };
  68 
  69     public void testMaxBy() {
  70         Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName);
  71         // lesser
  72         assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
  73         // euqal
  74         cmp = Comparator.comparing((Function<People, String>) People::getLastName);
  75         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
  76         // greater
  77         cmp = Comparator.comparing((ToIntFunction<People>) People::getAge);
  78         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
  79     }
  80 
  81     public void testLesserOf() {
  82         Comparator<People> cmp = Comparator.comparing((Function<People, String>) People::getFirstName);
  83         // lesser
  84         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
  85         // euqal
  86         cmp = Comparator.comparing((Function<People, String>) People::getLastName);
  87         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
  88         // greater
  89         cmp = Comparator.comparing((ToIntFunction<People>) People::getAge);
  90         assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
  91     }
  92 
  93     public void testNulls() {
  94         try {
  95             BinaryOperator<String> op = minBy(null);
  96             fail("minBy(null) should throw NPE");
  97         } catch (NullPointerException npe) {}
  98 
  99         try {
 100             BinaryOperator<String> op = maxBy(null);
 101             fail("maxBy(null) should throw NPE");
 102         } catch (NullPointerException npe) {}
 103     }
 104 }


  50         final String lastName;
  51         final int age;
  52 
  53         People(String first, String last, int age) {
  54             firstName = first;
  55             lastName = last;
  56             this.age = age;
  57         }
  58 
  59         String getFirstName() { return firstName; }
  60         String getLastName() { return lastName; }
  61         int getAge() { return age; }
  62     }
  63 
  64     private final People people[] = {
  65         new People("John", "Doe", 34),
  66         new People("Mary", "Doe", 30),
  67     };
  68 
  69     public void testMaxBy() {
  70         Comparator<People> cmp = Comparator.comparing(People::getFirstName);
  71         // lesser
  72         assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
  73         // euqal
  74         cmp = Comparator.comparing(People::getLastName);
  75         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
  76         // greater
  77         cmp = Comparator.comparingInt(People::getAge);
  78         assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
  79     }
  80 
  81     public void testMinBy() {
  82         Comparator<People> cmp = Comparator.comparing(People::getFirstName);
  83         // lesser
  84         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
  85         // euqal
  86         cmp = Comparator.comparing(People::getLastName);
  87         assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
  88         // greater
  89         cmp = Comparator.comparingInt(People::getAge);
  90         assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
  91     }
  92 
  93     public void testNulls() {
  94         try {
  95             BinaryOperator<String> op = minBy(null);
  96             fail("minBy(null) should throw NPE");
  97         } catch (NullPointerException npe) {}
  98 
  99         try {
 100             BinaryOperator<String> op = maxBy(null);
 101             fail("maxBy(null) should throw NPE");
 102         } catch (NullPointerException npe) {}
 103     }
 104 }