forked from eXist-db/exist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eXist-db#4621 from line-o/fix/map-merge-2
allow all options in map:merge#2
- Loading branch information
Showing
5 changed files
with
191 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,13 +43,15 @@ | |
|
||
import javax.annotation.Nullable; | ||
import java.util.Iterator; | ||
import java.util.Optional; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.ToLongFunction; | ||
|
||
/** | ||
* Full implementation of the XDM map() type based on an | ||
* immutable hash-map. | ||
* | ||
* @author <a href="mailto:[email protected]">Adam Rettter</a> | ||
* @author <a href="mailto:[email protected]">Adam Retter</a> | ||
*/ | ||
public class MapType extends AbstractMapType { | ||
|
||
|
@@ -208,6 +210,43 @@ public AbstractMapType merge(final Iterable<AbstractMapType> others) { | |
return new MapType(getExpression(), context, newMap.forked(), prevType); | ||
} | ||
|
||
@Override | ||
public AbstractMapType merge(final Iterable<AbstractMapType> others, final BinaryOperator<Sequence> mergeFn) { | ||
|
||
// create a transient map | ||
IMap<AtomicValue, Sequence> newMap = map.linear(); | ||
|
||
int prevType = keyType; | ||
for (final AbstractMapType other: others) { | ||
if (other instanceof MapType) { | ||
// MapType - optimise merge | ||
final MapType otherMap = (MapType) other; | ||
newMap = newMap.merge(otherMap.map, mergeFn); | ||
|
||
if (prevType != otherMap.keyType) { | ||
prevType = MIXED_KEY_TYPES; | ||
} | ||
} else { | ||
// non MapType | ||
for (final IEntry<AtomicValue, Sequence> entry : other) { | ||
final AtomicValue key = entry.key(); | ||
final Optional<Sequence> headEntry = newMap.get(key); | ||
if (headEntry.isPresent()) { | ||
newMap = newMap.put(key, mergeFn.apply(headEntry.get(), entry.value())); | ||
} else { | ||
newMap = newMap.put(key, entry.value()); | ||
} | ||
if (prevType != key.getType()) { | ||
prevType = MIXED_KEY_TYPES; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// return an immutable map | ||
return new MapType(context, newMap.forked(), prevType); | ||
} | ||
|
||
public void add(final AtomicValue key, final Sequence value) { | ||
setKeyType(key.getType()); | ||
map = map.put(key, value); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters