J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 5018254 |
| 27 | * @summary Test null-allowing Comparators |
| 28 | * @author Martin Buchholz |
| 29 | */ |
| 30 | |
| 31 | import java.util.*; |
| 32 | import java.util.concurrent.*; |
| 33 | |
| 34 | public class NullAtEnd { |
| 35 | static volatile int passed = 0, failed = 0; |
| 36 | |
| 37 | static void fail(String msg) { |
| 38 | failed++; |
| 39 | new AssertionError(msg).printStackTrace(); |
| 40 | } |
| 41 | |
| 42 | static void pass() { |
| 43 | passed++; |
| 44 | } |
| 45 | |
| 46 | static void unexpected(Throwable t) { |
| 47 | failed++; |
| 48 | t.printStackTrace(); |
| 49 | } |
| 50 | |
| 51 | static void check(boolean condition, String msg) { |
| 52 | if (condition) |
| 53 | passed++; |
| 54 | else |
| 55 | fail(msg); |
| 56 | } |
| 57 | |
| 58 | static void check(boolean condition) { |
| 59 | check(condition, "Assertion failure"); |
| 60 | } |
| 61 | |
| 62 | private static boolean eq(Object x, Object y) { |
| 63 | return x == null ? y == null : x.equals(y); |
| 64 | } |
| 65 | |
| 66 | private static final Comparator<String> NULL_AT_END |
| 67 | = new Comparator<String>() { |
| 68 | /** |
| 69 | * Allows for nulls. Null is greater than anything non-null. |
| 70 | */ |
| 71 | public int compare(String x, String y) { |
| 72 | if (x == null && y == null) return 0; |
| 73 | if (x == null && y != null) return 1; |
| 74 | if (x != null && y == null) return -1; |
| 75 | return x.compareTo(y); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | public static void main(String[] args) { |
| 80 | try { |
| 81 | SortedMap<String,String> m1 |
| 82 | = new TreeMap<String,String>(NULL_AT_END); |
| 83 | check(eq(m1.put("a", "a"), null)); |
| 84 | check(eq(m1.put("b", "b"), null)); |
| 85 | check(eq(m1.put("c", "c"), null)); |
| 86 | check(eq(m1.put(null, "d"), null)); |
| 87 | |
| 88 | SortedMap<String,String> m2 = new TreeMap<String,String>(m1); |
| 89 | |
| 90 | check(eq(m1.lastKey(), null)); |
| 91 | check(eq(m1.get(m1.lastKey()), "d")); |
| 92 | check(eq(m1.remove(m1.lastKey()), "d")); |
| 93 | check(eq(m1.lastKey(), "c")); |
| 94 | |
| 95 | check(eq(m2.entrySet().toString(), "[a=a, b=b, c=c, null=d]")); |
| 96 | |
| 97 | SortedMap<String,String> m3 = m2.tailMap("b"); |
| 98 | |
| 99 | check(eq(m3.lastKey(), null)); |
| 100 | check(eq(m3.get(m3.lastKey()), "d")); |
| 101 | check(eq(m3.remove(m3.lastKey()), "d")); |
| 102 | check(eq(m3.lastKey(), "c")); |
| 103 | |
| 104 | } catch (Throwable t) { unexpected(t); } |
| 105 | |
| 106 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 107 | if (failed > 0) throw new Error("Some tests failed"); |
| 108 | } |
| 109 | } |