All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rbmhtechnology.vind.api.query.facet.Interval Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
package com.rbmhtechnology.vind.api.query.facet;

import com.rbmhtechnology.vind.api.query.datemath.DateMathExpression;

import java.time.ZonedDateTime;
import java.util.Date;

/**
 * @author Thomas Kurz ([email protected])
 * @since 26.07.16.
 */
public class Interval {

    protected String name;

    protected boolean includeStart = true;
    protected boolean includeEnd = true;

    protected T start;
    protected T end;

    public T getStart() {
        return start;
    }

    public T getEnd() {
        return end;
    }


    public boolean includesStart() {
        return includeStart;
    }

    public boolean includesEnd() {
        return includeEnd;
    }

    public String getName() {
        return name;
    }

    public static  NumericInterval numericInterval(String name, T start, T end) {
        return new NumericInterval<>(name, start, end);
    }

    public static  NumericInterval numericInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
        return new NumericInterval<>(name, start, end, includeStart, includeEnd);
    }

    public static  UtilDateInterval dateInterval(String name, T start, T end) {
        return new UtilDateInterval<>(name, start, end);
    }

    public static  UtilDateInterval dateInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
        return new UtilDateInterval<>(name, start, end, includeStart, includeEnd);
    }

    public static  ZonedDateTimeInterval dateInterval(String name, T start, T end) {
        return new ZonedDateTimeInterval<>(name, start, end);
    }

    public static  ZonedDateTimeInterval dateInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
        return new ZonedDateTimeInterval<>(name, start, end, includeStart, includeEnd);
    }

    public static  DateMathInterval dateInterval(String name, T start, T end) {
        return new DateMathInterval<>(name, start, end);
    }

    public static  DateMathInterval dateInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
        return new DateMathInterval<>(name, start, end, includeStart, includeEnd);
    }

    @Override
    public String toString(){
        final String serializeFacet = "" +
                "\"%s\":{" +
                "\"type\":\"%s\","+
                "\"start\":\"%s\","+
                "\"includeStart\":%s,"+
                "\"end\":\"%s\","+
                "\"includeEnd\":%s"+
                "}";
        return String.format(serializeFacet,
                this.name,
                this.getClass().getSimpleName(),
                this.start,
                this.includeStart,
                this.end,
                this.includeEnd
        );
    }

    public static class NumericInterval extends Interval {
        protected NumericInterval(String name, T start, T end) {
            this.name = name;
            this.start = start;
            this.end = end;
        }
        protected NumericInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
            this.name = name;
            this.start = start;
            this.end = end;
            this.includeStart = includeStart;
            this.includeEnd = includeEnd;
        }
    }

    public static class UtilDateInterval extends Interval {
        public UtilDateInterval(String name, T start, T end) {
            this.name = name;
            this.start = start;
            this.end = end;
        }
        public UtilDateInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
            this.name = name;
            this.start = start;
            this.end = end;
            this.includeStart = includeStart;
            this.includeEnd = includeEnd;
        }

        public long getTimeStampStart() {
            return ((Date)this.getStart()).toInstant().getEpochSecond();
        }

        public long getTimeStampEnd() {
            return ((Date)getEnd()).toInstant().getEpochSecond();
        }
    }

    public static class ZonedDateTimeInterval extends Interval {
        public ZonedDateTimeInterval(String name, T start, T end) {
            this.name = name;
            this.start = start;
            this.end = end;
        }
        public ZonedDateTimeInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {
            this.name = name;
            this.start = start;
            this.end = end;
            this.includeStart = includeStart;
            this.includeEnd = includeEnd;
        }

        public long getTimeStampStart() {
            return ((ZonedDateTime)this.getStart()).toInstant().getEpochSecond();
        }

        public long getTimeStampEnd() {
            return ((ZonedDateTime)getEnd()).toInstant().getEpochSecond();
        }
    }

    public static class DateMathInterval extends Interval {
        public DateMathInterval(String name, T start, T end) {
            this.name = name;
            this.start = start;
            this.end = end;
        }

        public DateMathInterval(String name, T start, T end, boolean includeStart, boolean includeEnd) {

            this.name = name;
            this.start = start;
            this.end = end;
            this.includeStart = includeStart;
            this.includeEnd = includeEnd;
        }

        public long getTimeStampStart() {
            return this.getStart().getTimeStamp();
        }

        public long getTimeStampEnd() {
            return getEnd().getTimeStamp();
        }

        public static class UtilDateDateMathInterval extends DateMathInterval {
            public UtilDateDateMathInterval(String name, DateMathExpression start, DateMathExpression end) {
                super(name,start,end);
            }

            public UtilDateDateMathInterval(String name, DateMathExpression start, DateMathExpression end, boolean includeStart, boolean includeEnd) {
                super(name,start,end,includeStart,includeEnd);
            }
        }

        public static class ZoneDateTimeDateMathInterval  extends DateMathInterval {
            public ZoneDateTimeDateMathInterval(String name, DateMathExpression start, DateMathExpression end) {
                super(name,start,end);
            }

            public ZoneDateTimeDateMathInterval(String name, DateMathExpression start, DateMathExpression end, boolean includeStart, boolean includeEnd) {
                super(name,start,end,includeStart,includeEnd);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy