@@ -0,0 +1,3635 @@
+/*%0A *%0A * Copyright 2016, TeamDev Ltd. All rights reserved.%0A *%0A * Redistribution and use in source and/or binary forms, with or without%0A * modification, must retain the above copyright notice and the following%0A * disclaimer.%0A *%0A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS%0A * %22AS IS%22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT%0A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR%0A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT%0A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,%0A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT%0A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,%0A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY%0A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT%0A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE%0A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.%0A *%0A */%0Apackage org.spine3.base;%0A%0Aimport com.google.protobuf.Any;%0Aimport com.google.protobuf.FieldMask;%0Aimport com.google.protobuf.Message;%0Aimport org.spine3.client.EntityFilters;%0Aimport org.spine3.client.EntityId;%0Aimport org.spine3.client.EntityIdFilter;%0Aimport org.spine3.client.Query;%0Aimport org.spine3.client.Target;%0Aimport org.spine3.protobuf.AnyPacker;%0Aimport org.spine3.protobuf.TypeUrl;%0A%0Aimport javax.annotation.Nullable;%0Aimport java.util.Set;%0A%0A/**%0A * Client-side utilities for working with queries.%0A *%0A * @author Alex Tymchenko%0A */%0Apublic class Queries %7B%0A%0A    private Queries() %7B%0A    %7D%0A%0A    public static Query readByIds(Class%3C? extends Message%3E entityClass, Set%3CMessage%3E ids) %7B%0A        final Query result = compose(entityClass, ids, null);%0A        return result;%0A    %7D%0A%0A    public static Query readAll(Class%3C? extends Message%3E entityClass) %7B%0A        final Query result = compose(entityClass, null, null);%0A        return result;%0A    %7D%0A%0A%0A    // TODO%5Balex.tymchenko%5D: think of Optional instead. Consider Java 8 vs Guava Optional.%0A    private static Query compose(Class%3C? extends Message%3E entityClass, @Nullable Set%3CMessage%3E ids, @Nullable FieldMask fieldMask) %7B%0A        final TypeUrl type = TypeUrl.of(entityClass);%0A%0A        final boolean includeAll = ids == null;%0A%0A        final EntityIdFilter.Builder idFilterBuilder = EntityIdFilter.newBuilder();%0A%0A        if (!includeAll) %7B%0A            for (Message rawId : ids) %7B%0A                final Any packedId = AnyPacker.pack(rawId);%0A                final EntityId entityId = EntityId.newBuilder()%0A                                                  .setId(packedId)%0A                                                  .build();%0A                idFilterBuilder.addIds(entityId);%0A            %7D%0A        %7D%0A        final EntityIdFilter idFilter = idFilterBuilder.build();%0A        final EntityFilters filters = EntityFilters.newBuilder()%0A                                                   .setIdFilter(idFilter)%0A                                                   .build();%0A%0A        final Target target = Target.newBuilder()%0A                                    .setIncludeAll(includeAll)%0A                                    .setType(type.getTypeName())%0A                                    .setFilters(filters)%0A                                    .build();%0A%0A%0A        final Query.Builder queryBuilder = Query.newBuilder()%0A                                                .setTarget(target);%0A        if (fieldMask != null) %7B%0A            queryBuilder.setFieldMask(fieldMask);%0A        %7D%0A        final Query result = queryBuilder%0A                .build();%0A        return result;%0A    %7D%0A%7D%0A

Create a small utility class for working with queries.