net.time4j.LongElement Maven / Gradle / Ivy
/*
* -----------------------------------------------------------------------
* Copyright © 2013-2014 Meno Hochschild,
* -----------------------------------------------------------------------
* This file (LongElement.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.ChronoElement;
import net.time4j.engine.ChronoEntity;
import net.time4j.engine.ChronoFunction;
import net.time4j.engine.ChronoOperator;
import net.time4j.format.NumericalElement;
import java.io.InvalidObjectException;
import java.io.ObjectStreamException;
import java.math.BigDecimal;
/**
* Repräsentiert ein Uhrzeitelement vom long-Typ.
*
* @author Meno Hochschild
* @concurrency
*/
final class LongElement
extends AbstractTimeElement
implements ProportionalElement,
NumericalElement {
//~ Statische Felder/Initialisierungen --------------------------------
/**
* Zeiger auf den Tagesüberlauf.
*/
static final ChronoElement DAY_OVERFLOW = new LongElement();
private static final long serialVersionUID = 5930990958663061693L;
//~ Instanzvariablen --------------------------------------------------
private transient final Long defaultMin;
private transient final Long defaultMax;
private transient final ChronoFunction, BigDecimal> rf;
//~ Konstruktoren -----------------------------------------------------
private LongElement() {
this("DAY_OVERFLOW", Long.MIN_VALUE, Long.MAX_VALUE);
}
private LongElement(
String name,
long defaultMin,
long defaultMax
) {
super(name);
this.defaultMin = Long.valueOf(defaultMin);
this.defaultMax = Long.valueOf(defaultMax);
this.rf = new ProportionalFunction(this, true);
}
//~ Methoden ----------------------------------------------------------
@Override
public Class getType() {
return Long.class;
}
@Override
public Long getDefaultMinimum() {
return this.defaultMin;
}
@Override
public Long getDefaultMaximum() {
return this.defaultMax;
}
@Override
public boolean isDateElement() {
return false;
}
@Override
public boolean isTimeElement() {
return true;
}
@Override
public int numerical(Long value) {
long v = value.longValue();
if (v >= Integer.MIN_VALUE && v <= Integer.MAX_VALUE) {
return value.intValue();
} else {
throw new ArithmeticException("Numerical overflow: " + value);
}
}
@Override
public ChronoFunction, BigDecimal> ratio() {
return this.rf;
}
@Override
public ChronoOperator roundedUp(int stepwidth) {
return new RoundingOperator(
this,
Boolean.TRUE,
stepwidth
);
}
@Override
public ChronoOperator roundedHalf(int stepwidth) {
return new RoundingOperator(
this,
null,
stepwidth
);
}
@Override
public ChronoOperator roundedDown(int stepwidth) {
return new RoundingOperator(
this,
Boolean.FALSE,
stepwidth
);
}
/**
* Erzeugt ein neues Uhrzeitelement ohne Formatsymbol.
*
* @param name name of element
* @param defaultMin default minimum
* @param defaultMax default maximum
*/
static LongElement create(
String name,
long defaultMin,
long defaultMax
) {
return new LongElement(name, defaultMin, defaultMax);
}
private Object readResolve() throws ObjectStreamException {
Object element = PlainTime.lookupElement(this.name());
if (element == null) {
if (this.name().equals("DAY_OVERFLOW")) {
return DAY_OVERFLOW;
} else {
throw new InvalidObjectException(this.name());
}
} else {
return element;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy