001    package com.nimbusds.jose;
002    
003    
004    import net.minidev.json.JSONAware;
005    import net.minidev.json.JSONObject;
006    
007    import net.jcip.annotations.Immutable;
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-01-15)
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                            throw new IllegalArgumentException("The algorithm name must not be null");
056                    
057                    this.name = name;
058                    
059                    requirement = req;
060            }
061            
062            
063            /**
064             * Creates a new JOSE algorithm name.
065             *
066             * @param name The algorithm name. Must not be {@code null}.
067             */
068            public Algorithm(final String name) {
069            
070                    this(name, null);
071            }
072            
073            
074            /**
075             * Gets the name of this algorithm.
076             *
077             * @return The algorithm name.
078             */
079            public String getName() {
080            
081                    return name;
082            }
083            
084            
085            /**
086             * Gets the implementation requirement of this algorithm.
087             *
088             * @return The implementation requirement, {@code null} if not known.
089             */
090            public Requirement getRequirement() {
091            
092                    return requirement;
093            }
094            
095            
096            /**
097             * Overrides {@code Object.hashCode()}.
098             *
099             * @return The object hash code.
100             */
101            @Override
102            public int hashCode() {
103            
104                    return name.hashCode();
105            }
106            
107            
108            /**
109             * Overrides {@code Object.equals()}.
110             *
111             * @param object The object to compare to.
112             *
113             * @return {@code true} if the objects have the same value, otherwise
114             *         {@code false}.
115             */
116            @Override
117            public boolean equals(final Object object) {
118            
119                    return object != null && 
120                           object instanceof Algorithm && 
121                           this.toString().equals(object.toString());
122            }
123            
124            
125            /**
126             * Returns the string representation of this algorithm.
127             *
128             * @see #getName
129             *
130             * @return The string representation.
131             */
132            @Override
133            public String toString() {
134            
135                    return name;
136            }
137            
138            
139            /**
140             * Returns the JSON string representation of this algorithm.
141             * 
142             * @return The JSON string representation.
143             */
144            @Override
145            public String toJSONString() {
146            
147                    StringBuilder sb = new StringBuilder();
148                    sb.append('"');
149                    sb.append(JSONObject.escape(name));
150                    sb.append('"');
151                    return sb.toString();
152            }
153    }