mirror of
https://github.com/jetty/jetty.project.git
synced 2025-02-28 19:09:10 +00:00
Issue #3404
updates after review: + use ToIntFunction + reformat Signed-off-by: Greg Wilkins <gregw@webtide.com>
This commit is contained in:
parent
73e68ac7b5
commit
69f6b3b616
@ -31,7 +31,7 @@ import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.ToIntFunction;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.StreamSupport;
|
||||
|
||||
@ -450,7 +450,7 @@ public class HttpFields implements Iterable<HttpField>
|
||||
* @param secondaryOrdering Function to apply an ordering other than specified by quality
|
||||
* @return List the values in quality order with the q param and OWS stripped
|
||||
*/
|
||||
public List<String> getQualityCSV(HttpHeader header, Function<String, Integer> secondaryOrdering)
|
||||
public List<String> getQualityCSV(HttpHeader header, ToIntFunction<String> secondaryOrdering)
|
||||
{
|
||||
QuotedQualityCSV values = null;
|
||||
for (HttpField f : this)
|
||||
|
@ -18,21 +18,23 @@
|
||||
|
||||
package org.eclipse.jetty.http;
|
||||
|
||||
import static java.lang.Integer.MIN_VALUE;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.ToIntFunction;
|
||||
|
||||
import org.eclipse.jetty.util.log.Log;
|
||||
|
||||
import static java.lang.Integer.MIN_VALUE;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Implements a quoted comma separated list of quality values
|
||||
* in accordance with RFC7230 and RFC7231.
|
||||
* Values are returned sorted in quality order, with OWS and the
|
||||
* Values are returned sorted in quality order, with OWS and the
|
||||
* quality parameters removed.
|
||||
*
|
||||
* @see "https://tools.ietf.org/html/rfc7230#section-3.2.6"
|
||||
* @see "https://tools.ietf.org/html/rfc7230#section-7"
|
||||
* @see "https://tools.ietf.org/html/rfc7231#section-5.3.1"
|
||||
@ -41,44 +43,48 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable<String>
|
||||
{
|
||||
/**
|
||||
* Lambda to apply a most specific MIME encoding secondary ordering.
|
||||
*
|
||||
* @see "https://tools.ietf.org/html/rfc7231#section-5.3.2"
|
||||
*/
|
||||
public static Function<String, Integer> MOST_SPECIFIC_MIME_ORDERING = s ->
|
||||
public static ToIntFunction<String> MOST_SPECIFIC_MIME_ORDERING = s ->
|
||||
{
|
||||
if ("*/*".equals(s))
|
||||
return 0;
|
||||
if (s.endsWith("/*"))
|
||||
return 1;
|
||||
if (s.indexOf(';')<0)
|
||||
if (s.indexOf(';') < 0)
|
||||
return 2;
|
||||
return 3;
|
||||
};
|
||||
|
||||
|
||||
private final List<Double> _quality = new ArrayList<>();
|
||||
private boolean _sorted = false;
|
||||
private final Function<String, Integer> _secondaryOrdering;
|
||||
|
||||
private final ToIntFunction<String> _secondaryOrdering;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Sorts values with equal quality according to the length of the value String.
|
||||
*/
|
||||
public QuotedQualityCSV()
|
||||
{
|
||||
this((Function)null);
|
||||
this((ToIntFunction)null);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Sorts values with equal quality according to given order.
|
||||
*
|
||||
* @param preferredOrder Array indicating the preferred order of known values
|
||||
*/
|
||||
public QuotedQualityCSV(String[] preferredOrder)
|
||||
{
|
||||
this((s) ->
|
||||
{
|
||||
for (int i=0;i<preferredOrder.length;++i)
|
||||
for (int i = 0; i < preferredOrder.length; ++i)
|
||||
if (preferredOrder[i].equals(s))
|
||||
return preferredOrder.length-i;
|
||||
return preferredOrder.length - i;
|
||||
|
||||
if ("*".equals(s))
|
||||
return preferredOrder.length;
|
||||
@ -88,15 +94,17 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable<String>
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
/**
|
||||
* Orders values with equal quality with the given function.
|
||||
*
|
||||
* @param secondaryOrdering Function to apply an ordering other than specified by quality
|
||||
*/
|
||||
public QuotedQualityCSV(Function<String, Integer> secondaryOrdering)
|
||||
public QuotedQualityCSV(ToIntFunction<String> secondaryOrdering)
|
||||
{
|
||||
this._secondaryOrdering = secondaryOrdering == null ? s->0 : secondaryOrdering;
|
||||
this._secondaryOrdering = secondaryOrdering == null ? s -> 0 : secondaryOrdering;
|
||||
}
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
@Override
|
||||
protected void parsedValue(StringBuffer buffer)
|
||||
@ -134,7 +142,7 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable<String>
|
||||
}
|
||||
buffer.setLength(Math.max(0, paramName - 1));
|
||||
|
||||
if (q!=1.0D)
|
||||
if (q != 1.0D)
|
||||
// replace assumed quality
|
||||
_quality.set(_quality.size() - 1, q);
|
||||
}
|
||||
@ -147,7 +155,7 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable<String>
|
||||
sort();
|
||||
return _values;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<String> iterator()
|
||||
{
|
||||
@ -158,35 +166,35 @@ public class QuotedQualityCSV extends QuotedCSV implements Iterable<String>
|
||||
|
||||
protected void sort()
|
||||
{
|
||||
_sorted=true;
|
||||
_sorted = true;
|
||||
|
||||
Double last = 0.0D;
|
||||
int lastSecondaryOrder = Integer.MIN_VALUE;
|
||||
|
||||
for (int i = _values.size(); i-- > 0;)
|
||||
for (int i = _values.size(); i-- > 0; )
|
||||
{
|
||||
String v = _values.get(i);
|
||||
Double q = _quality.get(i);
|
||||
|
||||
int compare=last.compareTo(q);
|
||||
if (compare>0 || (compare==0 && _secondaryOrdering.apply(v)<lastSecondaryOrder))
|
||||
int compare = last.compareTo(q);
|
||||
if (compare > 0 || (compare == 0 && _secondaryOrdering.applyAsInt(v) < lastSecondaryOrder))
|
||||
{
|
||||
_values.set(i, _values.get(i + 1));
|
||||
_values.set(i + 1, v);
|
||||
_quality.set(i, _quality.get(i + 1));
|
||||
_quality.set(i + 1, q);
|
||||
last = 0.0D;
|
||||
lastSecondaryOrder=0;
|
||||
lastSecondaryOrder = 0;
|
||||
i = _values.size();
|
||||
continue;
|
||||
}
|
||||
|
||||
last=q;
|
||||
lastSecondaryOrder=_secondaryOrdering.apply(v);
|
||||
last = q;
|
||||
lastSecondaryOrder = _secondaryOrdering.applyAsInt(v);
|
||||
}
|
||||
|
||||
int last_element=_quality.size();
|
||||
while(last_element>0 && _quality.get(--last_element).equals(0.0D))
|
||||
|
||||
int last_element = _quality.size();
|
||||
while (last_element > 0 && _quality.get(--last_element).equals(0.0D))
|
||||
{
|
||||
_quality.remove(last_element);
|
||||
_values.remove(last_element);
|
||||
|
Loading…
x
Reference in New Issue
Block a user