FileDocCategorySizeDatePackage
MorseCodeConverter.javaAPI DocAndroid 1.5 API5732Wed May 06 22:41:08 BST 2009com.example.android.apis.os

MorseCodeConverter

public class MorseCodeConverter extends Object
Class that implements the text to morse code coversion

Fields Summary
private static final long
SPEED_BASE
static final long
DOT
static final long
DASH
static final long
GAP
static final long
LETTER_GAP
static final long
WORD_GAP
private static final long[]
LETTERS
The characters from 'A' to 'Z'
private static final long[]
NUMBERS
The characters from '0' to '9'
private static final long[]
ERROR_GAP
Constructors Summary
Methods Summary
static long[]pattern(char c)
Return the pattern data for a given character


             
        
        if (c >= 'A" && c <= 'Z") {
            return LETTERS[c - 'A"];
        }
        if (c >= 'a" && c <= 'z") {
            return LETTERS[c - 'a"];
        }
        else if (c >= '0" && c <= '9") {
            return NUMBERS[c - '0"];
        }
        else {
            return ERROR_GAP;
        }
    
static long[]pattern(java.lang.String str)

        boolean lastWasWhitespace;
        int strlen = str.length();

        // Calculate how long our array needs to be.
        int len = 1;
        lastWasWhitespace = true;
        for (int i=0; i<strlen; i++) {
            char c = str.charAt(i);
            if (Character.isWhitespace(c)) {
                if (!lastWasWhitespace) {
                    len++;
                    lastWasWhitespace = true;
                }
            } else {
                if (!lastWasWhitespace) {
                    len++;
                }
                lastWasWhitespace = false;
                len += pattern(c).length;
            }
        }

        // Generate the pattern array.  Note that we put an extra element of 0
        // in at the beginning, because the pattern always starts with the pause,
        // not with the vibration.
        long[] result = new long[len+1];
        result[0] = 0;
        int pos = 1;
        lastWasWhitespace = true;
        for (int i=0; i<strlen; i++) {
            char c = str.charAt(i);
            if (Character.isWhitespace(c)) {
                if (!lastWasWhitespace) {
                    result[pos] = WORD_GAP;
                    pos++;
                    lastWasWhitespace = true;
                }
            } else {
                if (!lastWasWhitespace) {
                    result[pos] = LETTER_GAP;
                    pos++;
                }
                lastWasWhitespace = false;
                long[] letter = pattern(c);
                System.arraycopy(letter, 0, result, pos, letter.length);
                pos += letter.length;
            }
        }
        return result;