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.List;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * A simple class to keep a list of user names.
|
---|
10 | *
|
---|
11 | * Instead of storing user names as strings with every OSM primitive, we store
|
---|
12 | * a reference to an user object, and make sure that for each username there
|
---|
13 | * is only one user object.
|
---|
14 | *
|
---|
15 | *
|
---|
16 | */
|
---|
17 | public class User {
|
---|
18 | static private long uidCounter = 0;
|
---|
19 | /**
|
---|
20 | * the map of known users
|
---|
21 | */
|
---|
22 | private static HashMap<Long,User> userMap = new HashMap<Long,User>();
|
---|
23 |
|
---|
24 |
|
---|
25 | private static long getNextLocalUid() {
|
---|
26 | synchronized(User.class) {
|
---|
27 | uidCounter--;
|
---|
28 | return uidCounter;
|
---|
29 | }
|
---|
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 = new User(uid, name);
|
---|
51 | userMap.put(user.getId(), user);
|
---|
52 | return user;
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Returns the user with user id <code>uid</code> or null if this user doesn't exist
|
---|
58 | *
|
---|
59 | * @param uid the user id
|
---|
60 | * @return the user; null, if there is no user with this id
|
---|
61 | */
|
---|
62 | public static User getById(long uid) {
|
---|
63 | return userMap.get(uid);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Returns the list of users with name <code>name</code> or the empty list if
|
---|
68 | * no such users exist
|
---|
69 | *
|
---|
70 | * @param name the user name
|
---|
71 | * @return the list of users with name <code>name</code> or the empty list if
|
---|
72 | * no such users exist
|
---|
73 | */
|
---|
74 | public static List<User> getByName(String name) {
|
---|
75 | name = name == null ? "" : name;
|
---|
76 | List<User> ret = new ArrayList<User>();
|
---|
77 | for (User user: userMap.values()) {
|
---|
78 | if (user.getName().equals(name)) {
|
---|
79 | ret.add(user);
|
---|
80 | }
|
---|
81 | }
|
---|
82 | return ret;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /** the user name */
|
---|
86 | private String name;
|
---|
87 | /** the user id */
|
---|
88 | private long uid;
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Replies the user name
|
---|
92 | *
|
---|
93 | * @return the user name. Never null, but may be the empty string
|
---|
94 | */
|
---|
95 | public String getName() {
|
---|
96 | return name == null ? "" : name;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Replies the user id. If this user is known to the OSM server the positive user id
|
---|
101 | * from the server is replied. Otherwise, a negative local value is replied.
|
---|
102 | *
|
---|
103 | * A negative local is only unique during an editing session. It is lost when the
|
---|
104 | * application is closed and there is no guarantee that a negative local user id is
|
---|
105 | * always bound to a user with the same name.
|
---|
106 | *
|
---|
107 | */
|
---|
108 | public long getId() {
|
---|
109 | return uid;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** private constructor, only called from get method. */
|
---|
113 | private User(long uid, String name) {
|
---|
114 | this.uid = uid;
|
---|
115 | this.name = name;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public boolean isOsmUser() {
|
---|
119 | return uid > 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | public boolean isLocalUser() {
|
---|
123 | return uid < 0;
|
---|
124 | }
|
---|
125 |
|
---|
126 | @Override
|
---|
127 | public int hashCode() {
|
---|
128 | final int prime = 31;
|
---|
129 | int result = 1;
|
---|
130 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
131 | result = prime * result + (int) (uid ^ (uid >>> 32));
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public boolean equals(Object obj) {
|
---|
137 | if (this == obj)
|
---|
138 | return true;
|
---|
139 | if (obj == null)
|
---|
140 | return false;
|
---|
141 | if (getClass() != obj.getClass())
|
---|
142 | return false;
|
---|
143 | User other = (User) obj;
|
---|
144 | if (name == null) {
|
---|
145 | if (other.name != null)
|
---|
146 | return false;
|
---|
147 | } else if (!name.equals(other.name))
|
---|
148 | return false;
|
---|
149 | if (uid != other.uid)
|
---|
150 | return false;
|
---|
151 | return true;
|
---|
152 | }
|
---|
153 | }
|
---|