Path: blob/master/test/jdk/java/util/HashMap/OverrideIsEmpty.java
41152 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2013 IBM Corporation25*/2627/**28* @test29* @bug 801938130* @summary Verify that we do not get exception when we override isEmpty()31* in a subclass of HashMap32* @author [email protected]33*/3435import java.util.function.BiFunction;36import java.util.HashMap;3738public class OverrideIsEmpty {39public static class NotEmptyHashMap<K,V> extends HashMap<K,V> {40private K alwaysExistingKey;41private V alwaysExistingValue;4243@Override44public V get(Object key) {45if (key == alwaysExistingKey) {46return alwaysExistingValue;47}48return super.get(key);49}5051@Override52public int size() {53return super.size() + 1;54}5556@Override57public boolean isEmpty() {58return size() == 0;59}60}6162public static void main(String[] args) {63NotEmptyHashMap<Object, Object> map = new NotEmptyHashMap<>();64Object key = new Object();65Object value = new Object();66map.get(key);67map.remove(key);68map.replace(key, value, null);69map.replace(key, value);70map.computeIfPresent(key, new BiFunction<Object, Object, Object>() {71public Object apply(Object key, Object oldValue) {72return oldValue;73}74});75}76}77787980