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

Last change on this file since 3779 was 3776, checked in by framm, 13 years ago

display re-licensing status in the user list, so mappers can immediately see who has agreed to the new contributor terms and who has not. code is still a little rough on error handling but should work. feedback encouraged.

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