org.elasticsearch.common.rounding.DateTimeUnit Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.common.rounding;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.joda.Joda;
import org.joda.time.DateTimeField;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.ISOChronology;
import java.util.function.Function;
public enum DateTimeUnit {
WEEK_OF_WEEKYEAR((byte) 1, tz -> ISOChronology.getInstance(tz).weekOfWeekyear()),
YEAR_OF_CENTURY((byte) 2, tz -> ISOChronology.getInstance(tz).yearOfCentury()),
QUARTER((byte) 3, tz -> Joda.QuarterOfYear.getField(ISOChronology.getInstance(tz))),
MONTH_OF_YEAR((byte) 4, tz -> ISOChronology.getInstance(tz).monthOfYear()),
DAY_OF_MONTH((byte) 5, tz -> ISOChronology.getInstance(tz).dayOfMonth()),
HOUR_OF_DAY((byte) 6, tz -> ISOChronology.getInstance(tz).hourOfDay()),
MINUTES_OF_HOUR((byte) 7, tz -> ISOChronology.getInstance(tz).minuteOfHour()),
SECOND_OF_MINUTE((byte) 8, tz -> ISOChronology.getInstance(tz).secondOfMinute());
private final byte id;
private final Function fieldFunction;
DateTimeUnit(byte id, Function fieldFunction) {
this.id = id;
this.fieldFunction = fieldFunction;
}
public byte id() {
return id;
}
/**
* @return the {@link DateTimeField} for the provided {@link DateTimeZone} for this time unit
*/
public DateTimeField field(DateTimeZone tz) {
return fieldFunction.apply(tz);
}
public static DateTimeUnit resolve(byte id) {
switch (id) {
case 1:
return WEEK_OF_WEEKYEAR;
case 2:
return YEAR_OF_CENTURY;
case 3:
return QUARTER;
case 4:
return MONTH_OF_YEAR;
case 5:
return DAY_OF_MONTH;
case 6:
return HOUR_OF_DAY;
case 7:
return MINUTES_OF_HOUR;
case 8:
return SECOND_OF_MINUTE;
default:
throw new ElasticsearchException("Unknown date time unit id [" + id + "]");
}
}
}