001package com.nimbusds.jose; 002 003 004import net.jcip.annotations.Immutable; 005import net.minidev.json.JSONAware; 006import net.minidev.json.JSONObject; 007 008 009/** 010 * Compression algorithm name, represents the {@code zip} header parameter in 011 * JSON Web Encryption (JWE) objects. This class is immutable. 012 * 013 * <p>Includes a constant for the standard DEFLATE compression algorithm: 014 * 015 * <ul> 016 * <li>{@link #DEF} 017 * </ul> 018 * 019 * <p>Additional compression algorithm names can be defined using the 020 * constructor. 021 * 022 * @author Vladimir Dzhuvinov 023 * @version $version$ (2013-01-15) 024 */ 025@Immutable 026public final class CompressionAlgorithm implements JSONAware { 027 028 029 /** 030 * DEFLATE Compressed Data Format Specification version 1.3, as 031 * described in RFC 1951. 032 */ 033 public static final CompressionAlgorithm DEF = new CompressionAlgorithm("DEF"); 034 035 036 /** 037 * The algorithm name. 038 */ 039 private final String name; 040 041 042 /** 043 * Creates a new compression algorithm with the specified name. 044 * 045 * @param name The compression algorithm name. Must not be {@code null}. 046 */ 047 public CompressionAlgorithm(final String name) { 048 049 if (name == null) { 050 throw new IllegalArgumentException("The compression algorithm name must not be null"); 051 } 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}