001/*
002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.ecm.core.scroll;
020
021import java.util.Collections;
022import java.util.List;
023import java.util.Objects;
024import java.util.function.Predicate;
025import java.util.stream.Collectors;
026
027import org.nuxeo.ecm.core.api.scroll.ScrollRequest;
028
029/**
030 * Scroll Request for a static result set.
031 *
032 * @since 11.1
033 */
034public class StaticScrollRequest implements ScrollRequest {
035
036    protected static final String SCROLL_TYPE = "static";
037
038    protected static final String SCROLL_NAME = "list";
039
040    protected final int size;
041
042    protected final List<String> identifiers;
043
044    protected StaticScrollRequest(Builder builder) {
045        this.identifiers = builder.identifiers;
046        this.size = builder.getSize();
047    }
048
049    @Override
050    public String getType() {
051        return SCROLL_TYPE;
052    }
053
054    @Override
055    public String getName() {
056        return SCROLL_NAME;
057    }
058
059    @Override
060    public int getSize() {
061        return size;
062    }
063
064    public List<String> getIdentifiers() {
065        return identifiers;
066    }
067
068    public static Builder builder(String singleIdentifier) {
069        return new Builder(singleIdentifier);
070    }
071
072    public static Builder builder(List<String> identifiers) {
073        return new Builder(identifiers);
074    }
075
076    @Override
077    public String toString() {
078        return "StaticScrollRequest{" + "size=" + size + ", identifiers=" + identifiers + '}';
079    }
080
081    public static class Builder {
082
083        public static final int DEFAULT_SCROLL_SIZE = 10;
084
085        protected final List<String> identifiers;
086
087        protected int size;
088
089        public Builder(String singleIdentifier) {
090            Objects.requireNonNull(singleIdentifier, "identifier cannot be null");
091            this.identifiers = Collections.singletonList(singleIdentifier);
092        }
093
094        public Builder(List<String> identifiers) {
095            Objects.requireNonNull(identifiers, "identifiers cannot be null");
096            List<String> ids = identifiers.stream()
097                                          .map(String::trim)
098                                          .filter(Predicate.not(String::isBlank))
099                                          .collect(Collectors.toList());
100            if (ids.isEmpty()) {
101                throw new IllegalArgumentException("identifiers cannot be empty");
102            }
103            this.identifiers = Collections.unmodifiableList(ids);
104        }
105
106        public Builder size(int size) {
107            if (size <= 0) {
108                throw new IllegalArgumentException("size must be > 0");
109            }
110            this.size = size;
111            return this;
112        }
113
114        public int getSize() {
115            return size == 0 ? DEFAULT_SCROLL_SIZE : size;
116        }
117
118        public StaticScrollRequest build() {
119            return new StaticScrollRequest(this);
120        }
121    }
122
123}