Java Array to String toString(int[] codes)

Here you can find the source of toString(int[] codes)

Description

Translates array of key codes into corresponding string representation.

License

Apache License

Parameter

Parameter Description
codes a parameter

Return

string representation of given key codes

Declaration

public static String toString(int[] codes) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.HashMap;
import java.util.Map;

public class Main {
    private static final char separator = '+';
    private static final Map<Integer, String> codeToName = new HashMap<>();
    private static final Map<String, String> internalToExternalName = new HashMap<>();

    /**/*from w w w  . j  a  va 2s  .  c  o  m*/
     * Translates array of key codes into corresponding string representation. 
     * @param codes
     * @return string representation of given key codes
     */
    public static String toString(int[] codes) {
        if (codes.length == 0) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        buffer.append(toString(codes[0]));
        for (int i = 1; i < codes.length; i++) {
            String s = toString(codes[i]);
            if (s != null) {
                buffer.append(separator).append(s);
            }
        }
        return buffer.toString();
    }

    /**
     * Creates string representation of separate character (e.g. {@code Ctrl}, {@code Alt} etc).  
     * @param code - code of the character
     * @return string representation. 
     */
    public static String toString(int code) {
        String internalName = codeToName.get(code);
        if (internalName == null) {
            return null;
        }
        String externalName = internalToExternalName.get(internalName);
        if (externalName == null) {
            externalName = internalName;
        }
        return capitalize(externalName);
    }

    private static String capitalize(String str) {
        String prefix = str.substring(0, 1).toUpperCase();
        String suffix = str.length() > 1 ? str.substring(1).toLowerCase() : "";
        return prefix + suffix;
    }
}

Related

  1. toString(Class[] modelPath)
  2. toString(final byte[] buffer, final int offset, final int length)
  3. toString(final Object[] array)
  4. toString(int X[][])
  5. toString(int[] a, int maxValues)
  6. toString(int[] v, char delim)
  7. toString(long[] dom)
  8. toString(Map arg)
  9. toString(Object array)