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.joda.time.DateTime;
020import org.nuxeo.ecm.platform.query.api.Bucket;
021
022/**
023 * Immutable bucket for date range.
024 *
025 * @since 6.0
026 */
027public class BucketRangeDate implements Bucket {
028
029    private final BucketRange range;
030
031    // joda DateTime are immutables
032    private final DateTime fromDate;
033
034    private final DateTime toDate;
035
036    public BucketRangeDate(String key, DateTime from, DateTime to, long docCount) {
037        if (key == null) {
038            throw new IllegalArgumentException("key is null");
039        };
040        // fromDate.
041        range = new BucketRange(key, from != null ? from.getMillis() : null, to != null ? to.getMillis() : null,
042                docCount);
043        fromDate = from;
044        toDate = to;
045    }
046
047    @Override
048    public String getKey() {
049        return range.getKey();
050    }
051
052    @Override
053    public long getDocCount() {
054        return range.getDocCount();
055    }
056
057    public Double getFrom() {
058        return range.getFrom();
059    }
060
061    /**
062     * @return null if there are no minimal limit
063     */
064    public DateTime getFromAsDate() {
065        return fromDate;
066    }
067
068    public Double getTo() {
069        return range.getTo();
070    }
071
072    /**
073     * @return null if there are no maximal limit
074     */
075    public DateTime getToAsDate() {
076        return toDate;
077    }
078
079    @Override
080    public String toString() {
081        return String.format("BucketRangeDate(%s, %d, %s, %s)", getKey(), getDocCount(), fromDate, toDate);
082    }
083
084}