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