Java String Normalize normalizeMatchup(final String matchup)

Here you can find the source of normalizeMatchup(final String matchup)

Description

Returns a normalized form of the specified match-up string (which can be either a race match-up or a league match-up).

License

Apache License

Parameter

Parameter Description
matchup match-up to be normalized

Return

a normalized form of the specified match-up string

Declaration

public static String normalizeMatchup(final String matchup) 

Method Source Code

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

import java.util.Arrays;

public class Main {
    /**//from   w  w w . j  av a  2s .c  om
     * Returns a normalized form of the specified match-up string (which can be either a race match-up or a league match-up).
     * 
     * <p>
     * The gain of this normalization is that 2 different match-ups which are the permutations of each other have the same normalized form therefore match-up
     * permutations can be tested if they are equal by a simple equality filter (where we compare the normalized forms of the match-up filter and the match-up
     * of the entities).
     * </p>
     * 
     * <p>
     * Normalization consists of the following steps:
     * <ol>
     * <li>make match-up lower-cased (e.g. <code>"ZZPvTPZ"</code> => <code>"zzpvtpz"</code>)
     * <li>sort letters inside teams (e.g. <code>"zzpvtpz"</code> => <code>"pzzvptz"</code>)
     * <li>sort teams (e.g. <code>"pzzvptz"</code> => <code>"ptzvpzz"</code>)
     * </ol>
     * </p>
     * 
     * @param matchup match-up to be normalized
     * @return a normalized form of the specified match-up string
     */
    public static String normalizeMatchup(final String matchup) {
        if (matchup == null)
            return null;

        // Step 1: lower-case
        final String[] teams = matchup.toLowerCase().split("v");

        // Step 2: sort letters inside teams
        for (int i = 0; i < teams.length; i++) {
            final char[] letters = teams[i].toCharArray();
            Arrays.sort(letters);
            teams[i] = new String(letters);
        }

        // Step 3: sort teams
        Arrays.sort(teams);

        // Concatenate teams and return the normalized form:
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < teams.length; i++) {
            if (i > 0)
                sb.append('v');
            sb.append(teams[i]);
        }

        return sb.toString();
    }
}

Related

  1. normalizeCutter(String cutter, int numDigits)
  2. normalizeDose(String dose)
  3. normalizeEnglishIdentifier(String id)
  4. normalizeFieldNameOrPath(final String nameOrPath)
  5. normalizeIndex(String input, String[] indexList)
  6. normalizePackageNamePart(String name)
  7. normalizeRepositoryName(String input)
  8. normalizeSearchString(String src)
  9. normalizeString(String input)