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