ContentType { public static ContentType valueOf(String contentType) { final Matcher matcher = PATTERN.matcher(contentType); if (matcher.matches()) { return new ContentType(contentType, matcher.group(1), matcher.group(2), matcher.group(3)); } else { throw new IllegalArgumentException("Invalid content type [" + contentType + "]"); } } private  ContentType(String value, String type, String subtype, String parameters); static ContentType valueOf(String contentType); String getValue(); String getType(); String getSubtype(); @Override boolean equals(Object o); @Override int hashCode(); static final String TEXT_PLAIN_VALUE; static final String APPLICATION_JSON_VALUE; static final String APPLICATION_JSON_UTF8_VALUE; static final String APPLICATION_PROBLEM_JSON_VALUE; static final ContentType TEXT_PLAIN; static final ContentType APPLICATION_JSON; static final ContentType APPLICATION_JSON_UTF8; static final ContentType APPLICATION_PROBLEM_JSON; }
@Test(expected = IllegalArgumentException.class) public void shouldFailOnIllegalContentType() { ContentType.valueOf("foo bar"); }
@Test public void shouldBeHasheable() { final HashSet<ContentType> contentTypes = new HashSet<>(); contentTypes.add(ContentType.APPLICATION_JSON); contentTypes.add(ContentType.TEXT_PLAIN); contentTypes.add(ContentType.valueOf("application/octet-stream")); assertEquals(3, contentTypes.size()); assertTrue(contentTypes.contains(ContentType.valueOf("application/json"))); assertTrue(contentTypes.contains(ContentType.valueOf("text/plain"))); assertTrue(contentTypes.contains(ContentType.valueOf("application/octet-stream"))); }