/*
 * Copyright 2018, TeamDev. 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.base;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.protobuf.Any;
import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.Message;
import com.google.protobuf.Timestamp;
import io.spine.annotation.Internal;
import io.spine.string.Stringifiers;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

import java.util.Optional;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.spine.base.Time.getCurrentTime;
import static io.spine.util.Exceptions.newIllegalStateException;

/**
 * A {@code Throwable}, which state is a {@link Message}.
 *
 * <p>Typically used to signalize about a command rejection, occurred in a system. In which case
 * the {@code message} thrown is a detailed description of the rejection reason.
 *
 * @author Alex Tymchenko
 * @author Alexander Yevsyukov
 */
public abstract class ThrowableMessage extends Throwable {

    private static final long serialVersionUID = 0L;

    /**
     * We accept GeneratedMessage (instead of Message) because generated messages
     * implement {@code Serializable}.
     */
    private final GeneratedMessageV3 message;

    /** The moment of creation of this object. */
    private final Timestamp timestamp;

    /** Optional ID of the entity which thrown the message. */
    private @MonotonicNonNull Any producerId;

    protected ThrowableMessage(GeneratedMessageV3 message) {
        super();
        this.message = checkNotNull(message);
        this.timestamp = getCurrentTime();
    }

    public Message getMessageThrown() {
        return message;
    }

    /**
     * Returns timestamp of the rejection message creation.
     */
    public Timestamp getTimestamp() {
        return timestamp;
    }

    /**
     * Initializes the ID of the entity, which thrown the message.
     *
     * <p>This internal API method can be called only once. It is supposed to be used by
     * the framework, and must not be called by the user's code.
     *
     * @param  producerId the ID of the entity packed into {@code Any}
     * @return a reference to this {@code ThrowableMessage} instance
     */
    @Internal
    @CanIgnoreReturnValue
    public synchronized ThrowableMessage initProducer(Any producerId) {
        checkNotNull(producerId);
        if (this.producerId != null) {
            Object unpackedId = Identifier.unpack(producerId);
            String stringId = Stringifiers.toString(unpackedId);
            throw newIllegalStateException("Producer already initialized: %s", stringId);
        }
        this.producerId = producerId;
        return this;
    }

    /**
     * Obtains ID of the entity which thrown the message.
     */
    public synchronized Optional<Any> producerId() {
        return Optional.ofNullable(producerId);
    }
}

@@ -1128,55 +1128,8 @@
 ny;%0A
-import com.google.protobuf.GeneratedMessageV3;%0A
 impo
@@ -1978,177 +1978,38 @@
     
-/**%0A     * We accept GeneratedMessage (instead of Message) because generated messages%0A     * implement %7B@code Serializable%7D.%0A     */%0A    private final Generated
+private final Rejection
 Message
-V3
  mes
@@ -2250,26 +2250,24 @@
 age(
-Generated
+Rejection
 Message
-V3
  mes
@@ -2383,24 +2383,24 @@
 e();%0A    %7D%0A%0A
-
     public M
@@ -2398,16 +2398,25 @@
  public 
+Rejection
 Message 

Make ThrowableMessage work with RejectionMessage instead of GeneratedMessage3.