class AcceptLanguageHeaderDelegate implements RuntimeDelegate.HeaderDelegate<AcceptLanguage> {

    @Override
    public AcceptLanguage fromString(String header) {
        if (header == null) {
            throw new IllegalArgumentException();
        }

        try {
            header = HeaderHelper.removeWhitespaces(header);
            String tag;
            Map<String, String> params = null;

            int p = scan(header, QUALITY_SEPARATOR);
            if (charAtIs(header, p, QUALITY_SEPARATOR)) {
                tag = header.substring(0, p);
                params = new HeaderParameterParser().parse(header);
            } else {
                tag = header;
            }

            String lang;
            String country = null;

            p = scan(header, SUB_TAG_SEPARATOR);
            if (charAtIs(header, p, SUB_TAG_SEPARATOR)) {
                lang = tag.substring(0, p);
                country = tag.substring(p + 1);
            } else {
                lang = tag;
            }

            if (params == null || params.get(QVALUE) == null) {
                return new AcceptLanguage(new Language(new Locale(lang, nullToEmpty(country))));
            }

            return new AcceptLanguage(new Locale(lang, nullToEmpty(country)), parseQualityValue(params.get(QVALUE)));
        } catch (ParseException e) {
            throw new IllegalArgumentException("Accept language header malformed");
        }
    }

}