/* * Copyright 2000-2018 JetBrains s.r.o. * * 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 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package jetbrains.buildServer.server.rest.data; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.stream.Stream; import jetbrains.buildServer.server.rest.errors.NotFoundException; import jetbrains.buildServer.util.ItemProcessor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * @author Yegor.Yarko * Date: 06/06/2016 */ public interface FinderDataBinding { @Nullable Long getDefaultPageItemsCount(); @Nullable Long getDefaultLookupLimit(); @NotNull String[] getKnownDimensions(); @NotNull String[] getHiddenDimensions(); @Nullable Locator.DescriptionProvider getLocatorDescriptionProvider(); /** * @return the item found or null if this is not single item locator * @throws NotFoundException when the locator is for single item, but the item does not exist / is not accessible for the current user */ @Nullable ITEM findSingleItem(@NotNull final Locator locator); @NotNull LocatorDataBinding getLocatorDataBinding(@NotNull final Locator locator); interface LocatorDataBinding { @NotNull ItemHolder getPrefilteredItems(); /** * Returns filter based on passed locator * Should not have side-effects other than marking used locator dimensions * * @param locator can be empty locator. Can */ @NotNull ItemFilter getFilter(); } @NotNull String getItemLocator(@NotNull final ITEM item); /** * Returns new empty set which ensures proper items matching * Is used for "unique" dimension processing * @return null if "unique" dimension is not supported */ @Nullable Set createContainerSet(); interface ItemHolder

{ void process(@NotNull final ItemProcessor

processor); } @NotNull static

ItemHolder

getItemHolder(@NotNull Stream items) { return processor -> items.forEach(item -> processor.processItem(item)); } @NotNull static

ItemHolder

getItemHolder(@NotNull Iterable items) { return new CollectionItemHolder

(items); } static class CollectionItemHolder

implements ItemHolder

{ @NotNull final private Iterable myEntries; public CollectionItemHolder(@NotNull final Iterable entries) { myEntries = entries; } public void process(@NotNull final ItemProcessor

processor) { for (P entry : myEntries) { processor.processItem(entry); } } } static class AggregatingItemHolder

implements ItemHolder

{ @NotNull final private List> myItemHolders = new ArrayList<>(); public void add(ItemHolder

holder) { myItemHolders.add(holder); } public void process(@NotNull final ItemProcessor

processor) { for (ItemHolder

itemHolder : myItemHolders) { itemHolder.process(processor); } } } /** * Works only for P with due hash/equals * * @param

*/ static class DeduplicatingItemHolder

implements ItemHolder

{ @NotNull private final ItemHolder

myItemHolder; private @NotNull Set

myProcessed; public DeduplicatingItemHolder(@NotNull final ItemHolder

itemHolder, @NotNull final Set

processed) { myItemHolder = itemHolder; myProcessed = processed; } public void process(@NotNull final ItemProcessor

processor) { myItemHolder.process(new ItemProcessor

() { @Override public boolean processItem(final P item) { if (myProcessed.add(item)) return processor.processItem(item); return true; } }); } } }