package com.timgroup.eventstore.merging;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.timgroup.eventstore.api.Position;
import com.timgroup.eventstore.api.PositionCodec;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

final class MergedEventReaderPosition implements Position {
    final Position[] inputPositions;

    MergedEventReaderPosition(Position... inputPositions) {
        this.inputPositions = inputPositions;
    }

    MergedEventReaderPosition withNextPosition(int readerIndex, Position position) {
        Position[] newPositions = inputPositions.clone();
        newPositions[readerIndex] = position;
        return new MergedEventReaderPosition(newPositions);
    }

    static final class MergedEventReaderPositionCodec implements PositionCodec {
        private final ObjectMapper objectMapper = new ObjectMapper();

        private final NamedReaderWithCodec[] namedReaders;

        MergedEventReaderPositionCodec(NamedReaderWithCodec... namedReaders) {
            this.namedReaders = namedReaders;
        }

        @Override
        public String serializePosition(Position position) {
            MergedEventReaderPosition mergedPosition = (MergedEventReaderPosition)position;

            Map<String, String> positionMap = new HashMap<>();

            for (int index = 0; index < namedReaders.length; index++) {
                positionMap.put(namedReaders[index].name, namedReaders[index].codec.serializePosition(mergedPosition.inputPositions[index]));
            }

            try {
                return objectMapper.writeValueAsString(positionMap);
            } catch (JsonProcessingException e) {
                throw new IllegalArgumentException("unable to serialise position", e);
            }
        }

        @Override
        public Position deserializePosition(String serialisedPosition) {
            try {
                JsonNode positionMap = objectMapper.readTree(serialisedPosition);

                Position[] deserialisedPositions = new Position[namedReaders.length];
                for (int index = 0; index < namedReaders.length; index++) {
                    deserialisedPositions[index] = namedReaders[index].codec.deserializePosition(positionMap.get(namedReaders[index].name).asText());
                }

                return new MergedEventReaderPosition(deserialisedPositions);

            } catch (IOException e) {
                throw new IllegalArgumentException("unable to deserialise position", e);
            }
        }
    }

}

@@ -314,24 +314,49 @@
 OException;%0A
+import java.util.Arrays;%0A
 import java.
@@ -369,16 +369,16 @@
 ashMap;%0A
-
 import j
@@ -855,24 +855,136 @@
 ns);%0A    %7D%0A%0A
+    @Override%0A    public String toString() %7B%0A        return %22(%22 + Arrays.toString(inputPositions) + %22)%22;%0A    %7D%0A%0A
     static f
@@ -1390,32 +1390,32 @@
 ion position) %7B%0A
-
             Merg
@@ -1480,16 +1480,17 @@
 osition)
+ 
 position

Add toString for merged position