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