source: josm/trunk/src/org/openstreetmap/josm/data/osm/User.java@ 9600

Last change on this file since 9600 was 9371, checked in by simon04, 8 years ago

Java 7: use Objects.equals and Objects.hash

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.osm;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Map;
11import java.util.Objects;
12import java.util.Set;
13
14import org.openstreetmap.josm.tools.Utils;
15
16/**
17 * A simple class to keep a list of user names.
18 *
19 * Instead of storing user names as strings with every OSM primitive, we store
20 * a reference to an user object, and make sure that for each username there
21 * is only one user object.
22 *
23 * @since 227
24 */
25public final class User {
26
27 private static long uidCounter;
28
29 /**
30 * the map of known users
31 */
32 private static Map<Long, User> userMap = new HashMap<>();
33
34 /**
35 * The anonymous user is a local user used in places where no user is known.
36 * @see #getAnonymous()
37 */
38 private static final User anonymous = createLocalUser(tr("<anonymous>"));
39
40 private static long getNextLocalUid() {
41 uidCounter--;
42 return uidCounter;
43 }
44
45 /**
46 * Creates a local user with the given name
47 *
48 * @param name the name
49 * @return a new local user with the given name
50 */
51 public static synchronized User createLocalUser(String name) {
52 for (long i = -1; i >= uidCounter; --i) {
53 User olduser = getById(i);
54 if (olduser != null && olduser.hasName(name))
55 return olduser;
56 }
57 User user = new User(getNextLocalUid(), name);
58 userMap.put(user.getId(), user);
59 return user;
60 }
61
62 private static User lastUser;
63
64 /**
65 * Creates a user known to the OSM server
66 *
67 * @param uid the user id
68 * @param name the name
69 * @return a new OSM user with the given name and uid
70 */
71 public static synchronized User createOsmUser(long uid, String name) {
72
73 if (lastUser != null && lastUser.getId() == uid) {
74 return lastUser;
75 }
76
77 Long ouid = uid;
78 User user = userMap.get(ouid);
79 if (user == null) {
80 user = new User(uid, name);
81 userMap.put(ouid, user);
82 }
83 if (name != null) user.addName(name);
84
85 lastUser = user;
86
87 return user;
88 }
89
90 /**
91 * clears the static map of user ids to user objects
92 */
93 public static synchronized void clearUserMap() {
94 userMap.clear();
95 }
96
97 /**
98 * Returns the user with user id <code>uid</code> or null if this user doesn't exist
99 *
100 * @param uid the user id
101 * @return the user; null, if there is no user with this id
102 */
103 public static synchronized User getById(long uid) {
104 return userMap.get(uid);
105 }
106
107 /**
108 * Returns the list of users with name <code>name</code> or the empty list if
109 * no such users exist
110 *
111 * @param name the user name
112 * @return the list of users with name <code>name</code> or the empty list if
113 * no such users exist
114 */
115 public static synchronized List<User> getByName(String name) {
116 if (name == null) {
117 name = "";
118 }
119 List<User> ret = new ArrayList<>();
120 for (User user: userMap.values()) {
121 if (user.hasName(name)) {
122 ret.add(user);
123 }
124 }
125 return ret;
126 }
127
128 /**
129 * Replies the anonymous user
130 * @return The anonymous user
131 */
132 public static User getAnonymous() {
133 return anonymous;
134 }
135
136 /** the user name */
137 private final Set<String> names = new HashSet<>();
138 /** the user id */
139 private final long uid;
140
141 /**
142 * Replies the user name
143 *
144 * @return the user name. Never <code>null</code>, but may be the empty string
145 */
146 public String getName() {
147 return Utils.join("/", names);
148 }
149
150 /**
151 * Returns the list of user names
152 *
153 * @return list of names
154 */
155 public List<String> getNames() {
156 return new ArrayList<>(names);
157 }
158
159 /**
160 * Adds a user name to the list if it is not there, yet.
161 *
162 * @param name User name
163 */
164 public void addName(String name) {
165 names.add(name);
166 }
167
168 /**
169 * Returns true if the name is in the names list
170 *
171 * @param name User name
172 * @return <code>true</code> if the name is in the names list
173 */
174 public boolean hasName(String name) {
175 return names.contains(name);
176 }
177
178 /**
179 * Replies the user id. If this user is known to the OSM server the positive user id
180 * from the server is replied. Otherwise, a negative local value is replied.
181 *
182 * A negative local is only unique during an editing session. It is lost when the
183 * application is closed and there is no guarantee that a negative local user id is
184 * always bound to a user with the same name.
185 *
186 * @return the user id
187 */
188 public long getId() {
189 return uid;
190 }
191
192 /**
193 * Private constructor, only called from get method.
194 * @param uid user id
195 * @param name user name
196 */
197 private User(long uid, String name) {
198 this.uid = uid;
199 if (name != null) {
200 addName(name);
201 }
202 }
203
204 /**
205 * Determines if this user is known to OSM
206 * @return {@code true} if this user is known to OSM, {@code false} otherwise
207 */
208 public boolean isOsmUser() {
209 return uid > 0;
210 }
211
212 /**
213 * Determines if this user is local
214 * @return {@code true} if this user is local, {@code false} otherwise
215 */
216 public boolean isLocalUser() {
217 return uid < 0;
218 }
219
220 @Override
221 public int hashCode() {
222 return Objects.hash(uid);
223 }
224
225 @Override
226 public boolean equals(Object obj) {
227 if (this == obj) return true;
228 if (obj == null || getClass() != obj.getClass()) return false;
229 User user = (User) obj;
230 return uid == user.uid;
231 }
232
233 @Override
234 public String toString() {
235 StringBuilder s = new StringBuilder();
236 s.append("id:").append(uid);
237 if (names.size() == 1) {
238 s.append(" name:").append(getName());
239 } else if (names.size() > 1) {
240 s.append(String.format(" %d names:%s", names.size(), getName()));
241 }
242 return s.toString();
243 }
244}
Note: See TracBrowser for help on using the repository browser.