1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.Map;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.visitor.Visitor;
|
---|
9 |
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * A key can be associated together with a value to any osm primitive.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class Key extends OsmPrimitive {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The key's name
|
---|
20 | */
|
---|
21 | public final String name;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * All keys are stored here.
|
---|
25 | */
|
---|
26 | private static Map<String, Key> allKeys = new HashMap<String, Key>();
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Generate a key with the given name. You cannot call this directly but
|
---|
30 | * have to use the static constructor. This makes sure, you get every key
|
---|
31 | * only once.
|
---|
32 | */
|
---|
33 | private Key(String name) {
|
---|
34 | this.name = name;
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Get an instance of the key with the given name.
|
---|
39 | * @param name The name of the key to get.
|
---|
40 | * @return An shared instance of the key with the given name.
|
---|
41 | */
|
---|
42 | public static Key get(String name) {
|
---|
43 | Key key = allKeys.get(name);
|
---|
44 | if (key == null) {
|
---|
45 | key = new Key(name);
|
---|
46 | allKeys.put(name, key);
|
---|
47 | }
|
---|
48 | return key;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Return an empty list, since keys cannot have nodes.
|
---|
53 | */
|
---|
54 | @Override
|
---|
55 | public Collection<Node> getAllNodes() {
|
---|
56 | return new LinkedList<Node>();
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Keys are equal, when their name is equal, regardless of their other keys.
|
---|
61 | */
|
---|
62 | @Override
|
---|
63 | public boolean equals(Object obj) {
|
---|
64 | if (!(obj instanceof Key))
|
---|
65 | return false;
|
---|
66 | return name.equals(((Key)obj).name);
|
---|
67 | }
|
---|
68 |
|
---|
69 | @Override
|
---|
70 | public int hashCode() {
|
---|
71 | return name.hashCode();
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void visit(Visitor visitor) {
|
---|
76 | visitor.visit(this);
|
---|
77 | }
|
---|
78 | }
|
---|