001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.http; 014 015import static org.apache.juneau.internal.CollectionUtils.*; 016 017import java.util.*; 018 019import org.apache.juneau.internal.*; 020 021/** 022 * Category of headers that consist of simple comma-delimited lists of strings with q-values. 023 * 024 * <p> 025 * <h5 class='figure'>Example</h5> 026 * <p class='bcode'> 027 * Accept-Encoding: compress;q=0.5, gzip;q=1.0 028 * </p> 029 * 030 * <h5 class='section'>See Also:</h5> 031 * <ul class='doctree'> 032 * <li class='extlink'><a class='doclink' href='https://www.w3.org/Protocols/rfc2616/rfc2616.html'>Hypertext Transfer Protocol -- HTTP/1.1</a> 033 * </ul> 034 */ 035public class HeaderRangeArray { 036 037 final StringRange[] typeRanges; 038 private final List<StringRange> typeRangesList; 039 040 /** 041 * Constructor. 042 * 043 * @param value The raw header value. 044 */ 045 protected HeaderRangeArray(String value) { 046 this.typeRanges = StringRange.parse(value); 047 this.typeRangesList = immutableList(typeRanges); 048 } 049 050 /** 051 * Given a list of type values, returns the best match for this header. 052 * 053 * @param types The types to match against. 054 * @return The index into the array of the best match, or <code>-1</code> if no suitable matches could be found. 055 */ 056 public int findMatch(String[] types) { 057 058 // Type ranges are ordered by 'q'. 059 // So we only need to search until we've found a match. 060 for (StringRange mr : typeRanges) 061 for (int i = 0; i < types.length; i++) 062 if (mr.matches(types[i])) 063 return i; 064 065 return -1; 066 } 067 068 /** 069 * Returns the list of the types ranges that make up this header. 070 * 071 * <p> 072 * The types ranges in the list are sorted by their q-value in descending order. 073 * 074 * @return An unmodifiable list of type ranges. 075 */ 076 public List<StringRange> asSimpleRanges() { 077 return typeRangesList; 078 } 079 080 @Override /* Object */ 081 public String toString() { 082 return StringUtils.join(typeRanges, ','); 083 } 084}