class MediaTypeHelper {

    public static boolean isMatched(MediaType pattern, MediaType checkMe) {
        if (pattern == null || checkMe == null) {
            throw new IllegalArgumentException("Null arguments are not allowed");
        }

        if (pattern.isWildcardType()) {
            return true;
        }

        if (pattern.getType().equalsIgnoreCase(checkMe.getType())) {
            if (pattern.isWildcardSubtype() || pattern.getSubtype().equalsIgnoreCase(checkMe.getSubtype())) {
                return true;
            }

            Matcher patternMatcher = EXT_SUBTYPE_PATTERN.matcher(pattern.getSubtype());
            Matcher checkMeMatcher = EXT_SUBTYPE_PATTERN.matcher(checkMe.getSubtype());

            if (patternMatcher.matches()) {
                String patternPrefix = patternMatcher.group(1);
                String patternSuffix = patternMatcher.group(2);

                if (!checkMeMatcher.matches()) {
                    // pattern is type such as application/atom+xml, application/*+xml, application/xml+*
                    // checkMe is type such as application/xml
                    return checkMe.getSubtype().equalsIgnoreCase(patternPrefix)
                           || checkMe.getSubtype().equalsIgnoreCase(patternSuffix);
                }

                // both types are extended types
                String checkMePrefix = checkMeMatcher.group(1);
                String checkMeSuffix = checkMeMatcher.group(2);

                if (MEDIA_TYPE_WILDCARD.equals(patternSuffix) && patternPrefix.equalsIgnoreCase(checkMePrefix)) {
                    // parts before '+' are the same and pattern after '+' is wildcard '*'
                    // For example two sub-types: atom+* and atom+xml
                    return true;
                }

                if (MEDIA_TYPE_WILDCARD.equals(patternPrefix) && patternSuffix.equalsIgnoreCase(checkMeSuffix)) {
                    // parts after '+' are the same and pattern before '+' is wildcard '*'
                    // For example two sub-types: *+xml and atom+xml
                    return true;
                }
            }
        }

        return false;
    }

    private  MediaTypeHelper();

}