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