@Override public String toString() { return type.getName(); } }
@Override public int hashCode() { return subType.hashCode(); }
@Override public int hashCode() { int result = getType().hashCode(); result = 67867979 * result + getSubType().hashCode(); result = 67867979 * result + getParameters().hashCode(); return result; }
@Override public String toString() { if (toStringCache == null) { toStringCache = subType.getFacet().getRegistrationTreeName().map(tree -> tree + ".").orElse("") + subType.getName() + subType.getSuffix().map(suffix -> "+" + suffix).orElse(""); } return toStringCache; } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; final MimeType mimeType = (MimeType) o; if (!getType().equals(mimeType.getType())) return false; //noinspection SimplifiableIfStatement if (!getSubType().equals(mimeType.getSubType())) return false; return getParameters().equals(mimeType.getParameters()); }
@Test public void test_mimetype_parser_does_parse_valid_mimetype_in_the_standard_facet_without_a_suffix () throws ParserFailure { Optional<MimeType> oResult; MimeType mime; oResult = MimeTypeParser.parse("application/json"); assertThat(oResult.isPresent()).isTrue(); assertThat(oResult.get()).isNotNull(); mime = oResult.get(); assertThat(mime.getType()).isEqualTo(MimeType.Type.APPLICATION); assertThat(mime.getSubType().getName()).isEqualTo("json"); }
@Test public void test_mimetype_compares_two_indentical_instances_correctly () { final MimeType one = MimeType.of(MimeType.Type.APPLICATION, MimeType.SubType.of("json")); final MimeType two = MimeType.of(MimeType.Type.APPLICATION, MimeType.SubType.of("json")); assertThat(one).isEqualTo(two); assertThat(one.hashCode()).isEqualTo(two.hashCode()); }
@Test public void test_mimetype_to_string_works () { final Map<String,String> parameters = new HashMap<>(); parameters.put("key","value"); final MimeType complex = MimeType.of( MimeType.Type.EXAMPLE, MimeType.SubType.of(MimeType.Facet.PERSONAL, "niketest", Optional.of("json")), parameters ); assertThat(complex.toString()).isEqualTo("example/prs.niketest+json;key=value"); }
private MediaRangeSubType mrSubType(String typeString) { if (MediaRange.WILDCARD_SUBTYPE.toString().equals(typeString)) return MediaRange.WILDCARD_SUBTYPE; MimeType.Facet facet = MimeType.Facet.STANDARD; if (typeString.startsWith(MimeType.Facet.VENDOR.getRegistrationTreeName().get())) facet = MimeType.Facet.VENDOR; Optional<String> suffix = Optional.empty(); if (typeString.endsWith("+json")) suffix = Optional.of("json"); return new MimeMediaRangeSubType(MimeType.SubType.of(facet, typeString, suffix)); }
@Override public int hashCode() { return type.hashCode(); }
/** * Creates a MimeType instance with the given type and sub-type values. * * @param type * the type to assign to the mimetype * @param subType * the subtype to assign to the mimetype * * @return a MimeType instance with the given type, sub-type, and an empty parameters map. */ public static MimeType of(final Type type, final SubType subType) { return of(type, subType, new HashMap<>()); }
/** * Creates a SubType assigned with the given name, suffix, and facet. * Note, per RFC-6838 4.2, top-level type and subtype names are case-insensitive. * * @param facet * the facet to assign the subtype * @param name * the name to assign the subtype * @param suffix * the suffix to assign the subtype * * @return a SubType assigned with the given name, suffix, and facet */ public static SubType of(final Facet facet, final String name, final Optional<String> suffix) { return new SubType(facet, name, suffix); }
/** * Creates a MimeType instance with the given type, sub-type, and parameters values. * * @param type * the type to assign to the mimetype * @param subType * the subtype to assign to the mimetype * @param parameters * the parameters to assign to the mimetype * * @return a MimeType instance with the given type, sub-type, and an empty parameters map. */ public static MimeType of(final Type type, final SubType subType, final Map<String, String> parameters) { return new MimeType(type, subType, parameters); }
/** * Attempts to lookup a defined top-level type using the given type name. * Note, per RFC-6838 4.2, top-level type and subtype names are case-insensitive. * * @param typeName * the name to lookup a Type instance by. * * @return The top-level Type instance identified by the given type name, if one is not found, then a new Type * instance is created with the given name. */ public static Type of(final String typeName) { // return a top-level type instance if givenType's name matches, else create a new type final Type type = lowercaseNameToTypeValuesMap.get(typeName.toLowerCase()); return type != null ? type : new Type(typeName); // allow non-registered top-level types. }
@Override public String toString() { if (toStringCache == null) { toStringCache = type.getName() + "/" + subType.getFacet().getRegistrationTreeName().map(tree -> tree + ".").orElse("") + subType.getName() + subType.getSuffix().map(suffix -> "+" + suffix).orElse("") + parameters.entrySet().stream().map(entry -> ";" + entry.getKey() + "=" + entry.getValue()) .collect(Collectors.joining()); } return toStringCache; } }
@Test public void test_mimetype_compares_two_different_instances_correctly () { final MimeType one = MimeType.of(MimeType.Type.APPLICATION, MimeType.SubType.of("json")); final MimeType two = MimeType.of(MimeType.Type.APPLICATION, MimeType.SubType.of(MimeType.Facet.STANDARD, "schema", Optional.of("json"))); assertThat(one).isNotEqualTo(two); assertThat(one.hashCode()).isNotEqualTo(two.hashCode()); }
@Override public int hashCode() { return getName().hashCode(); } }
/** * Creates a SubType with a Name assigned to the given name, an empty suffix, and assigned to the STANDARD * facet. * * @param name * the name of this subtype * * @return a SubType with a Name assigned to the given name, an empty suffix, and assigned to the STANDARD * facet. */ public static SubType of(final String name) { return new SubType(Facet.STANDARD, name, Optional.empty()); }