view artifacts/src/main/java/org/dive4elements/river/artifacts/model/DateRange.java @ 9214:091fd9676496

Made DateRange class immutable
author gernotbelger
date Tue, 03 Jul 2018 14:26:56 +0200
parents 9b4f5e61c02e
children
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts.model;

import java.io.Serializable;
import java.util.Date;

import org.dive4elements.artifacts.common.utils.DateUtils;

public class DateRange implements Serializable {
    private static final long serialVersionUID = -2553914795388094818L;

    private final Date from;
    private final Date to;

    public DateRange(final Date from, final Date to) {
        this.from = from;
        this.to = to;
    }

    public Date getFrom() {
        return this.from;
    }

    public Date getTo() {
        return this.to;
    }

    private static final boolean equalDates(final Date a, final Date b) {
        if (a == null && b != null)
            return false;

        if (a != null && b == null)
            return false;

        if (a == null)
            return true;

        return a.equals(b);
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof DateRange))
            return false;

        final DateRange o = (DateRange) other;
        return equalDates(this.from, o.from) && equalDates(this.to, o.to);
    }

    public int getFromYear() {
        return DateUtils.getYearFromDate(this.from);
    }

    public int getToYear() {
        return DateUtils.getYearFromDate(this.to);
    }
}

http://dive4elements.wald.intevation.org