001package com.nimbusds.jose;
002
003
004import java.io.Serializable;
005
006import net.jcip.annotations.Immutable;
007import net.minidev.json.JSONAware;
008import net.minidev.json.JSONObject;
009
010
011/**
012 * Compression algorithm name, represents the {@code zip} header parameter in 
013 * JSON Web Encryption (JWE) objects. This class is immutable.
014 *
015 * <p>Includes a constant for the standard DEFLATE compression algorithm:
016 *
017 * <ul>
018 *     <li>{@link #DEF}
019 * </ul> 
020 *
021 * <p>Additional compression algorithm names can be defined using the 
022 * constructor. 
023 *
024 * @author Vladimir Dzhuvinov
025 * @version 2013-01-15
026 */
027@Immutable
028public final class CompressionAlgorithm implements JSONAware, Serializable {
029
030
031        private static final long serialVersionUID = 1L;
032
033
034        /**
035         * DEFLATE Compressed Data Format Specification version 1.3, as 
036         * described in RFC 1951.
037         */
038        public static final CompressionAlgorithm DEF = new CompressionAlgorithm("DEF");
039
040
041        /**
042         * The algorithm name.
043         */
044        private final String name;
045
046
047        /**
048         * Creates a new compression algorithm with the specified name.
049         *
050         * @param name The compression algorithm name. Must not be {@code null}.
051         */
052        public CompressionAlgorithm(final String name) {
053
054                if (name == null) {
055                        throw new IllegalArgumentException("The compression algorithm name must not be null");
056                }
057
058                this.name = name;
059        }
060
061
062        /**
063         * Gets the name of this compression algorithm.
064         *
065         * @return The compression algorithm name.
066         */
067        public String getName() {
068
069                return name;
070        }
071
072
073        /**
074         * Overrides {@code Object.hashCode()}.
075         *
076         * @return The object hash code.
077         */
078        @Override
079        public int hashCode() {
080
081                return name.hashCode();
082        }
083
084
085        /**
086         * Overrides {@code Object.equals()}.
087         *
088         * @param object The object to compare to.
089         *
090         * @return {@code true} if the objects have the same value, otherwise
091         *         {@code false}.
092         */
093        @Override
094        public boolean equals(final Object object) {
095
096                return object != null && 
097                                object instanceof CompressionAlgorithm && 
098                                this.toString().equals(object.toString());
099        }
100
101
102        /**
103         * Returns the string representation of this compression algorithm.
104         *
105         * @see #getName
106         *
107         * @return The string representation.
108         */
109        @Override
110        public String toString() {
111
112                return name;
113        }
114
115
116        /**
117         * Returns the JSON string representation of this compression algorithm.
118         * 
119         * @return The JSON string representation.
120         */
121        @Override
122        public String toJSONString() {
123
124                return "\"" + JSONObject.escape(name) + '"';
125        }
126}