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
015/**
016 * Represents a validator value.
017 *
018 * <p>
019 * <h5 class='figure'>Example</h5>
020 * <p class='bcode w800'>
021 *    ETag: "123456789"    – A strong ETag validator
022 *    ETag: W/"123456789"  – A weak ETag validator
023 * </p>
024 *
025 * <h5 class='section'>See Also:</h5>
026 * <ul class='doctree'>
027 *    <li class='extlink'>{@doc RFC2616}
028 * </ul>
029 */
030public class EntityValidator {
031
032   private final String value;
033   private final boolean isWeak;
034
035   /**
036    * Constructor.
037    *
038    * @param value The validator string value.
039    */
040   protected EntityValidator(String value) {
041      value = value.trim();
042      isWeak = value.startsWith("W/");
043      if (isWeak)
044         value = value.substring(2);
045      if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length()-1) == '"')
046         value = value.substring(1, value.length()-1);
047      this.value = value;
048   }
049
050   /**
051    * Returns the validator value stripped of quotes and weak tag.
052    *
053    * @return The validator value.
054    */
055   public String asString() {
056      return value;
057   }
058
059   /**
060    * Returns <jk>true</jk> if the weak flag is present in the value.
061    *
062    * @return <jk>true</jk> if the weak flag is present in the value.
063    */
064   public boolean isWeak() {
065      return isWeak;
066   }
067
068   /**
069    * Returns <jk>true</jk> if the validator string value is <code>*</code>.
070    *
071    * @return <jk>true</jk> if the validator string value is <code>*</code>.
072    */
073   public boolean isAny() {
074      return "*".equals(value);
075   }
076
077   @Override
078   public String toString() {
079      return (isWeak ? "W/" : "") + (isAny() ? "*" : ('"' + value + '"'));
080   }
081}