1 | // Copyright 2015 Georg-August-Universität Göttingen, Germany |
---|
2 | // |
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | // you may not use this file except in compliance with the License. |
---|
5 | // You may obtain a copy of the License at |
---|
6 | // |
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | // |
---|
9 | // Unless required by applicable law or agreed to in writing, software |
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | // See the License for the specific language governing permissions and |
---|
13 | // limitations under the License. |
---|
14 | |
---|
15 | package de.ugoe.cs.autoquest.plugin.genericevents.eventCore; |
---|
16 | |
---|
17 | import java.util.HashMap; |
---|
18 | import java.util.Map; |
---|
19 | |
---|
20 | import de.ugoe.cs.autoquest.eventcore.IEventTargetSpec; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * TODO comment |
---|
25 | * </p> |
---|
26 | * |
---|
27 | * @author Patrick Harms |
---|
28 | */ |
---|
29 | public class GenericEventTargetSpec implements IEventTargetSpec { |
---|
30 | |
---|
31 | /** */ |
---|
32 | private static final long serialVersionUID = 1L; |
---|
33 | |
---|
34 | /** */ |
---|
35 | private Map<String, String> targetParameters; |
---|
36 | |
---|
37 | /** */ |
---|
38 | private String targetId; |
---|
39 | |
---|
40 | /** */ |
---|
41 | private String name; |
---|
42 | |
---|
43 | /** |
---|
44 | * |
---|
45 | */ |
---|
46 | public GenericEventTargetSpec(String id, Map<String, String> parameters) { |
---|
47 | this.targetId = id; |
---|
48 | this.targetParameters = parameters; |
---|
49 | |
---|
50 | name = parameters.get("name"); |
---|
51 | |
---|
52 | if (name == null) { |
---|
53 | name = targetId; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | /* (non-Javadoc) |
---|
58 | * @see IEventTargetSpec#getSimilarity(IEventTargetSpec) |
---|
59 | */ |
---|
60 | @Override |
---|
61 | public boolean getSimilarity(IEventTargetSpec other) { |
---|
62 | if (this == other) { |
---|
63 | return true; |
---|
64 | } |
---|
65 | else if (other instanceof GenericEventTargetSpec) { |
---|
66 | Map<String, String> otherParameters = ((GenericEventTargetSpec) other).targetParameters; |
---|
67 | |
---|
68 | if (targetParameters == null) { |
---|
69 | return otherParameters == null; |
---|
70 | } |
---|
71 | else if (otherParameters == null) { |
---|
72 | return false; |
---|
73 | } |
---|
74 | else if (targetParameters.size() != otherParameters.size()) { |
---|
75 | return false; |
---|
76 | } |
---|
77 | else { |
---|
78 | for (Map.Entry<String, String> entry : targetParameters.entrySet()) { |
---|
79 | String otherValue = otherParameters.get(entry.getKey()); |
---|
80 | if ((otherValue != entry.getValue()) && |
---|
81 | ((otherValue == null) || (!otherValue.equals(entry.getValue())))) |
---|
82 | { |
---|
83 | return false; |
---|
84 | } |
---|
85 | } |
---|
86 | return true; |
---|
87 | } |
---|
88 | |
---|
89 | } |
---|
90 | else { |
---|
91 | return false; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * |
---|
97 | * |
---|
98 | * @return |
---|
99 | */ |
---|
100 | public String getId() { |
---|
101 | return targetId; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * |
---|
106 | */ |
---|
107 | public void mergeWith(GenericEventTargetSpec furtherSpec) { |
---|
108 | if (furtherSpec.targetParameters != null) { |
---|
109 | if (targetParameters == null) { |
---|
110 | targetParameters = new HashMap<>(furtherSpec.targetParameters); |
---|
111 | } |
---|
112 | else { |
---|
113 | targetParameters.putAll(furtherSpec.targetParameters); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | /* (non-Javadoc) |
---|
119 | * @see java.lang.Object#toString() |
---|
120 | */ |
---|
121 | @Override |
---|
122 | public String toString() { |
---|
123 | return name; |
---|
124 | } |
---|
125 | |
---|
126 | } |
---|