001/*
002 * nimbus-jose-jwt
003 *
004 * Copyright 2012-2016, Connect2id Ltd.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.jose;
019
020
021import com.nimbusds.jose.util.JSONStringUtils;
022import net.jcip.annotations.Immutable;
023
024import java.io.Serializable;
025import java.util.Objects;
026
027
028/**
029 * Compression algorithm name, represents the {@code zip} header parameter in 
030 * JSON Web Encryption (JWE) objects. This class is immutable.
031 *
032 * <p>Includes a constant for the standard DEFLATE compression algorithm:
033 *
034 * <ul>
035 *     <li>{@link #DEF}
036 * </ul> 
037 *
038 * <p>Additional compression algorithm names can be defined using the 
039 * constructor. 
040 *
041 * @author Vladimir Dzhuvinov
042 * @version 2024-04-20
043 */
044@Immutable
045public final class CompressionAlgorithm implements Serializable {
046
047
048        private static final long serialVersionUID = 1L;
049
050
051        /**
052         * DEFLATE Compressed Data Format Specification version 1.3, as 
053         * described in RFC 1951.
054         */
055        public static final CompressionAlgorithm DEF = new CompressionAlgorithm("DEF");
056
057
058        /**
059         * The algorithm name.
060         */
061        private final String name;
062
063
064        /**
065         * Creates a new compression algorithm with the specified name.
066         *
067         * @param name The compression algorithm name. Must not be {@code null}.
068         */
069        public CompressionAlgorithm(final String name) {
070
071                this.name = Objects.requireNonNull(name);
072        }
073
074
075        /**
076         * Gets the name of this compression algorithm.
077         *
078         * @return The compression algorithm name.
079         */
080        public String getName() {
081
082                return name;
083        }
084
085
086        /**
087         * Overrides {@code Object.hashCode()}.
088         *
089         * @return The object hash code.
090         */
091        @Override
092        public int hashCode() {
093
094                return name.hashCode();
095        }
096
097
098        /**
099         * Overrides {@code Object.equals()}.
100         *
101         * @param object The object to compare to.
102         *
103         * @return {@code true} if the objects have the same value, otherwise
104         *         {@code false}.
105         */
106        @Override
107        public boolean equals(final Object object) {
108
109                return object instanceof CompressionAlgorithm &&
110                        this.toString().equals(object.toString());
111        }
112
113
114        /**
115         * Returns the string representation of this compression algorithm.
116         *
117         * @see #getName
118         *
119         * @return The string representation.
120         */
121        @Override
122        public String toString() {
123
124                return name;
125        }
126
127
128        /**
129         * Returns the JSON string representation of this compression algorithm.
130         * 
131         * @return The JSON string representation.
132         */
133        public String toJSONString() {
134                return JSONStringUtils.toJSONString(name);
135        }
136}