1 module hunt.http.codec.http.model.QuotedQualityCSV;
2 
3 // import java.util.ArrayList;
4 // import java.util.Iterator;
5 import hunt.collection.List;
6 // import java.util.function.Function;
7 
8 
9 /**
10  * : a quoted comma separated list of quality values in accordance with
11  * RFC7230 and RFC7231. Values are returned sorted in quality order, with OWS
12  * and the quality parameters removed.
13  *
14  * @see "https://tools.ietf.org/html/rfc7230#section-3.2.6"
15  * @see "https://tools.ietf.org/html/rfc7230#section-7"
16  * @see "https://tools.ietf.org/html/rfc7231#section-5.3.1"
17  */
18 // class QuotedQualityCSV :QuotedCSV : Iterable<string> {
19 //     private static Double ZERO = 0.0;
20 //     private static Double ONE = 1.0;
21 
22 //     /**
23 //      * Function to apply a most specific MIME encoding secondary ordering
24 //      */
25 //     static Function<string, Integer> MOST_SPECIFIC = s -> {
26 //         string[] elements = s.split("/");
27 //         return 1000000 * elements.length + 1000 * elements[0].length() + elements[elements.length - 1].length();
28 //     };
29 
30 //     private List<Double> _quality = new ArrayList<>();
31 //     private bool _sorted = false;
32 //     private Function<string, Integer> _secondaryOrdering;
33 
34 
35 //     /**
36 //      * Sorts values with equal quality according to the length of the value string.
37 //      */
38 //     this() {
39 //         this((s) -> 0);
40 //     }
41 
42 //     /**
43 //      * Sorts values with equal quality according to given order.
44 //      *
45 //      * @param preferredOrder Array indicating the preferred order of known values
46 //      */
47 //     this(string[] preferredOrder) {
48 //         this((s) -> {
49 //             for (int i = 0; i < preferredOrder.length; ++i)
50 //                 if (preferredOrder[i].equals(s))
51 //                     return preferredOrder.length - i;
52 
53 //             if ("*".equals(s))
54 //                 return preferredOrder.length;
55 
56 //             return MIN_VALUE;
57 //         });
58 //     }
59 
60 //     /**
61 //      * Orders values with equal quality with the given function.
62 //      *
63 //      * @param secondaryOrdering Function to apply an ordering other than specified by quality
64 //      */
65 //     this(Function<string, Integer> secondaryOrdering) {
66 //         this._secondaryOrdering = secondaryOrdering;
67 //     }
68 
69 //     override
70 //     protected void parsedValue(StringBuffer buffer) {
71 //         super.parsedValue(buffer);
72 //         _quality.add(ONE);
73 //     }
74 
75 //     override
76 //     protected void parsedParam(StringBuffer buffer, int valueLength, int paramName, int paramValue) {
77 //         if (paramName < 0) {
78 //             if (buffer.charAt(buffer.length() - 1) == ';') {
79 //                 buffer.setLength(buffer.length() - 1);
80 //             }
81 //         } else if (paramValue >= 0 &&
82 //                 buffer.charAt(paramName) == 'q' && paramValue > paramName &&
83 //                 buffer.length() >= paramName && buffer.charAt(paramName + 1) == '=') {
84 //             Double q;
85 //             try {
86 //                 q = (_keepQuotes && buffer.charAt(paramValue) == '"')
87 //                         ? new Double(buffer.substring(paramValue + 1, buffer.length() - 1))
88 //                         : new Double(buffer.substring(paramValue));
89 //             } catch (Exception e) {
90 //                 q = ZERO;
91 //             }
92 //             buffer.setLength(std.algorithm.max(0, paramName - 1));
93 
94 //             if (!ONE.equals(q)) {
95 //                 _quality.set(_quality.size() - 1, q);
96 //             }
97 //         }
98 //     }
99 
100 //     List<string> getValues() {
101 //         if (!_sorted) {
102 //             sort();
103 //         }
104 //         return _values;
105 //     }
106 
107 //     override
108 //     Iterator<string> iterator() {
109 //         if (!_sorted) {
110 //             sort();
111 //         }
112 //         return _values.iterator();
113 //     }
114 
115 //     protected void sort() {
116 //         _sorted = true;
117 
118 //         Double last = ZERO;
119 //         int lastSecondaryOrder = Integer.MIN_VALUE;
120 
121 //         for (int i = _values.size(); i-- > 0; ) {
122 //             string v = _values.get(i);
123 //             Double q = _quality.get(i);
124 
125 //             int compare = last.compareTo(q);
126 //             if (compare > 0 || (compare == 0 && _secondaryOrdering.apply(v) < lastSecondaryOrder)) {
127 //                 _values.set(i, _values.get(i + 1));
128 //                 _values.set(i + 1, v);
129 //                 _quality.set(i, _quality.get(i + 1));
130 //                 _quality.set(i + 1, q);
131 //                 last = ZERO;
132 //                 lastSecondaryOrder = 0;
133 //                 i = _values.size();
134 //                 continue;
135 //             }
136 
137 //             last = q;
138 //             lastSecondaryOrder = _secondaryOrdering.apply(v);
139 //         }
140 
141 //         int last_element = _quality.size();
142 //         while (last_element > 0 && _quality.get(--last_element).equals(ZERO)) {
143 //             _quality.remove(last_element);
144 //             _values.remove(last_element);
145 //         }
146 //     }
147 // }