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$ (2013-01-15)
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 != null && 
092                           object instanceof CompressionAlgorithm && 
093                           this.toString().equals(object.toString());
094            }
095            
096            
097            /**
098             * Returns the string representation of this compression algorithm.
099             *
100             * @see #getName
101             *
102             * @return The string representation.
103             */
104            @Override
105            public String toString() {
106            
107                    return name;
108            }
109            
110            
111            /**
112             * Returns the JSON string representation of this compression algorithm.
113             * 
114             * @return The JSON string representation.
115             */
116            @Override
117            public String toJSONString() {
118            
119                    StringBuilder sb = new StringBuilder();
120                    sb.append('"');
121                    sb.append(JSONObject.escape(name));
122                    sb.append('"');
123                    return sb.toString();
124            }
125    }