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

Last change on this file since 8509 was 8470, checked in by Don-vip, 9 years ago

javadoc fixes. Removed one duplicated method in exception handling

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