aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/entity/User.java
blob: 04f3e4fc49f8e10a5a42384709bab8cbc8dcb975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package net.lacolaco.smileessence.entity;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

public class User {
    // 重複防止用キャッシュ こっちは weak reference
    private static Cache<Long, User> storage = CacheBuilder.newBuilder().weakValues().build();

    public synchronized static User fetch(long userId) {
        return storage.getIfPresent(userId);
    }

    public synchronized static User fromTwitter(final twitter4j.User st) {
        User u = fetch(st.getId());
        if (u == null) {
            u = new User(st);
            storage.put(st.getId(), u);
        } else {
            u.update(st);
        }
        return u;
    }

    // インスタンス
    private long id;
    private boolean isProtected;
    private String screenName;
    private String name;
    private String profileImageUrl;
    private String profileBannerUrl;
    private String description;
    private String location;
    private String url;
    private int favoritesCount;
    private int statusesCount;
    private int friendsCount;
    private int followersCount;
    private boolean isVerified;

    private User(twitter4j.User st) {
        update(st);
    }

    private void update(twitter4j.User user) {
        id = user.getId();
        isProtected = user.isProtected();
        screenName = user.getScreenName();
        name = user.getName();
        profileImageUrl = user.getProfileImageURLHttps();
        profileBannerUrl = user.getProfileBannerURL();
        description = user.getDescription();
        location = user.getLocation();
        url = user.getURL();
        favoritesCount = user.getFavouritesCount();
        statusesCount = user.getStatusesCount();
        friendsCount = user.getFriendsCount();
        followersCount = user.getFollowersCount();
        isVerified = user.isVerified();
    }

    public long getId() {
        return id;
    }

    public boolean isProtected() {
        return isProtected;
    }

    public String getScreenName() {
        return screenName;
    }

    public String getName() {
        return name;
    }

    public String getProfileImageUrl() {
        return profileImageUrl;
    }

    public String getProfileImageUrlOriginal() {
        return getProfileImageUrlWithSuffix("");
    }

    private String getProfileImageUrlWithSuffix(String suffix) {
        String original = getProfileImageUrl();
        if (original != null) {
            String url = original.substring(0, original.lastIndexOf("_")) + suffix;
            int extIndex = original.lastIndexOf(".");
            if (extIndex > original.lastIndexOf("/")) {
                url += original.substring(extIndex);
            }
            return url;
        }
        return null;
    }

    public String getProfileBannerUrl() {
        return profileBannerUrl;
    }

    public String getDescription() {
        return description;
    }

    public String getLocation() {
        return location;
    }

    public String getUrl() {
        return url;
    }

    public int getFavoritesCount() {
        return favoritesCount;
    }

    public int getStatusesCount() {
        return statusesCount;
    }

    public int getFriendsCount() {
        return friendsCount;
    }

    public int getFollowersCount() {
        return followersCount;
    }

    public boolean isVerified() {
        return isVerified;
    }
}