/*
 * Copyright 2018, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package io.spine.server.procman;

import io.spine.core.RejectionEnvelope;
import io.spine.server.delivery.DeliveryTag;
import io.spine.server.delivery.RejectionShardedStream;

/**
 * A strategy on delivering the rejections to the instances of a certain process manager type.
 *
 * @param <I> the ID type of process manager, to which rejections are being delivered
 * @param <P> the type of process manager
 * @author Alex Tymchenko
 */
public class PmRejectionDelivery<I, P extends ProcessManager<I, ?, ?>>
        extends PmDelivery<I, P, RejectionEnvelope,
                                           RejectionShardedStream<I>, RejectionShardedStream.Builder<I>> {

    protected PmRejectionDelivery(ProcessManagerRepository<I, P, ?> repository) {
        super(new PmRejectionConsumer<>(repository));
    }

    private static class PmRejectionConsumer<I, P extends ProcessManager<I, ?, ?>>
            extends PmMessageConsumer<I, P, RejectionEnvelope, RejectionShardedStream<I>,
            RejectionShardedStream.Builder<I>> {
        protected PmRejectionConsumer(ProcessManagerRepository<I, P, ?> repository) {
            super(DeliveryTag.forRejectionsOf(repository), repository);
        }

        @Override
        protected RejectionShardedStream.Builder<I> newShardedStreamBuilder() {
            return RejectionShardedStream.newBuilder();
        }

        @Override
        protected PmRejectionEndpoint<I, P> getEndpoint(RejectionEnvelope envelope) {
            return PmRejectionEndpoint.of(repository(), envelope);
        }
    }
}

@@ -1545,19 +1545,73 @@
 ivery%3CI,
- P,
+%0A                           P,%0A                          
  Rejecti
@@ -1622,32 +1622,16 @@
 velope,%0A
-                
         
@@ -1675,16 +1675,43 @@
 ream%3CI%3E,
+%0A                          
  Rejecti
@@ -2011,30 +2011,144 @@
 r%3CI,
- P, RejectionEnvelope,
+%0A                                      P,%0A                                      RejectionEnvelope,%0A                                     
  Rej
@@ -2163,32 +2163,58 @@
 ardedStream%3CI%3E,%0A
+                          
             Reje
@@ -2246,16 +2246,17 @@
 r%3CI%3E%3E %7B%0A
+%0A
         

Fix wrapping of the generic params.