public final class MediaTypeSet extends AbstractSet<MediaType>
Set of MediaTypes which provides useful methods for content negotiation.
This Set provides match(MediaType) and matchHeaders(CharSequence...)
so that a user can find the preferred MediaType that matches the specified media ranges. For example:
MediaTypeSet set = new MediaTypeSet(MediaType.HTML_UTF_8, MediaType.PLAIN_TEXT_UTF_8);
Optional<MediaType> negotiated1 = set.matchHeaders("text/html; q=0.5, text/plain");
assert negotiated1.isPresent();
assert negotiated1.get().equals(MediaType.PLAIN_TEXT_UTF_8);
Optional<MediaType> negotiated2 = set.matchHeaders("audio/*, text/*");
assert negotiated2.isPresent();
assert negotiated2.get().equals(MediaType.HTML_UTF_8);
Optional<MediaType> negotiated3 = set.matchHeaders("video/webm");
assert !negotiated3.isPresent();
| Constructor and Description |
|---|
MediaTypeSet(Iterable<MediaType> mediaTypes)
Creates a new instance.
|
MediaTypeSet(MediaType... mediaTypes)
Creates a new instance.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
contains(Object o) |
Iterator<MediaType> |
iterator() |
Optional<MediaType> |
match(Iterable<MediaType> ranges)
|
Optional<MediaType> |
match(MediaType range)
|
Optional<MediaType> |
match(MediaType first,
MediaType... rest)
|
Optional<MediaType> |
matchHeaders(CharSequence... acceptHeaders)
|
Optional<MediaType> |
matchHeaders(Iterable<? extends CharSequence> acceptHeaders)
|
int |
size() |
equals, hashCode, removeAlladd, addAll, clear, containsAll, isEmpty, remove, retainAll, toArray, toArray, toStringclone, finalize, getClass, notify, notifyAll, wait, wait, waitadd, addAll, clear, containsAll, isEmpty, remove, retainAll, spliterator, toArray, toArrayparallelStream, removeIf, streampublic MediaTypeSet(MediaType... mediaTypes)
public boolean contains(Object o)
contains in interface Collection<MediaType>contains in interface Set<MediaType>contains in class AbstractCollection<MediaType>public int size()
size in interface Collection<MediaType>size in interface Set<MediaType>size in class AbstractCollection<MediaType>public Optional<MediaType> matchHeaders(Iterable<? extends CharSequence> acceptHeaders)
MediaType in this List that matches one of the media ranges specified in the
specified string.acceptHeaders - the values of the "accept" header, as defined in
the section 14.1, RFC2616MediaType that matches one of the specified media ranges.
Optional.empty() if there are no matches or acceptHeaders does not contain
any valid ranges.public Optional<MediaType> matchHeaders(CharSequence... acceptHeaders)
MediaType in this List that matches one of the media ranges specified in the
specified string.acceptHeaders - the values of the "accept" header, as defined in
the section 14.1, RFC2616MediaType that matches one of the specified media ranges.
Optional.empty() if there are no matches or acceptHeaders does not contain
any valid ranges.public Optional<MediaType> match(MediaType range)
MediaType that matches the specified media range.
Optional.empty() if there are no matchespublic Optional<MediaType> match(MediaType first, MediaType... rest)
MediaType that matches one of the specified media ranges.
Optional.empty() if there are no matches or the specified ranges do not contain
any valid ranges.Copyright © 2020 LeanCloud. All rights reserved.