Package com.linecorp.armeria.common
Class MediaTypeSet
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<MediaType>
-
- com.linecorp.armeria.common.MediaTypeSet
-
public final class MediaTypeSet extends AbstractSet<MediaType>
An immutableSet
ofMediaType
s which provides useful methods for content negotiation.This
Set
providesmatch(MediaType)
andmatchHeaders(CharSequence...)
so that a user can find the preferredMediaType
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 Summary
Constructors Constructor Description MediaTypeSet(MediaType... mediaTypes)
Creates a new instance.MediaTypeSet(Iterable<MediaType> mediaTypes)
Creates a new instance.
-
Method Summary
Modifier and Type Method Description boolean
contains(Object o)
Iterator<MediaType>
iterator()
Optional<MediaType>
match(MediaType range)
Optional<MediaType>
match(MediaType first, MediaType... rest)
Optional<MediaType>
match(Iterable<MediaType> ranges)
Optional<MediaType>
matchHeaders(CharSequence... acceptHeaders)
Optional<MediaType>
matchHeaders(Iterable<? extends CharSequence> acceptHeaders)
int
size()
-
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
-
Methods inherited from class java.util.AbstractCollection
add, addAll, clear, containsAll, isEmpty, remove, retainAll, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
-
-
-
Method Detail
-
contains
public boolean contains(Object o)
- Specified by:
contains
in interfaceCollection<MediaType>
- Specified by:
contains
in interfaceSet<MediaType>
- Overrides:
contains
in classAbstractCollection<MediaType>
-
size
public int size()
- Specified by:
size
in interfaceCollection<MediaType>
- Specified by:
size
in interfaceSet<MediaType>
- Specified by:
size
in classAbstractCollection<MediaType>
-
matchHeaders
public Optional<MediaType> matchHeaders(Iterable<? extends CharSequence> acceptHeaders)
Finds theMediaType
in thisList
that matches one of the media ranges specified in the specified string.- Parameters:
acceptHeaders
- the values of the"accept"
header, as defined in the section 14.1, RFC2616- Returns:
- the most preferred
MediaType
that matches one of the specified media ranges.Optional.empty()
if there are no matches oracceptHeaders
does not contain any valid ranges.
-
matchHeaders
public Optional<MediaType> matchHeaders(CharSequence... acceptHeaders)
Finds theMediaType
in thisList
that matches one of the media ranges specified in the specified string.- Parameters:
acceptHeaders
- the values of the"accept"
header, as defined in the section 14.1, RFC2616- Returns:
- the most preferred
MediaType
that matches one of the specified media ranges.Optional.empty()
if there are no matches oracceptHeaders
does not contain any valid ranges.
-
match
public Optional<MediaType> match(MediaType range)
- Returns:
- the
MediaType
that matches the specified media range.Optional.empty()
if there are no matches
-
match
public Optional<MediaType> match(MediaType first, MediaType... rest)
- Returns:
- the most preferred
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.
-
-