24
24
import org .apache .commons .lang3 .tuple .Pair ;
25
25
import org .hl7 .fhir .instance .model .api .IIdType ;
26
26
27
+ import java .util .Collection ;
27
28
import java .util .HashMap ;
28
29
import java .util .List ;
29
30
import java .util .Map ;
30
- import java .util .Objects ;
31
31
import java .util .stream .Collectors ;
32
32
33
33
public class IdSubstitutionMap {
@@ -80,8 +80,10 @@ public List<Pair<IIdType, IIdType>> entrySet() {
80
80
}
81
81
82
82
public void put (IIdType theSource , IIdType theTarget ) {
83
- myMap .put (new Entry (theSource ), new Entry (theTarget ));
84
- myReverseMap .put (new Entry (theTarget ), new Entry (theSource ));
83
+ Entry sourceEntry = new Entry (theSource );
84
+ Entry targetEntry = new Entry (theTarget );
85
+ myMap .put (sourceEntry , targetEntry );
86
+ myReverseMap .put (targetEntry , sourceEntry );
85
87
}
86
88
87
89
public boolean isEmpty () {
@@ -93,15 +95,16 @@ public boolean isEmpty() {
93
95
* the same ResourceType and IdPart as the target id.
94
96
*/
95
97
public void updateTargets (IIdType theNewId ) {
96
- if (theNewId == null ) {
98
+ if (theNewId == null || theNewId . getValue () == null ) {
97
99
return ;
98
100
}
99
- String newUnqualifiedVersionLessId = theNewId .toUnqualifiedVersionless ().getValue ();
100
- entrySet ().stream ()
101
- .map (Pair ::getValue )
102
- .filter (targetId ->
103
- Objects .equals (targetId .toUnqualifiedVersionless ().getValue (), newUnqualifiedVersionLessId ))
104
- .forEach (targetId -> targetId .setValue (theNewId .getValue ()));
101
+
102
+ Entry newEntry = new Entry (theNewId );
103
+ Collection <Entry > targets = myReverseMap .removeAll (newEntry );
104
+ for (Entry nextTarget : targets ) {
105
+ myMap .put (nextTarget , newEntry );
106
+ }
107
+ myReverseMap .putAll (newEntry , targets );
105
108
}
106
109
107
110
private static class Entry {
@@ -135,6 +138,11 @@ public boolean equals(Object theOther) {
135
138
public int hashCode () {
136
139
return myUnversionedId .hashCode ();
137
140
}
141
+
142
+ @ Override
143
+ public String toString () {
144
+ return "Entry[" + "myUnversionedId='" + myUnversionedId + "', myId=" + myId + ']' ;
145
+ }
138
146
}
139
147
140
148
static String toVersionlessValue (IIdType theId ) {
0 commit comments