/* * Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
privatestaticint testToIdentityString() { int errors = 0; // Test null behavior try {
Objects.toIdentityString(null);
errors++;
} catch (NullPointerException npe) {
; // Expected
} // Behavior on typical objects
Object o = new Object(){};
errors += (Objects.toIdentityString(o).equals(o.toString()))? 0 : 1; // Verify object's toString *not* called
Object badToString = new Object() {
@Override public String toString() { thrownew RuntimeException();
}
};
Objects.toIdentityString(badToString); // Verify object's hashCode *not* called
Object badHashCode = new Object() {
@Override publicint hashCode() { thrownew RuntimeException("0xDEADBEFF");
}
};
Objects.toIdentityString(badHashCode); return errors;
}
privatestaticint testCompare() { int errors = 0;
String[] values = {"e. e. cummings", "zzz"};
String[] VALUES = {"E. E. Cummings", "ZZZ"};
errors += compareTest(null, null, 0); for(int i = 0; i < values.length; i++) {
String a = values[i];
errors += compareTest(a, a, 0); for(int j = 0; j < VALUES.length; j++) { int expected = Integer.compare(i, j);
String b = VALUES[j];
errors += compareTest(a, b, expected);
}
} return errors;
}
privatestaticint compareTest(String a, String b, int expected) { int errors = 0; int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER); if (Integer.signum(result) != Integer.signum(expected)) {
errors++;
System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
a, b, result, expected);
} return errors;
}
privatestaticint testRequireNonNull() { int errors = 0;
final String RNN_1 = "1-arg requireNonNull"; final String RNN_2 = "2-arg requireNonNull"; final String RNN_3 = "Supplier requireNonNull";
Function<String, String> rnn1 = s -> Objects.requireNonNull(s); Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers"); Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.