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 w800'>
027 *    Accept-Encoding: compress;q=0.5, gzip;q=1.0
028 * </p>
029 *
030 * <ul class='seealso'>
031 *    <li class='extlink'>{@doc RFC2616}
032 * </ul>
033 */
034public class HeaderRangeArray {
035
036   final StringRange[] typeRanges;
037   private final List<StringRange> typeRangesList;
038
039   /**
040    * Constructor.
041    *
042    * @param value The raw header value.
043    */
044   protected HeaderRangeArray(String value) {
045      this.typeRanges = StringRange.parse(value);
046      this.typeRangesList = immutableList(typeRanges);
047   }
048
049   /**
050    * Given a list of type values, returns the best match for this header.
051    *
052    * @param types The types to match against.
053    * @return The index into the array of the best match, or <c>-1</c> if no suitable matches could be found.
054    */
055   public int findMatch(String[] types) {
056
057      // Type ranges are ordered by 'q'.
058      // So we only need to search until we've found a match.
059      for (StringRange mr : typeRanges)
060         for (int i = 0; i < types.length; i++)
061            if (mr.matches(types[i]))
062               return i;
063
064      return -1;
065   }
066
067   /**
068    * Returns the list of the types ranges that make up this header.
069    *
070    * <p>
071    * The types ranges in the list are sorted by their q-value in descending order.
072    *
073    * @return An unmodifiable list of type ranges.
074    */
075   public List<StringRange> asSimpleRanges() {
076      return typeRangesList;
077   }
078
079   @Override /* Object */
080   public String toString() {
081      return StringUtils.join(typeRanges, ',');
082   }
083}