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.header;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.ArrayUtils.copyOf;
017import static org.apache.juneau.internal.CollectionUtils.*;
018
019import java.util.*;
020import org.apache.juneau.annotation.*;
021import org.apache.juneau.internal.*;
022
023/**
024 * A list of {@link EntityTag} beans.
025 */
026@BeanIgnore
027public class EntityTags {
028   //-----------------------------------------------------------------------------------------------------------------
029   // Static
030   //-----------------------------------------------------------------------------------------------------------------
031
032   /** Represents an empty entity tags object. */
033   public static final EntityTags EMPTY = new EntityTags("");
034
035   private static final Cache<String,EntityTags> CACHE = Cache.of(String.class, EntityTags.class).build();
036
037   /**
038    * Returns a parsed entity tags header value.
039    *
040    * @param value The raw header value.
041    * @return A parsed header value.
042    */
043   public static EntityTags of(String value) {
044      return isEmpty(value) ? EMPTY : CACHE.get(value, ()->new EntityTags(value));
045   }
046
047   /**
048    * Returns a parsed entity tags header value.
049    *
050    * @param value The header value.
051    * @return A parsed header value.
052    */
053   public static EntityTags of(EntityTag...value) {
054      return value == null ? null : new EntityTags(value);
055   }
056
057   //-----------------------------------------------------------------------------------------------------------------
058   // Instance
059   //-----------------------------------------------------------------------------------------------------------------
060
061   private final EntityTag[] value;
062   private final String string;
063
064   /**
065    * Constructor.
066    *
067    * @param value The header value.
068    */
069   public EntityTags(String value) {
070      this.string = value;
071      this.value = parse(value);
072   }
073
074   /**
075    * Constructor.
076    *
077    * @param value The header value.
078    */
079   public EntityTags(EntityTag...value) {
080      this.string = join(value, ", ");
081      this.value = copyOf(value);
082   }
083
084   private EntityTag[] parse(String value) {
085      if (value == null)
086         return null;
087      String[] s = split(value);
088      EntityTag[] v = new EntityTag[s.length];
089      for (int i = 0; i < s.length; i++)
090         v[i] = EntityTag.of(s[i]);
091      return v;
092   }
093
094   /**
095    * Returns the entity tags in this object as a list.
096    *
097    * <p>
098    * Returns an unmodifiable list.
099    *
100    * @return The entity tags in this object as a list.  Can be <jk>null</jk>.
101    */
102   public List<EntityTag> toList() {
103      return ulist(value);
104   }
105
106   /**
107    * Returns the entity tags in this object as an array.
108    *
109    * <p>
110    * Returns a copy of the entity tags.
111    *
112    * @return The entity tags in this object as an array.  Can be <jk>null</jk>.
113    */
114   public EntityTag[] toArray() {
115      return copyOf(value);
116   }
117
118   @Override /* Object */
119   public String toString() {
120      return string;
121   }
122}