/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.Immutable;
import io.spine.protobuf.Messages;
import io.spine.type.KnownMessage;
import io.spine.validate.MessageWithConstraints;

/**
 * A message for which {@link ValidatingBuilder} was generated by Spine Model Compiler.
 *
 * <p>Messages that implement this interface are <em>likely</em> produced by
 * {@link ValidatingBuilder#vBuild()} of the corresponding builder type. As such, these messages are
 * <em>likely</em> to {@linkplain #validate() conform} to the validation constraints defined in
 * the corresponding Protobuf type.
 *
 * <p>Implementing this interface does <em>NOT</em> guarantee such conformity.
 * This is so because an instance of a message can still be built by methods
 * {@link ValidatingBuilder#buildPartial()} or {@link ValidatingBuilder#build()} which do not
 * perform the check of the validation constraints.
 *
 * @param <B>
 *         the type of the builder
 * @param <M>
 *         the type of the message used for binging of the builder type
 */
@Immutable
public interface BuiltMessage<B extends ValidatingBuilder<M>, M extends BuiltMessage<B, M>>
        extends MessageWithConstraints, KnownMessage {

    /**
     * Obtains the instance of the builder for the passed message class.
     *
     * @param messageClass
     *          the class of the message to be created by the returned builder
     * @param <B>
     *          the type of the builder
     * @param <M>
     *          the type of the message
     * @return a new empty builder
     */
    @SuppressWarnings("unused")
    static <B extends ValidatingBuilder<M>, M extends BuiltMessage<B, M>>
    B builderOf(Class<? extends M> messageClass) {
        @SuppressWarnings("unchecked") // The cast is ensured by generic params of `BuildMessage`.
        B builder = (B) Messages.builderFor(messageClass);
        return builder;
    }
}

/*
 * Copyright 2021, TeamDev. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.Immutable;
import io.spine.protobuf.Messages;
import io.spine.type.KnownMessage;
import io.spine.validate.MessageWithConstraints;

/**
 * A message for which {@link ValidatingBuilder} was generated by Spine Model Compiler.
 *
 * <p>Messages that implement this interface are <em>likely</em> produced by
 * {@link ValidatingBuilder#vBuild()} of the corresponding builder type. As such, these messages are
 * <em>likely</em> to {@linkplain #validate() conform} to the validation constraints defined in
 * the corresponding Protobuf type.
 *
 * <p>Implementing this interface does <em>NOT</em> guarantee such conformity.
 * This is so because an instance of a message can still be built by methods
 * {@link ValidatingBuilder#buildPartial()} or {@link ValidatingBuilder#build()} which do not
 * perform the check of the validation constraints.
 *
 * @param <B>
 *         the type of the builder
 * @param <M>
 *         the type of the message used for binging of the builder type
 */
@Immutable
public interface BuiltMessage<B extends ValidatingBuilder<M>, M extends BuiltMessage<B, M>>
        extends MessageWithConstraints, KnownMessage {

    /**
     * Obtains the instance of the builder for the passed message class.
     *
     * @param messageClass
     *          the class of the message to be created by the returned builder
     * @param <B>
     *          the type of the builder
     * @param <M>
     *          the type of the message
     * @return a new empty builder
     */
    @SuppressWarnings("unused")
    static <B extends ValidatingBuilder<M>, M extends BuiltMessage<B, M>>
    B builderOf(Class<? extends M> messageClass) {
        @SuppressWarnings("unchecked") // The cast is ensured by generic params of `BuiltMessage`.
        B builder = (B) Messages.builderFor(messageClass);
        return builder;
    }
}

Fix spelling in comment