001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.oauth;
003
004import org.openstreetmap.josm.tools.CheckParameterUtil;
005
006import oauth.signpost.OAuthConsumer;
007
008public class OAuthToken {
009
010    /**
011     * Creates an OAuthToken from the token currently managed by the {@link OAuthConsumer}.
012     *
013     * @param consumer the consumer
014     * @return the token
015     */
016    public static OAuthToken createToken(OAuthConsumer consumer) {
017        return new OAuthToken(consumer.getToken(), consumer.getTokenSecret());
018    }
019
020    private final String key;
021    private final String secret;
022
023    /**
024     * Creates a new token
025     *
026     * @param key the token key
027     * @param secret the token secret
028     */
029    public OAuthToken(String key, String secret) {
030        this.key = key;
031        this.secret = secret;
032    }
033
034    /**
035     * Creates a clone of another token
036     *
037     * @param other the other token. Must not be null.
038     * @throws IllegalArgumentException if other is null
039     */
040    public OAuthToken(OAuthToken other) {
041        CheckParameterUtil.ensureParameterNotNull(other, "other");
042        this.key = other.key;
043        this.secret = other.secret;
044    }
045
046    /**
047     * Replies the token key
048     *
049     * @return the token key
050     */
051    public String getKey() {
052        return key;
053    }
054
055    /**
056     * Replies the token secret
057     *
058     * @return the token secret
059     */
060    public String getSecret() {
061        return secret;
062    }
063
064    @Override
065    public int hashCode() {
066        final int prime = 31;
067        int result = 1;
068        result = prime * result + ((key == null) ? 0 : key.hashCode());
069        result = prime * result + ((secret == null) ? 0 : secret.hashCode());
070        return result;
071    }
072
073    @Override
074    public boolean equals(Object obj) {
075        if (this == obj)
076            return true;
077        if (obj == null)
078            return false;
079        if (getClass() != obj.getClass())
080            return false;
081        OAuthToken other = (OAuthToken) obj;
082        if (key == null) {
083            if (other.key != null)
084                return false;
085        } else if (!key.equals(other.key))
086            return false;
087        if (secret == null) {
088            if (other.secret != null)
089                return false;
090        } else if (!secret.equals(other.secret))
091            return false;
092        return true;
093    }
094}