001/*
002 * (C) Copyright 2014 Nuxeo SA (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 *     Benoit Delbosc
018 */
019package org.nuxeo.ecm.platform.query.core;
020
021import org.nuxeo.ecm.platform.query.api.Bucket;
022
023import java.util.Locale;
024
025/**
026 * Immutable bucket for range.
027 *
028 * @since 6.0
029 */
030public final class BucketRange implements Bucket {
031
032    private final String key;
033
034    private final long docCount;
035
036    private final Double from;
037
038    private final Double to;
039
040    public BucketRange(String key, Number from, Number to, long docCount) {
041        if (key == null) {
042            throw new IllegalArgumentException("key is null");
043        };
044        this.key = key;
045        if (from != null) {
046            this.from = from.doubleValue();
047        } else {
048            this.from = null;
049        }
050        if (to != null) {
051            this.to = to.doubleValue();
052        } else {
053            this.to = null;
054        }
055        this.docCount = docCount;
056    }
057
058    @Override
059    public String getKey() {
060        return key;
061    }
062
063    @Override
064    public long getDocCount() {
065        return docCount;
066    }
067
068    /**
069     * @return null if there are no minimal limit
070     */
071    public Double getFrom() {
072        return from;
073    }
074
075    /**
076     * @return null if there are no max limit
077     */
078    public Double getTo() {
079        return to;
080    }
081
082    @Override
083    public String toString() {
084        return String.format(Locale.ENGLISH, "BucketRange(%s, %d, %.2f, %.2f)", key, docCount, from, to);
085    }
086
087}