Skip to content

Commit 1aa87e0

Browse files
author
Andrey Turbanov
committed
8287148: Avoid redundant HashMap.containsKey calls in ExtendedKeyCodes.getExtendedKeyCodeForChar
Reviewed-by: prr
1 parent 74be2d9 commit 1aa87e0

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/java.desktop/share/classes/sun/awt/ExtendedKeyCodes.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,41 +24,39 @@
2424
*/
2525
package sun.awt;
2626

27-
import java.util.Collections;
2827
import java.util.HashMap;
2928
import java.util.HashSet;
3029
import java.awt.event.KeyEvent;
3130

3231
public class ExtendedKeyCodes {
3332
/**
3433
* ATTN: These are the readonly hashes with load factor == 1;
35-
* adding a value, please set the inital capacity to exact number of items
34+
* adding a value, please set the initial capacity to exact number of items
3635
* or higher.
3736
*/
3837
// Keycodes declared in KeyEvent.java with corresponding Unicode values.
39-
private static final HashMap<Integer, Integer> regularKeyCodesMap =
40-
new HashMap<Integer,Integer>(98, 1.0f);
38+
private static final HashMap<Integer, Integer> regularKeyCodesMap =
39+
new HashMap<>(98, 1.0f);
4140

4241
// Keycodes derived from Unicode values. Here should be collected codes
4342
// for characters appearing on the primary layer of at least one
4443
// known keyboard layout. For instance, sterling sign is on the primary layer
4544
// of the Mac Italian layout.
4645
private static final HashSet<Integer> extendedKeyCodesSet =
47-
new HashSet<Integer>(496, 1.0f);
48-
public static final int getExtendedKeyCodeForChar( int c ) {
46+
new HashSet<>(496, 1.0f);
47+
public static int getExtendedKeyCodeForChar( int c ) {
4948
int uc = Character.toUpperCase( c );
50-
int lc = Character.toLowerCase( c );
51-
if (regularKeyCodesMap.containsKey( c )) {
52-
if(regularKeyCodesMap.containsKey(uc)) {
53-
return regularKeyCodesMap.get( uc );
54-
}
55-
return regularKeyCodesMap.get( c );
49+
Integer regularKeyCode = regularKeyCodesMap.get(c);
50+
if (regularKeyCode != null) {
51+
return regularKeyCodesMap.getOrDefault(uc, regularKeyCode);
5652
}
5753
uc += 0x01000000;
58-
lc += 0x01000000;
5954
if (extendedKeyCodesSet.contains( uc )) {
6055
return uc;
61-
}else if (extendedKeyCodesSet.contains( lc )) {
56+
}
57+
int lc = Character.toLowerCase( c );
58+
lc += 0x01000000;
59+
if (extendedKeyCodesSet.contains( lc )) {
6260
return lc;
6361
}
6462
return KeyEvent.VK_UNDEFINED;

0 commit comments

Comments
 (0)