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

Last change on this file since 12747 was 12537, checked in by Don-vip, 7 years ago

PMD - VariableNamingConventions

  • Property svn:eol-style set to native
File size: 7.0 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 lastUser.setPreferredName(name);
73 return lastUser;
74 }
75
76 Long ouid = uid;
77 User user = userMap.get(ouid);
78 if (user == null) {
79 user = new User(uid, name);
80 userMap.put(ouid, user);
81 }
82 if (name != null) user.addName(name);
83
84 lastUser = user;
85
86 return user;
87 }
88
89 /**
90 * clears the static map of user ids to user objects
91 */
92 public static synchronized void clearUserMap() {
93 userMap.clear();
94 lastUser = null;
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 LinkedHashSet<String> names = new LinkedHashSet<>();
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 * @see #getByName(String)
146 * @see #createOsmUser(long, String)
147 * @see #createLocalUser(String)
148 */
149 public String getName() {
150 return names.isEmpty() ? "" : names.iterator().next();
151 }
152
153 /**
154 * Returns the list of user names
155 *
156 * @return list of names
157 */
158 public List<String> getNames() {
159 return new ArrayList<>(names);
160 }
161
162 /**
163 * Adds a user name to the list if it is not there, yet.
164 *
165 * @param name User name
166 */
167 public void addName(String name) {
168 names.add(name);
169 }
170
171 /**
172 * Sets the preferred user name, i.e., the one that will be returned when calling {@link #getName()}.
173 *
174 * Rationale: A user can change its name multiple times and after reading various (outdated w.r.t. user name)
175 * data files it is unclear which is the up-to-date user name.
176 * @param name the preferred user name to set
177 */
178 public void setPreferredName(String name) {
179 if (names.size() == 1 && names.contains(name)) {
180 return;
181 }
182 final Collection<String> allNames = new LinkedHashSet<>(names);
183 names.clear();
184 names.add(name);
185 names.addAll(allNames);
186 }
187
188 /**
189 * Returns true if the name is in the names list
190 *
191 * @param name User name
192 * @return <code>true</code> if the name is in the names list
193 */
194 public boolean hasName(String name) {
195 return names.contains(name);
196 }
197
198 /**
199 * Replies the user id. If this user is known to the OSM server the positive user id
200 * from the server is replied. Otherwise, a negative local value is replied.
201 *
202 * A negative local is only unique during an editing session. It is lost when the
203 * application is closed and there is no guarantee that a negative local user id is
204 * always bound to a user with the same name.
205 *
206 * @return the user id
207 */
208 public long getId() {
209 return uid;
210 }
211
212 /**
213 * Private constructor, only called from get method.
214 * @param uid user id
215 * @param name user name
216 */
217 private User(long uid, String name) {
218 this.uid = uid;
219 if (name != null) {
220 addName(name);
221 }
222 }
223
224 /**
225 * Determines if this user is known to OSM
226 * @return {@code true} if this user is known to OSM, {@code false} otherwise
227 */
228 public boolean isOsmUser() {
229 return uid > 0;
230 }
231
232 /**
233 * Determines if this user is local
234 * @return {@code true} if this user is local, {@code false} otherwise
235 */
236 public boolean isLocalUser() {
237 return uid < 0;
238 }
239
240 @Override
241 public int hashCode() {
242 return Objects.hash(uid);
243 }
244
245 @Override
246 public boolean equals(Object obj) {
247 if (this == obj) return true;
248 if (obj == null || getClass() != obj.getClass()) return false;
249 User user = (User) obj;
250 return uid == user.uid;
251 }
252
253 @Override
254 public String toString() {
255 StringBuilder s = new StringBuilder();
256 s.append("id:").append(uid);
257 if (names.size() == 1) {
258 s.append(" name:").append(getName());
259 } else if (names.size() > 1) {
260 s.append(String.format(" %d names:%s", names.size(), getName()));
261 }
262 return s.toString();
263 }
264}
Note: See TracBrowser for help on using the repository browser.