class MMTypeTools {

    public static String bestMatch(Collection<String> supported, String header) {
        List<ParseResults> parseResults = new LinkedList<ParseResults>();

        List<FitnessAndQuality> weightedMatches = new LinkedList<FitnessAndQuality>();
        for (String r : header.split(",")) {
            parseResults.add(parseMediaRange(r));
        }

        for (String s : supported) {
            FitnessAndQuality fitnessAndQuality = fitnessAndQualityParsed(s,
                    parseResults);
            fitnessAndQuality.mimeType = s;
            weightedMatches.add(fitnessAndQuality);
        }
        Collections.sort(weightedMatches);

        FitnessAndQuality lastOne = weightedMatches.get(weightedMatches.size() - 1);
//        debug("Get back: " + lastOne.quality + " , " + lastOne.mimeType);

        if (lastOne.quality != 0) {
            return lastOne.mimeType;
        }

        return "";
    }

}

class MMTypeToolsTest {

    @Test
    public void testBestMatch() {

        System.out.println("bestMatch");
        
        Collection<String> acceps = new ArrayList<String>();
        acceps.add("text/html");
        acceps.add("text/*;q=0.9");
        acceps.add("image/jpeg;q=0.9");
        acceps.add("image/png;q=0.9");
        acceps.add("image/*;q=0.9");
        acceps.add("*/*;q=0.8");
       
        String fileContentType = "text/plain";
        String expResult = "text/*;q=0.9";
        String result = MMTypeTools.bestMatch(acceps, fileContentType);
        assertEquals(expResult, result);
    }

}