Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8287148

Avoid redundant HashMap.containsKey calls in ExtendedKeyCodes.getExtendedKeyCodeForChar

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P5 P5
    • 19
    • 19
    • client-libs
    • b26

      There is HashMap 'regularKeyCodesMap' in sun.awt.ExtendedKeyCodes class. It contains only non-null keys and values.
      t means we can replace containsKey+get with get+null check. Or with single getOrDefault call.
      It's clearer and a bit faster.

           public static final int getExtendedKeyCodeForChar( int c ) {
               int uc = Character.toUpperCase( c );
               int lc = Character.toLowerCase( c );
               if (regularKeyCodesMap.containsKey( c )) {
                   if(regularKeyCodesMap.containsKey(uc)) {
                       return regularKeyCodesMap.get( uc );
                   }
                   return regularKeyCodesMap.get( c );
               }
               uc += 0x01000000;
               lc += 0x01000000;
               if (extendedKeyCodesSet.contains( uc )) {
                   return uc;
               }else if (extendedKeyCodesSet.contains( lc )) {
                   return lc;
               }
               return KeyEvent.VK_UNDEFINED;
           }

            aturbanov Andrey Turbanov
            aturbanov Andrey Turbanov
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: