view flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/Access.java @ 3307:81a98f360553

Very basic official lines in dc, malplaced. flys-artifacts/trunk@4995 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 13 Jul 2012 13:43:26 +0000
parents 9422b559b2d5
children 5ff3b2f5fb1c
line wrap: on
line source
package de.intevation.flys.artifacts.access;

import de.intevation.artifactdatabase.data.StateData;

import de.intevation.flys.artifacts.FLYSArtifact;

import de.intevation.flys.artifacts.model.DateRange;

import de.intevation.flys.utils.FLYSUtils;

import gnu.trove.TDoubleArrayList;

import java.util.ArrayList;
import java.util.Date;

import org.apache.log4j.Logger;

public class Access
{
    private static Logger log = Logger.getLogger(Access.class);

    protected FLYSArtifact artifact;

    public Access() {
    }

    public Access(FLYSArtifact artifact) {
        this.artifact = artifact;
    }

    public FLYSArtifact getArtifact() {
        return artifact;
    }

    public void setArtifact(FLYSArtifact artifact) {
        this.artifact = artifact;
    }

    protected String getString(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key + "' value");
            return null;
        }
        return (String)sd.getValue();
    }

    protected Double getDouble(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key + "' value");
            return null;
        }
        try {
            return Double.valueOf((String)sd.getValue());
        }
        catch (NumberFormatException nfe) {
            log.warn(key + " '" + sd.getValue() + "' is not numeric.");
        }
        return null;
    }

    protected Long getLong(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key + "' value");
            return null;
        }
        try {
            return Long.valueOf((String)sd.getValue());
        }
        catch (NumberFormatException nfe) {
            log.warn(key + " '" + sd.getValue() + "' is not a long integer.");
        }
        return null;
    }

    protected Integer getInteger(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key + "' value");
            return null;
        }
        try {
            return Integer.valueOf((String)sd.getValue());
        }
        catch (NumberFormatException nfe) {
            log.warn(key + " '" + sd.getValue() + "' is not a integer.");
        }
        return null;
    }

    protected int [] getIntArray(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key +"' value");
            return null;
        }
        return FLYSUtils.intArrayFromString((String)sd.getValue());
    }

    protected DateRange [] getDateRange(String key) {

        StateData sd = artifact.getData(key);

        if (sd == null) {
            log.warn("missing '" + key + "'");
            return null;
        }

        String data = (String)sd.getValue();
        String[] pairs = data.split("\\s*;\\s*");

        ArrayList<DateRange> aPs = new ArrayList<DateRange>(pairs.length);

        for (int i = 0; i < pairs.length; i++) {
            String[] fromTo = pairs[i].split("\\s*,\\s*");
            if (fromTo.length >= 2) {
                try {
                    Date from = new Date(Long.parseLong(fromTo[0]));
                    Date to   = new Date(Long.parseLong(fromTo[1]));
                    DateRange aP = new DateRange(from, to);
                    if (!aPs.contains(aP)) {
                        aPs.add(aP);
                    }
                }
                catch (NumberFormatException nfe) {
                    log.warn(key + " contains no long values.", nfe);
                }
            }
        }

        DateRange [] result = aPs.toArray(new DateRange[aPs.size()]);

        if (log.isDebugEnabled()) {
            for (int i = 0; i < result.length; ++i) {
                DateRange ap = result[i];
                log.debug("period " +
                    ap.getFrom() + " - " + ap.getTo());
            }
        }

        return result;
    }

    protected Boolean getBoolean(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key + "' value");
            return null;
        }
        return Boolean.valueOf((String)sd.getValue());
    }

    protected double [] getDoubleArray(String key) {
        StateData sd = artifact.getData(key);
        if (sd == null) {
            log.warn("missing '" + key + "'");
            return null;
        }
        String [] parts = ((String)sd.getValue()).split("[\\s;]+");
        TDoubleArrayList list = new TDoubleArrayList(parts.length);
        for (String part: parts) {
            try {
                list.add(Double.parseDouble(part));
            }
            catch (NumberFormatException nfe) {
                log.warn("'" + part + "' is not numeric.");
            }
        }
        return list.toNativeArray();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org