1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.concurrent.atomic.AtomicLong;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * A simple class to keep a list of user names.
|
---|
12 | *
|
---|
13 | * Instead of storing user names as strings with every OSM primitive, we store
|
---|
14 | * a reference to an user object, and make sure that for each username there
|
---|
15 | * is only one user object.
|
---|
16 | *
|
---|
17 | *
|
---|
18 | */
|
---|
19 | public class User {
|
---|
20 |
|
---|
21 | static private AtomicLong uidCounter = new AtomicLong();
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * the map of known users
|
---|
25 | */
|
---|
26 | private static HashMap<Long,User> userMap = new HashMap<Long,User>();
|
---|
27 |
|
---|
28 | private static long getNextLocalUid() {
|
---|
29 | return uidCounter.decrementAndGet();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Creates a local user with the given name
|
---|
34 | *
|
---|
35 | * @param name the name
|
---|
36 | */
|
---|
37 | public static User createLocalUser(String name) {
|
---|
38 | User user = new User(getNextLocalUid(), name);
|
---|
39 | userMap.put(user.getId(), user);
|
---|
40 | return user;
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Creates a user known to the OSM server
|
---|
45 | *
|
---|
46 | * @param uid the user id
|
---|
47 | * @param name the name
|
---|
48 | */
|
---|
49 | public static User createOsmUser(long uid, String name) {
|
---|
50 | User user = userMap.get(uid);
|
---|
51 | if (user == null) {
|
---|
52 | user = new User(uid, name);
|
---|
53 | userMap.put(user.getId(), user);
|
---|
54 | }
|
---|
55 | user.addName(name);
|
---|
56 | return user;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * clears the static map of user ids to user objects
|
---|
61 | *
|
---|
62 | */
|
---|
63 | public static void clearUserMap() {
|
---|
64 | userMap.clear();
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Returns the user with user id <code>uid</code> or null if this user doesn't exist
|
---|
69 | *
|
---|
70 | * @param uid the user id
|
---|
71 | * @return the user; null, if there is no user with this id
|
---|
72 | */
|
---|
73 | public static User getById(long uid) {
|
---|
74 | return userMap.get(uid);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Returns the list of users with name <code>name</code> or the empty list if
|
---|
79 | * no such users exist
|
---|
80 | *
|
---|
81 | * @param name the user name
|
---|
82 | * @return the list of users with name <code>name</code> or the empty list if
|
---|
83 | * no such users exist
|
---|
84 | */
|
---|
85 | public static List<User> getByName(String name) {
|
---|
86 | if (name == null) {
|
---|
87 | name = "";
|
---|
88 | }
|
---|
89 | List<User> ret = new ArrayList<User>();
|
---|
90 | for (User user: userMap.values()) {
|
---|
91 | if (user.hasName(name)) {
|
---|
92 | ret.add(user);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | return ret;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** the user name */
|
---|
99 | private final HashSet<String> names = new HashSet<String>();
|
---|
100 | /** the user id */
|
---|
101 | private final long uid;
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Replies the user name
|
---|
105 | *
|
---|
106 | * @return the user name. Never null, but may be the empty string
|
---|
107 | */
|
---|
108 | public String getName() {
|
---|
109 | StringBuilder sb = new StringBuilder();
|
---|
110 | for (String name: names) {
|
---|
111 | sb.append(name);
|
---|
112 | sb.append('/');
|
---|
113 | }
|
---|
114 | sb.deleteCharAt(sb.length() - 1);
|
---|
115 | return sb.toString();
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Returns the list of user names
|
---|
120 | *
|
---|
121 | * @returns list of names
|
---|
122 | */
|
---|
123 | public ArrayList<String> getNames() {
|
---|
124 | return new ArrayList<String>(names);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Adds a user name to the list if it is not there, yet.
|
---|
129 | *
|
---|
130 | * @param name
|
---|
131 | */
|
---|
132 | public void addName(String name) {
|
---|
133 | names.add(name);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Returns true if the name is in the names list
|
---|
138 | *
|
---|
139 | * @param name
|
---|
140 | */
|
---|
141 | public boolean hasName(String name) {
|
---|
142 | return names.contains(name);
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Replies the user id. If this user is known to the OSM server the positive user id
|
---|
147 | * from the server is replied. Otherwise, a negative local value is replied.
|
---|
148 | *
|
---|
149 | * A negative local is only unique during an editing session. It is lost when the
|
---|
150 | * application is closed and there is no guarantee that a negative local user id is
|
---|
151 | * always bound to a user with the same name.
|
---|
152 | *
|
---|
153 | */
|
---|
154 | public long getId() {
|
---|
155 | return uid;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** private constructor, only called from get method. */
|
---|
159 | private User(long uid, String name) {
|
---|
160 | this.uid = uid;
|
---|
161 | if (name != null) {
|
---|
162 | addName(name);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | public boolean isOsmUser() {
|
---|
167 | return uid > 0;
|
---|
168 | }
|
---|
169 |
|
---|
170 | public boolean isLocalUser() {
|
---|
171 | return uid < 0;
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public int hashCode() {
|
---|
176 | final int prime = 31;
|
---|
177 | int result = 1;
|
---|
178 | result = prime * result + getName().hashCode();
|
---|
179 | result = prime * result + (int) (uid ^ (uid >>> 32));
|
---|
180 | return result;
|
---|
181 | }
|
---|
182 |
|
---|
183 | @Override
|
---|
184 | public boolean equals(Object obj) {
|
---|
185 | if (! (obj instanceof User))
|
---|
186 | return false;
|
---|
187 | User other = (User) obj;
|
---|
188 | if (uid != other.uid)
|
---|
189 | return false;
|
---|
190 | return true;
|
---|
191 | }
|
---|
192 | }
|
---|