/*
 * Copyright 2019, 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.client;

import com.google.protobuf.Any;
import com.google.protobuf.Message;
import io.spine.annotation.Internal;
import io.spine.base.Identifier;
import io.spine.type.TypeUrl;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newLinkedList;
import static io.spine.base.Identifier.checkSupported;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

/**
 * Client-side utilities for working with {@link Query} and
 * {@link Topic Topic} targets.
 */
@Internal
public final class Targets {

    /** Prevent instantiation of this utility class. */
    private Targets() {
    }

    /**
     * Create a {@link Target} for a subset of the entity states by specifying their IDs.
     *
     * @param entityClass
     *         the class of a target entity
     * @param ids
     *         the IDs of interest of type {@link io.spine.base.Identifier#checkSupported(Class)
     *         which is supported as identifier}
     * @return the instance of {@code Target} assembled according to the parameters.
     * @throws IllegalArgumentException
     *         if any of IDs have invalid type or are {@code null}
     */
    public static Target someOf(Class<? extends Message> entityClass,
                                Set<?> ids) {
        checkNotNull(entityClass);
        checkNotNull(ids);

        Target result = composeTarget(entityClass, ids, null);
        return result;
    }

    /**
     * Create a {@link Target} for all of the specified entity states.
     *
     * @param entityClass
     *         the class of a target entity
     * @return the instance of {@code Target} assembled according to the parameters.
     */
    public static Target allOf(Class<? extends Message> entityClass) {
        checkNotNull(entityClass);

        Target result = composeTarget(entityClass, null, null);
        return result;
    }

    /**
     * Composes a target for entities matching declared predicates.
     *
     * @param entityClass
     *         the class of a target entity
     * @param ids
     *         the IDs of interest of type {@link io.spine.base.Identifier#checkSupported(Class)
     *         which is supported as identifier}
     * @param filters
     *         a set of entity column predicates each target entity must match
     * @return a {@code Target} instance formed according to the provided parameters
     */
    public static Target composeTarget(Class<? extends Message> entityClass,
                                       @Nullable Iterable<?> ids,
                                       @Nullable Iterable<CompositeFilter> filters) {

        boolean includeAll = (ids == null && filters == null);

        TypeUrl typeUrl = TypeUrl.of(entityClass);
        TargetVBuilder builder = TargetVBuilder.newBuilder()
                                               .setType(typeUrl.value());
        if (includeAll) {
            builder.setIncludeAll(true);
        } else {
            List<?> idsList = notNullList(ids);
            IdFilter idFilter = composeIdFilter(idsList);

            List<CompositeFilter> filterList = notNullList(filters);
            Filters targetFilters = filters(filterList, idFilter);
            builder.setFilters(targetFilters);
        }

        return builder.build();
    }

    private static IdFilter composeIdFilter(Collection<?> items) {
        List<Any> ids = items.stream()
                             .distinct()
                             .map(Targets::checkId)
                             .map(Identifier::pack)
                             .collect(toList());
        IdFilter filter = idFilter(ids);
        return filter;
    }

    private static IdFilter idFilter(List<Any> ids) {
        return IdFilterVBuilder.newBuilder()
                               .addAllIds(ids)
                               .build();
    }

    /**
     * Checks that object is not {@code null} and its type
     * {@link io.spine.base.Identifier#checkSupported(Class)
     * is supported as an identifier}.
     *
     * @param item
     *         an object to check
     * @param <I>
     *         a type of an object to check
     * @return the passed object
     */
    private static <I> I checkId(I item) {
        checkNotNull(item);
        checkSupported(item.getClass());
        return item;
    }

    private static Filters filters(List<CompositeFilter> entityColumnValues, IdFilter idFilter) {
        return FiltersVBuilder.newBuilder()
                              .setIdFilter(idFilter)
                              .addAllFilter(entityColumnValues)
                              .build();
    }

    /**
     * Returns an empty list in case of {@code null} input.
     *
     * @return a new {@link List} instance
     */
    private static <T> List<T> notNullList(@Nullable Iterable<T> input) {
        if (input == null) {
            return emptyList();
        }
        return newLinkedList(input);
    }
}

@@ -3841,16 +3841,51 @@
 lters) %7B
+%0A        checkNotNull(entityClass);
 %0A%0A      

Check `Targets.composeTarget()` arguments for null