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.io.Serializable;
022import java.util.Collections;
023import java.util.Map;
024import java.util.Objects;
025
026import org.apache.commons.lang3.builder.ToStringBuilder;
027import org.nuxeo.ecm.core.api.scroll.ScrollRequest;
028
029/**
030 * Generic Scroll Request.
031 *
032 * @since 11.1
033 */
034public class GenericScrollRequest implements ScrollRequest {
035
036    public static final String SCROLL_TYPE = "generic";
037
038    protected final int size;
039
040    protected final String query;
041
042    protected final String scrollName;
043
044    protected final Map<String, Serializable> options;
045
046    protected GenericScrollRequest(Builder builder) {
047        this.query = builder.query;
048        this.scrollName = builder.scrollerName;
049        this.size = builder.getSize();
050        this.options = builder.getOptions();
051    }
052
053    @Override
054    public String getType() {
055        return SCROLL_TYPE;
056    }
057
058    @Override
059    public String getName() {
060        return scrollName;
061    }
062
063    @Override
064    public int getSize() {
065        return size;
066    }
067
068    public String getQuery() {
069        return query;
070    }
071
072    public Map<String, Serializable> getOptions() {
073        return options;
074    }
075
076    public static Builder builder(String scrollName, String query) {
077        return new Builder(scrollName, query);
078    }
079
080    @Override
081    public String toString() {
082        return ToStringBuilder.reflectionToString(this);
083    }
084
085    public static class Builder {
086
087        public static final int DEFAULT_SCROLL_SIZE = 10;
088
089        protected final String query;
090
091        protected final String scrollerName;
092
093        protected int size;
094
095        protected Map<String, Serializable> options;
096
097        public Builder(String scrollerName, String query) {
098            Objects.requireNonNull(scrollerName, "scrollerName cannot be null");
099            Objects.requireNonNull(query, "query cannot be null");
100            this.scrollerName = scrollerName;
101            this.query = query;
102        }
103
104        public Builder size(int size) {
105            if (size <= 0) {
106                throw new IllegalArgumentException("size must be > 0");
107            }
108            this.size = size;
109            return this;
110        }
111
112        public int getSize() {
113            return size == 0 ? DEFAULT_SCROLL_SIZE : size;
114        }
115
116        public Builder options(Map<String, Serializable> options) {
117            if (options != null && !options.isEmpty()) {
118                if (options.containsKey(null)) {
119                    throw new IllegalArgumentException("option key cannot be null");
120                }
121                this.options = options;
122            }
123            return this;
124        }
125
126        public Map<String, Serializable> getOptions() {
127            return options == null ? Collections.emptyMap() : options;
128        }
129
130        public GenericScrollRequest build() {
131            return new GenericScrollRequest(this);
132        }
133    }
134
135}