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;
024
025import net.minidev.json.JSONAware;
026import net.minidev.json.JSONObject;
027
028
029/**
030 * The base class for algorithm names, with optional implementation 
031 * requirement. This class is immutable.
032 *
033 * <p>Includes constants for the following standard algorithm names:
034 *
035 * <ul>
036 *     <li>{@link #NONE none}
037 * </ul>
038 *
039 * @author Vladimir Dzhuvinov 
040 * @version 2013-03-27
041 */
042@Immutable
043public class Algorithm implements JSONAware, Serializable {
044
045
046        private static final long serialVersionUID = 1L;
047
048
049        /**
050         * No algorithm (unsecured JOSE object without signature / encryption).
051         */
052        public static final Algorithm NONE = new Algorithm("none", Requirement.REQUIRED);
053
054
055        /**
056         * The algorithm name.
057         */
058        private final String name;
059
060
061        /**
062         * The implementation requirement, {@code null} if not known.
063         */
064        private final Requirement requirement;
065
066
067        /**
068         * Creates a new JOSE algorithm name.
069         *
070         * @param name The algorithm name. Must not be {@code null}.
071         * @param req  The implementation requirement, {@code null} if not 
072         *             known.
073         */
074        public Algorithm(final String name, final Requirement req) {
075
076                if (name == null) {
077
078                        throw new IllegalArgumentException("The algorithm name must not be null");
079                }
080
081                this.name = name;
082
083                requirement = req;
084        }
085
086
087        /**
088         * Creates a new JOSE algorithm name.
089         *
090         * @param name The algorithm name. Must not be {@code null}.
091         */
092        public Algorithm(final String name) {
093
094                this(name, null);
095        }
096
097
098        /**
099         * Gets the name of this algorithm.
100         *
101         * @return The algorithm name.
102         */
103        public final String getName() {
104
105                return name;
106        }
107
108
109        /**
110         * Gets the implementation requirement of this algorithm.
111         *
112         * @return The implementation requirement, {@code null} if not known.
113         */
114        public final Requirement getRequirement() {
115
116                return requirement;
117        }
118
119
120        /**
121         * Overrides {@code Object.hashCode()}.
122         *
123         * @return The object hash code.
124         */
125        @Override
126        public final int hashCode() {
127
128                return name.hashCode();
129        }
130
131
132        /**
133         * Overrides {@code Object.equals()}.
134         *
135         * @param object The object to compare to.
136         *
137         * @return {@code true} if the objects have the same value, otherwise
138         *         {@code false}.
139         */
140        @Override
141        public boolean equals(final Object object) {
142
143                return object != null && 
144                       object instanceof Algorithm && 
145                       this.toString().equals(object.toString());
146        }
147
148
149        /**
150         * Returns the string representation of this algorithm.
151         *
152         * @see #getName
153         *
154         * @return The string representation.
155         */
156        @Override
157        public final String toString() {
158
159                return name;
160        }
161
162
163        /**
164         * Returns the JSON string representation of this algorithm.
165         * 
166         * @return The JSON string representation.
167         */
168        @Override
169        public final String toJSONString() {
170
171                return "\"" + JSONObject.escape(name) + '"';
172        }
173}