001/*
002 * (C) Copyright 2014 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Benoit Delbosc
016 */
017package org.nuxeo.ecm.platform.query.core;
018
019import org.nuxeo.ecm.platform.query.api.Bucket;
020
021import java.util.Locale;
022
023/**
024 * Immutable bucket for range.
025 *
026 * @since 6.0
027 */
028public final class BucketRange implements Bucket {
029
030    private final String key;
031
032    private final long docCount;
033
034    private final Double from;
035
036    private final Double to;
037
038    public BucketRange(String key, Number from, Number to, long docCount) {
039        if (key == null) {
040            throw new IllegalArgumentException("key is null");
041        };
042        this.key = key;
043        if (from != null) {
044            this.from = from.doubleValue();
045        } else {
046            this.from = null;
047        }
048        if (to != null) {
049            this.to = to.doubleValue();
050        } else {
051            this.to = null;
052        }
053        this.docCount = docCount;
054    }
055
056    @Override
057    public String getKey() {
058        return key;
059    }
060
061    @Override
062    public long getDocCount() {
063        return docCount;
064    }
065
066    /**
067     * @return null if there are no minimal limit
068     */
069    public Double getFrom() {
070        return from;
071    }
072
073    /**
074     * @return null if there are no max limit
075     */
076    public Double getTo() {
077        return to;
078    }
079
080    @Override
081    public String toString() {
082        return String.format(Locale.ENGLISH, "BucketRange(%s, %d, %.2f, %.2f)", key, docCount, from, to);
083    }
084
085}