001    /*
002     * The contents of this file are subject to the terms
003     * of the Common Development and Distribution License
004     * (the "License").  You may not use this file except
005     * in compliance with the License.
006     * 
007     * You can obtain a copy of the license at
008     * http://www.opensource.org/licenses/cddl1.php
009     * See the License for the specific language governing
010     * permissions and limitations under the License.
011     */
012    
013    /*
014     * EntityTag.java
015     *
016     * Created on March 21, 2007, 3:14 PM
017     *
018     */
019    
020    package javax.ws.rs.core;
021    
022    import javax.ws.rs.ext.RuntimeDelegate;
023    import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
024    
025    /**
026     * An abstraction for the value of a HTTP Entity Tag, used as the value 
027     * of an ETag response header.
028     * @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.11">HTTP/1.1 section 3.11</a>
029     */
030    public class EntityTag {
031        
032        private String value;
033        private boolean weak;
034        
035        private static final HeaderDelegate<EntityTag> delegate = 
036                RuntimeDelegate.getInstance().createHeaderDelegate(EntityTag.class);
037    
038        /**
039         * Creates a new instance of a strong EntityTag. 
040         * @param value the value of the tag, quotes not included. 
041         * @throws IllegalArgumentException if value is null
042         */
043        public EntityTag(String value) {
044            this(value, false);
045        }
046        
047        /**
048         * Creates a new instance of an EntityTag
049         * @param value the value of the tag, quotes not included.
050         * @param weak true if this represents a weak tag, false otherwise
051         * @throws IllegalArgumentException if value is null
052         */
053        public EntityTag(String value, boolean weak) {
054            if (value == null)
055                throw new IllegalArgumentException("value==null");
056            this.value = value;
057            this.weak = weak;
058        }
059        
060        /**
061         * Creates a new instance of EntityTag by parsing the supplied string.
062         * @param value the entity tag string
063         * @return the newly created EntityTag
064         * @throws IllegalArgumentException if the supplied string cannot be parsed
065         * or is null
066         */
067        public static EntityTag valueOf(String value) throws IllegalArgumentException {
068            return delegate.fromString(value);
069        }
070        
071        /**
072         * Check the strength of an EntityTag
073         * @return true if this represents a weak tag, false otherwise
074         */
075        public boolean isWeak() {
076            return weak;
077        }
078        
079        /**
080         * Get the value of an EntityTag
081         * @return the value of the tag
082         */
083        public String getValue() {
084            return value;
085        }
086        
087        /**
088         * Compares obj to this tag to see if they are the same considering weakness and
089         * value.
090         * @param obj the object to compare to
091         * @return true if the two tags are the same, false otherwise.
092         */
093        @Override
094        public boolean equals(Object obj) {
095            if (obj == null)
096                return false;
097            if (!(obj instanceof EntityTag))
098                return super.equals(obj);
099            EntityTag other = (EntityTag)obj;
100            if (value.equals(other.getValue()) && weak==other.isWeak())
101                return true;
102            return false;
103        }
104    
105        /**
106         * Generate hashCode based on value and weakness.
107         * @return the hashCode
108         */
109        @Override
110        public int hashCode() {
111            int hash = 3;
112            hash = 17 * hash + (this.value != null ? this.value.hashCode() : 0);
113            hash = 17 * hash + (this.weak ? 1 : 0);
114            return hash;
115        }
116    
117        /**
118         * Convert the entity tag to a string suitable for use as the value of the
119         * corresponding HTTP header.
120         * @return a stringified entity tag
121         */
122        @Override
123        public String toString() {
124            return delegate.toString(this);
125        }
126    }