view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixingsFacet.java @ 4442:26774405c884

Introduce a new FixingsFacet to add save access to the current Km Values which are accessed by a String like a map must be checked for null. The new FixingsFacet adds a getCurrentKm method that abstracts the direct access to the CallContext and always returns a valid double value.
author Björn Ricks <bjoern.ricks@intevation.de>
date Wed, 07 Nov 2012 14:06:03 +0100
parents
children b195fede1c3b
line wrap: on
line source
package de.intevation.flys.artifacts.model.fixings;

import de.intevation.artifacts.CallContext;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.artifacts.states.DefaultState.ComputeType;

/**
 * Facet to access the current Km from the context safely
 * 
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public class FixingsFacet extends DataFacet {

    public static final Double INVALID_KM = Double.valueOf(-1d);
    public static final String CURRENT_KM = "currentKm";

    public FixingsFacet() {
    }

    public  FixingsFacet(String name, String description) {
        super(0, name, description, ComputeType.ADVANCE, null, null);
    }

    public FixingsFacet(
            int         index,
            String      name,
            String      description,
            ComputeType type,
            String      hash,
            String      stateId
            ) {
        super(index, name, description, type, hash, stateId);
    }

    /**
     * Returns the current km from the context.
     * If the context is null or doesn't contain a currentKm then a double value of -1 will
     *  be returned.
     * @param context The CallContext instance
     * @return the current km as double
     */
    protected double getCurrentKm(CallContext context) {
        if (context == null) {
            return INVALID_KM;
        }
        Double dkm = (Double)context.getContextValue(CURRENT_KM);
        if (dkm == null) {
            return INVALID_KM;
        }
        return dkm.doubleValue();
    }
}

http://dive4elements.wald.intevation.org