001    package com.nimbusds.jose;
002    
003    
004    import net.jcip.annotations.Immutable;
005    import net.minidev.json.JSONAware;
006    import 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-01-15)
021     */
022    @Immutable
023    public 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                            throw new IllegalArgumentException("The algorithm name must not be null");
055                    }
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    }