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

Last change on this file since 13660 was 13101, checked in by Don-vip, 6 years ago

fix #15534 - NPE

  • Property svn:eol-style set to native
File size: 7.1 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.Collection;
8import java.util.HashMap;
9import java.util.LinkedHashSet;
10import java.util.List;
11import java.util.Map;
12import java.util.Objects;
13
14/**
15 * A simple class to keep a list of user names.
16 *
17 * Instead of storing user names as strings with every OSM primitive, we store
18 * a reference to an user object, and make sure that for each username there
19 * is only one user object.
20 *
21 * @since 227
22 */
23public final class User {
24
25 private static long uidCounter;
26
27 /**
28 * the map of known users
29 */
30 private static Map<Long, User> userMap = new HashMap<>();
31
32 /**
33 * The anonymous user is a local user used in places where no user is known.
34 * @see #getAnonymous()
35 */
36 private static final User ANONYMOUS = createLocalUser(tr("<anonymous>"));
37
38 private static long getNextLocalUid() {
39 uidCounter--;
40 return uidCounter;
41 }
42
43 /**
44 * Creates a local user with the given name
45 *
46 * @param name the name
47 * @return a new local user with the given name
48 */
49 public static synchronized User createLocalUser(String name) {
50 for (long i = -1; i >= uidCounter; --i) {
51 User olduser = getById(i);
52 if (olduser != null && olduser.hasName(name))
53 return olduser;
54 }
55 User user = new User(getNextLocalUid(), name);
56 userMap.put(user.getId(), user);
57 return user;
58 }
59
60 private static User lastUser;
61
62 /**
63 * Creates a user known to the OSM server
64 *
65 * @param uid the user id
66 * @param name the name
67 * @return a new OSM user with the given name and uid
68 */
69 public static synchronized User createOsmUser(long uid, String name) {
70
71 if (lastUser != null && lastUser.getId() == uid) {
72 if (name != null) {
73 lastUser.setPreferredName(name);
74 }
75 return lastUser;
76 }
77
78 User user = userMap.computeIfAbsent(uid, k -> new User(uid, name));
79 if (name != null) user.addName(name);
80
81 lastUser = user;
82
83 return user;
84 }
85
86 /**
87 * clears the static map of user ids to user objects
88 */
89 public static synchronized void clearUserMap() {
90 userMap.clear();
91 lastUser = null;
92 }
93
94 /**
95 * Returns the user with user id <code>uid</code> or null if this user doesn't exist
96 *
97 * @param uid the user id
98 * @return the user; null, if there is no user with this id
99 */
100 public static synchronized User getById(long uid) {
101 return userMap.get(uid);
102 }
103
104 /**
105 * Returns the list of users with name <code>name</code> or the empty list if
106 * no such users exist
107 *
108 * @param name the user name
109 * @return the list of users with name <code>name</code> or the empty list if
110 * no such users exist
111 */
112 public static synchronized List<User> getByName(String name) {
113 if (name == null) {
114 name = "";
115 }
116 List<User> ret = new ArrayList<>();
117 for (User user: userMap.values()) {
118 if (user.hasName(name)) {
119 ret.add(user);
120 }
121 }
122 return ret;
123 }
124
125 /**
126 * Replies the anonymous user
127 * @return The anonymous user
128 */
129 public static User getAnonymous() {
130 return ANONYMOUS;
131 }
132
133 /** the user name */
134 private final LinkedHashSet<String> names = new LinkedHashSet<>();
135 /** the user id */
136 private final long uid;
137
138 /**
139 * Replies the user name
140 *
141 * @return the user name. Never <code>null</code>, but may be the empty string
142 * @see #getByName(String)
143 * @see #createOsmUser(long, String)
144 * @see #createLocalUser(String)
145 */
146 public String getName() {
147 return names.isEmpty() ? "" : names.iterator().next();
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 * @throws NullPointerException if name is null
164 */
165 public void addName(String name) {
166 names.add(Objects.requireNonNull(name, "name"));
167 }
168
169 /**
170 * Sets the preferred user name, i.e., the one that will be returned when calling {@link #getName()}.
171 *
172 * Rationale: A user can change its name multiple times and after reading various (outdated w.r.t. user name)
173 * data files it is unclear which is the up-to-date user name.
174 * @param name the preferred user name to set
175 * @throws NullPointerException if name is null
176 */
177 public void setPreferredName(String name) {
178 if (names.size() == 1 && names.contains(name)) {
179 return;
180 }
181 final Collection<String> allNames = new LinkedHashSet<>(names);
182 names.clear();
183 names.add(Objects.requireNonNull(name, "name"));
184 names.addAll(allNames);
185 }
186
187 /**
188 * Returns true if the name is in the names list
189 *
190 * @param name User name
191 * @return <code>true</code> if the name is in the names list
192 */
193 public boolean hasName(String name) {
194 return names.contains(name);
195 }
196
197 /**
198 * Replies the user id. If this user is known to the OSM server the positive user id
199 * from the server is replied. Otherwise, a negative local value is replied.
200 *
201 * A negative local is only unique during an editing session. It is lost when the
202 * application is closed and there is no guarantee that a negative local user id is
203 * always bound to a user with the same name.
204 *
205 * @return the user id
206 */
207 public long getId() {
208 return uid;
209 }
210
211 /**
212 * Private constructor, only called from get method.
213 * @param uid user id
214 * @param name user name
215 */
216 private User(long uid, String name) {
217 this.uid = uid;
218 if (name != null) {
219 addName(name);
220 }
221 }
222
223 /**
224 * Determines if this user is known to OSM
225 * @return {@code true} if this user is known to OSM, {@code false} otherwise
226 */
227 public boolean isOsmUser() {
228 return uid > 0;
229 }
230
231 /**
232 * Determines if this user is local
233 * @return {@code true} if this user is local, {@code false} otherwise
234 */
235 public boolean isLocalUser() {
236 return uid < 0;
237 }
238
239 @Override
240 public int hashCode() {
241 return Objects.hash(uid);
242 }
243
244 @Override
245 public boolean equals(Object obj) {
246 if (this == obj) return true;
247 if (obj == null || getClass() != obj.getClass()) return false;
248 User user = (User) obj;
249 return uid == user.uid;
250 }
251
252 @Override
253 public String toString() {
254 StringBuilder s = new StringBuilder();
255 s.append("id:").append(uid);
256 if (names.size() == 1) {
257 s.append(" name:").append(getName());
258 } else if (names.size() > 1) {
259 s.append(String.format(" %d names:%s", names.size(), getName()));
260 }
261 return s.toString();
262 }
263}
Note: See TracBrowser for help on using the repository browser.