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