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 java.io.Serializable; 022 023import net.jcip.annotations.Immutable; 024import net.minidev.json.JSONAware; 025import net.minidev.json.JSONObject; 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 2013-01-15 043 */ 044@Immutable 045public final class CompressionAlgorithm implements JSONAware, 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 if (name == null) { 072 throw new IllegalArgumentException("The compression algorithm name must not be null"); 073 } 074 075 this.name = name; 076 } 077 078 079 /** 080 * Gets the name of this compression algorithm. 081 * 082 * @return The compression algorithm name. 083 */ 084 public String getName() { 085 086 return name; 087 } 088 089 090 /** 091 * Overrides {@code Object.hashCode()}. 092 * 093 * @return The object hash code. 094 */ 095 @Override 096 public int hashCode() { 097 098 return name.hashCode(); 099 } 100 101 102 /** 103 * Overrides {@code Object.equals()}. 104 * 105 * @param object The object to compare to. 106 * 107 * @return {@code true} if the objects have the same value, otherwise 108 * {@code false}. 109 */ 110 @Override 111 public boolean equals(final Object object) { 112 113 return object != null && 114 object instanceof CompressionAlgorithm && 115 this.toString().equals(object.toString()); 116 } 117 118 119 /** 120 * Returns the string representation of this compression algorithm. 121 * 122 * @see #getName 123 * 124 * @return The string representation. 125 */ 126 @Override 127 public String toString() { 128 129 return name; 130 } 131 132 133 /** 134 * Returns the JSON string representation of this compression algorithm. 135 * 136 * @return The JSON string representation. 137 */ 138 @Override 139 public String toJSONString() { 140 141 return "\"" + JSONObject.escape(name) + '"'; 142 } 143}