Collapses repeated consecutive characters in a CharSequence by matching against #CONSECUTIVE_CHARACTER_REGEX . - Android java.lang

Android examples for java.lang:Character

Description

Collapses repeated consecutive characters in a CharSequence by matching against #CONSECUTIVE_CHARACTER_REGEX .

Demo Code

/*/*from www . ja v a2s  .co  m*/
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
import android.content.Context;
import android.text.TextUtils;
import android.util.SparseIntArray;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{
    /** The Pattern used to match consecutive identical characters */
    private static Pattern CONSECUTIVE_CHARACTER_PATTERN = Pattern
            .compile(CONSECUTIVE_CHARACTER_REGEX);
    /** Map containing string to speech conversions. */
    private static final SparseIntArray UNICODE_MAP = new SparseIntArray();
    /**
     * Collapses repeated consecutive characters in a CharSequence by matching
     * against {@link #CONSECUTIVE_CHARACTER_REGEX}.
     *
     * @param context Context for retrieving resources
     * @param text The text to process
     * @return The text with consecutive identical characters collapsed
     */
    public static CharSequence collapseRepeatedCharacters(Context context,
            CharSequence text) {
        if (TextUtils.isEmpty(text)) {
            return null;
        }

        // TODO(caseyburkhardt): Add tests
        Matcher matcher = CONSECUTIVE_CHARACTER_PATTERN.matcher(text);
        while (matcher.find()) {
            final String replacement = context.getString(
                    R.string.character_collapse_template, matcher.group()
                            .length(),
                    getCleanValueFor(context, matcher.group().charAt(0)));
            final int matchFromIndex = matcher.end()
                    - matcher.group().length() + replacement.length();
            text = matcher.replaceFirst(replacement);
            matcher = CONSECUTIVE_CHARACTER_PATTERN.matcher(text);
            matcher.region(matchFromIndex, text.length());
        }

        return text;
    }
    /**
     * Returns the "clean" value for the specified character.
     */
    public static String getCleanValueFor(Context context, char key) {
        final int resId = UNICODE_MAP.get(key);

        if (resId != 0) {
            return context.getString(resId);
        }

        if (Character.isUpperCase(key)) {
            return context.getString(R.string.template_capital_letter,
                    Character.toString(key));
        }

        return Character.toString(key);
    }
}

Related Tutorials