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

net.time4j.engine.AdvancedElement Maven / Gradle / Ivy

/*
 * -----------------------------------------------------------------------
 * Copyright © 2013-2015 Meno Hochschild, 
 * -----------------------------------------------------------------------
 * This file (AdvancedElement.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.engine;


/**
 * 

Default implementation of a chronological element with some * element-related manipulation methods.

* * @param generic type of element values * @author Meno Hochschild * @serial exclude */ /*[deutsch] *

Standardimplementierung eines chronologischen Elements mit verschiedenen * element-bezogenen Manipulationsmethoden.

* * @param generic type of element values * @author Meno Hochschild * @serial exclude */ public abstract class AdvancedElement> extends BasicElement { //~ Statische Felder/Initialisierungen -------------------------------- private static final int NEW_VALUE_MODE = 0; private static final int MIN_MODE = 1; private static final int MAX_MODE = 2; private static final int FLOOR_MODE = 3; private static final int CEILING_MODE = 4; private static final int LENIENT_MODE = 5; private static final int DECREMENTING_MODE = 6; private static final int INCREMENTING_MODE = 7; //~ Konstruktoren ----------------------------------------------------- /** *

Used by subclasses which usually assign an instance to a * static constant (creating a singleton).

* * @param name name of element * @throws IllegalArgumentException if the name is empty or if it only * contains white space (spaces, tabs etc.) * @see ChronoElement#name() */ /*[deutsch] *

Konstruktor für Subklassen, die eine so erzeugte Instanz * in der Regel statischen Konstanten zuweisen und damit Singletons * erzeugen.

* * @param name name of element * @throws IllegalArgumentException if the name is empty or if it only * contains white space (spaces, tabs etc.) * @see ChronoElement#name() */ protected AdvancedElement(String name) { super(name); } //~ Methoden ---------------------------------------------------------- /** *

Compares the values of this element based on their natural order.

* * @throws ChronoException if this element is not registered in any entity * and/or if no element rule exists to extract the element value */ /*[deutsch] *

Vergleicht die Werte dieses Elements auf Basis ihrer * natürlichen Ordnung.

* * @throws ChronoException if this element is not registered in any entity * and/or if no element rule exists to extract the element value */ @Override public int compare( ChronoDisplay o1, ChronoDisplay o2 ) { return o1.get(this).compareTo(o2.get(this)); } /** *

Yields a new operator which can set any entity to its minimum * element value.

* * @param generic type of target entity * @param context context type * @return operator */ /*[deutsch] *

Liefert einen Operator, der eine beliebige Entität auf * das Elementminimum setzt.

* * @param generic type of target entity * @param context context type * @return operator */ public > ChronoOperator minimized( Class context ) { return new StdOperator(MIN_MODE, this); } /** *

Yields a new operator which can set any entity to its maximum * element value.

* * @param generic type of target entity * @param context context type * @return operator */ /*[deutsch] *

Liefert einen Operator, der eine beliebige Entität auf * das Elementmaximum setzt.

* * @param generic type of target entity * @param context context type * @return operator */ public > ChronoOperator maximized( Class context ) { return new StdOperator(MAX_MODE, this); } /** *

Yields a new operator which can set any entity such that its * actual element value gets the decremented value.

* * @param generic type of target entity * @param context context type * @return decrementing operator * @see TimeAxis#getBaseUnit(ChronoElement) */ /*[deutsch] *

Liefert einen Operator, der eine beliebige Entität so * anpasst, daß dieses Element den vorherigen Wert bekommt.

* * @param generic type of target entity * @param context context type * @return decrementing operator * @see TimeAxis#getBaseUnit(ChronoElement) */ public > ChronoOperator decremented( Class context ) { return new StdOperator(DECREMENTING_MODE, this); } /** *

Yields a new operator which can set any entity such that its * actual element value gets the incremented value.

* * @param generic type of target entity * @param context context type * @return incrementing operator * @see TimeAxis#getBaseUnit(ChronoElement) */ /*[deutsch] *

Liefert einen Operator, der eine beliebige Entität so * anpasst, daß dieses Element den nächsten Wert bekommt.

* * @param generic type of target entity * @param context context type * @return incrementing operator * @see TimeAxis#getBaseUnit(ChronoElement) */ public > ChronoOperator incremented( Class context ) { return new StdOperator(INCREMENTING_MODE, this); } /** *

Yields an operator which rounds any entity down so that the child * elements will be set to the minimum.

* * @param generic type of target entity * @param context context type * @return operator */ /*[deutsch] *

Liefert einen Operator, der eine Entität abrundet, indem alle * Kindselemente dieses Elements auf ihr Minimum gesetzt werden.

* * @param generic type of target entity * @param context context type * @return operator */ public > ChronoOperator atFloor( Class context ) { return new StdOperator(FLOOR_MODE, this); } /** *

Yields an operator which rounds any entity up so that the child * elements will be set to the maximum.

* * @param generic type of target entity * @param context context type * @return operator */ /*[deutsch] *

Liefert einen Operator, der eine Entität aufrundet, indem alle * Kindselemente dieses Elements auf ihr Maximum gesetzt werden.

* * @param generic type of target entity * @param context context type * @return operator */ public > ChronoOperator atCeiling( Class context ) { return new StdOperator(CEILING_MODE, this); } /** *

Yields an operator which sets any entity such that its actual * element value will be set in lenient mode to given value.

* * @param generic type of target entity * @param value new element value * @param context context type * @return operator */ /*[deutsch] *

Liefert einen Operator, der eine beliebige Entität so * anpasst, daß dieses Element auf den angegebenen Wert im * Nachsichtigkeitsmodus gesetzt wird.

* * @param generic type of target entity * @param value new element value * @param context context type * @return operator */ public > ChronoOperator setLenient( V value, Class context ) { return new StdOperator(LENIENT_MODE, this, value); } /** *

Yields an operator which sets any entity such that its actual * element value will be set in normal mode to given value.

* * @param generic type of target entity * @param value new element value * @param context context type * @return operator * @since 2.0 */ /*[deutsch] *

Liefert einen Operator, der eine beliebige Entität so * anpasst, daß dieses Element auf den angegebenen Wert im * Standardmodus gesetzt wird.

* * @param generic type of target entity * @param value new element value * @param context context type * @return operator * @since 2.0 */ public > ChronoOperator newValue( V value, Class context ) { return new StdOperator(NEW_VALUE_MODE, this, value); } //~ Innere Klassen ---------------------------------------------------- private static class StdOperator> implements ChronoOperator { //~ Instanzvariablen ---------------------------------------------- private final int mode; private final ChronoElement element; private final Object value; //~ Konstruktoren ------------------------------------------------- StdOperator( int mode, ChronoElement element ) { this(mode, element, null); } StdOperator( int mode, ChronoElement element, Object value // optional ) { super(); this.mode = mode; this.element = element; this.value = value; } //~ Methoden ------------------------------------------------------ @Override public T apply(T entity) { switch (this.mode) { case NEW_VALUE_MODE: return value(entity, this.element, this.value, false); case MIN_MODE: return min(entity, this.element); case MAX_MODE: return max(entity, this.element); case FLOOR_MODE: return floor(entity, this.element); case CEILING_MODE: return ceiling(entity, this.element); case LENIENT_MODE: return value(entity, this.element, this.value, true); case DECREMENTING_MODE: return this.move(entity, false); case INCREMENTING_MODE: return this.move(entity, true); default: throw new UnsupportedOperationException( "Unknown mode: " + this.mode); } } private static , V> T min( ChronoEntity entity, ChronoElement element ) { return entity.with(element, entity.getMinimum(element)); } private static , V> T max( ChronoEntity entity, ChronoElement element ) { return entity.with(element, entity.getMaximum(element)); } private static , V> T floor( ChronoEntity entity, ChronoElement element ) { T ctx = entity.getContext(); ChronoElement e = element; while ((e = getRule(ctx, e).getChildAtFloor(ctx)) != null) { ctx = withFloor(ctx, e); } return ctx; } private static , V> T ceiling( ChronoEntity entity, ChronoElement element ) { T ctx = entity.getContext(); ChronoElement e = element; while ((e = getRule(ctx, e).getChildAtCeiling(ctx)) != null) { ctx = withCeiling(ctx, e); } return ctx; } private static , V> T value( ChronoEntity entity, ChronoElement element, Object value, boolean lenient ) { T ctx = entity.getContext(); return getRule(ctx, element).withValue( ctx, element.getType().cast(value), lenient ); } private static , V> ElementRule getRule( T context, ChronoElement element ) { return context.getChronology().getRule(element); } private static , V> T withFloor( T context, ChronoElement element ) { ElementRule rule = getRule(context, element); return rule.withValue( context, rule.getMinimum(context), element.isLenient() ); } private static , V> T withCeiling( T context, ChronoElement element ) { ElementRule rule = getRule(context, element); return rule.withValue( context, rule.getMaximum(context), element.isLenient() ); } private T move( T entity, boolean forward ) { T result = null; if (entity instanceof TimePoint) { TimePoint tp = TimePoint.class.cast(entity); Object answer = add(tp, this.element, forward); result = entity.getChronology().getChronoType().cast(answer); } else { throw new ChronoException( "Base units not supported by: " + entity.getChronology().getChronoType()); } return result; } // wildcard capture private static > T add( TimePoint context, ChronoElement element, boolean forward ) { U unit = context.getChronology().getBaseUnit(element); if (forward) { return context.plus(1, unit); } else { return context.minus(1, unit); } } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy