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