net.time4j.TimeOperator Maven / Gradle / Ivy
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2014 Meno Hochschild,
* -----------------------------------------------------------------------
* This file (TimeOperator.java) is part of project Time4J.
*
* Time4J is free software: You can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* Time4J is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Time4J. If not, see .
* -----------------------------------------------------------------------
*/
package net.time4j;
import net.time4j.engine.AdvancedElement;
import net.time4j.engine.ChronoEntity;
import net.time4j.engine.ChronoOperator;
/**
* Definiert eine Manipulation von Uhrzeitobjekten nach
* dem Strategy-Entwurfsmuster.
*
* @author Meno Hochschild
* @concurrency
*/
final class TimeOperator
extends ElementOperator {
//~ Instanzvariablen --------------------------------------------------
private final ChronoOperator opCache;
private final ChronoOperator tsCache;
private final ChronoOperator moCache;
//~ Konstruktoren -----------------------------------------------------
/**
* Konstruiert eine neue Instanz.
*
* @param element element an operator will be applied on
* @param type type of operator
*/
TimeOperator(
AdvancedElement> element,
int type
) {
this(element, type, null);
}
/**
* Konstruiert eine neue Instanz.
*
* @param element element an operator will be applied on
* @param type operator type
* @param value lenient value of element
*/
TimeOperator(
final AdvancedElement> element,
final int type,
final Object value // optional
) {
super(element, type);
switch (type) {
case OP_NEW_VALUE:
this.opCache = newValue(element, value);
this.tsCache = newValueTS(element, value);
break;
case OP_MINIMIZE:
this.opCache = element.minimized(PlainTime.class);
this.tsCache = element.minimized(PlainTimestamp.class);
break;
case OP_MAXIMIZE:
this.opCache = element.maximized(PlainTime.class);
this.tsCache = element.maximized(PlainTimestamp.class);
break;
case OP_DECREMENT:
this.opCache = element.decremented(PlainTime.class);
this.tsCache = element.decremented(PlainTimestamp.class);
break;
case OP_INCREMENT:
this.opCache = element.incremented(PlainTime.class);
this.tsCache = element.incremented(PlainTimestamp.class);
break;
case OP_FLOOR:
this.opCache = child(element, false, PlainTime.class);
this.tsCache = child(element, false, PlainTimestamp.class);
break;
case OP_CEILING:
this.opCache = child(element, true, PlainTime.class);
this.tsCache = child(element, true, PlainTimestamp.class);
break;
case OP_LENIENT:
this.opCache = lenient(element, value);
this.tsCache = lenientTS(element, value);
break;
default:
throw new AssertionError("Unknown: " + this.getType());
}
this.moCache = new Moment.Operator(this.tsCache, element, type);
}
//~ Methoden ----------------------------------------------------------
@Override
public PlainTime apply(PlainTime entity) {
return this.opCache.apply(entity);
}
@Override
public ChronoOperator inStdTimezone() {
return this.moCache;
}
@Override
ChronoOperator onTimestamp() {
return this.tsCache;
}
private static , T extends ChronoEntity>
ChronoOperator child(
AdvancedElement element,
boolean up,
Class context
) {
String compare = element.name();
if (
compare.equals("MILLI_OF_SECOND")
|| compare.equals("MILLI_OF_DAY")
) {
return new FractionOperator('3', up);
} else if (
compare.equals("MICRO_OF_SECOND")
|| compare.equals("MICRO_OF_DAY")
) {
return new FractionOperator('6', up);
} else if (
compare.equals("NANO_OF_SECOND")
|| compare.equals("NANO_OF_DAY")
) {
return new FractionOperator('9', up);
}
if (up) {
return element.atCeiling(context);
} else {
return element.atFloor(context);
}
}
private static >
ChronoOperator newValue(
AdvancedElement element,
Object value
) {
return element.newValue(
element.getType().cast(value),
PlainTime.class);
}
private static >
ChronoOperator newValueTS(
AdvancedElement element,
Object value
) {
return new ValueOperator(
element.newValue(
element.getType().cast(value),
PlainTimestamp.class),
value
);
}
private static >
ChronoOperator lenient(
AdvancedElement element,
Object value
) {
return element.setLenient(
element.getType().cast(value),
PlainTime.class);
}
private static >
ChronoOperator lenientTS(
AdvancedElement element,
Object value
) {
return new ValueOperator(
element.setLenient(
element.getType().cast(value),
PlainTimestamp.class),
Number.class.cast(value)
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy