001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2011 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.model;
020
021import java.util.Date;
022
023import org.shredzone.flattr4j.connector.FlattrObject;
024import org.shredzone.flattr4j.oauth.RequiredScope;
025import org.shredzone.flattr4j.oauth.Scope;
026
027/**
028 * A {@link User}. Two {@link User} are considered equal if they contain the same id.
029 *
030 * @author Richard "Shred" Körber
031 */
032public class User extends Resource implements UserId {
033    private static final long serialVersionUID = 594781523400164895L;
034
035    /**
036     * Returns a {@link UserId} for the given User id.
037     *
038     * @param id
039     *            User id
040     * @return A {@link UserId} object for this id
041     */
042    public static UserId withId(final String id) {
043        return new UserId() {
044            @Override
045            public String getUserId() {
046                return id;
047            }
048        };
049    }
050
051    public User(FlattrObject data) {
052        super(data);
053    }
054
055    /**
056     * User id. Use this for referencing an user at the Flattr API.
057     * <p>
058     * Note: At the moment, the user's login name is returned. In a future version, this
059     * may change to the user identifier.
060     */
061    @Override
062    public String getUserId() {
063        return data.get("username");
064    }
065
066    /**
067     * Unique user identifier. Unlike the user id, this identifier cannot be changed.
068     * It cannot be used for API calls yet, but you should use it whenever your
069     * application needs a unique identifier.
070     *
071     * @since 2.8
072     */
073    public String getIdentifier() {
074        return data.get("id");
075    }
076
077    /**
078     * User login name. It is recommended that you use this method when you want to
079     * retrieve the user login name (e.g. for displaying it to the user). If you want to
080     * have a user reference for the Flattr API, use {@link #getUserId()} instead.
081     *
082     * @since 2.8
083     */
084    public String getUsername() {
085        return data.get("username");
086    }
087
088    /**
089     * URL that returns details of this resource as JSON.
090     */
091    public String getResource() {
092        return data.get("resource");
093    }
094
095    /**
096     * Human readable link to this resource at Flattr.
097     */
098    public String getLink() {
099        return data.get("link");
100    }
101
102    /**
103     * User's real first name.
104     */
105    public String getFirstname() {
106        return data.get("firstname");
107    }
108
109    /**
110     * User's real last name.
111     */
112    public String getLastname() {
113        return data.get("lastname");
114    }
115
116    /**
117     * City the user lives in.
118     */
119    public String getCity() {
120        return data.get("city");
121    }
122
123    /**
124     * Country the user lives in.
125     */
126    public String getCountry() {
127        return data.get("country");
128    }
129
130    /**
131     * URL of the user's home page.
132     *
133     * @since 2.5
134     */
135    public String getUrl() {
136        return data.get("url");
137    }
138
139    /**
140     * User's email address.
141     */
142    @RequiredScope(Scope.EXTENDEDREAD)
143    public String getEmail() {
144        return data.get("email");
145    }
146
147    /**
148     * User's own description.
149     */
150    public String getDescription() {
151        return data.get("about");
152    }
153
154    /**
155     * URL of the user's picture at Gravatar.
156     */
157    public String getGravatar() {
158        return data.get("avatar");
159    }
160
161    /**
162     * {@code true} if the user can flattr other users, {@code false} if not.
163     *
164     * @since 2.8
165     */
166    public boolean isActiveSupporter() {
167        return data.getInt("active_supporter") == 1;
168    }
169
170    /**
171     * Registration date.
172     *
173     * @since 2.0
174     */
175    @RequiredScope(Scope.EXTENDEDREAD)
176    public Date getRegisteredAt() {
177        return data.getDate("registered_at");
178    }
179
180    @Override
181    public boolean equals(Object obj) {
182        String pk = getUserId();
183        if (pk == null || obj == null || !(obj instanceof User)) {
184            return false;
185        }
186        return pk.equals(((User) obj).getUserId());
187    }
188
189    @Override
190    public int hashCode() {
191        String pk = getUserId();
192        return (pk != null ? pk.hashCode() : 0);
193    }
194
195}