class MediaTypeUtil {

    public static List<String> getSortedAcceptableMediaTypes(List<String> accept) {
        List<Item> result = new ArrayList<>(accept.size() + 1);
        for (String acceptItem : accept) {
            String[] subItems = acceptItem.split(",");

            for (String subItem : subItems) {
                String[] itemPlusParams = subItem.split(";");
                String item = itemPlusParams[0].trim();
                if (item.isEmpty()) continue;

                float weight = 1.0f;

                // use the 'q' parameter if possible to set the weight
                for (int i = 1; i < itemPlusParams.length; i++) {
                    String[] param = itemPlusParams[i].split("=", 2);
                    String paramName = param[0].trim();
                    if (param.length == 2 && paramName.equalsIgnoreCase("q")) {
                        try {
                            weight = Float.parseFloat(param[1].trim());
                        } catch (NumberFormatException e) {
                            // ignore
                        }
                        break; // 'q' param found, break out
                    }
                }
                result.add(new Item(item, weight));
            }
        }
        return result.stream().sorted().map(i -> i.value).collect(Collectors.toList());
    }

}