Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
9213:e3d23e1288bb | 9214:091fd9676496 |
---|---|
11 import java.io.Serializable; | 11 import java.io.Serializable; |
12 import java.util.Date; | 12 import java.util.Date; |
13 | 13 |
14 import org.dive4elements.artifacts.common.utils.DateUtils; | 14 import org.dive4elements.artifacts.common.utils.DateUtils; |
15 | 15 |
16 public class DateRange | 16 public class DateRange implements Serializable { |
17 implements Serializable | |
18 { | |
19 private static final long serialVersionUID = -2553914795388094818L; | 17 private static final long serialVersionUID = -2553914795388094818L; |
20 | 18 |
21 protected Date from; | 19 private final Date from; |
22 protected Date to; | 20 private final Date to; |
23 | 21 |
24 public DateRange(final Date from, final Date to) { | 22 public DateRange(final Date from, final Date to) { |
25 this.from = from; | 23 this.from = from; |
26 this.to = to; | 24 this.to = to; |
27 } | 25 } |
28 | 26 |
29 public Date getFrom() { | 27 public Date getFrom() { |
30 return this.from; | 28 return this.from; |
31 } | 29 } |
32 | 30 |
33 public void setFrom(final Date from) { | |
34 this.from = from; | |
35 } | |
36 | |
37 public Date getTo() { | 31 public Date getTo() { |
38 return this.to; | 32 return this.to; |
39 } | 33 } |
40 | 34 |
41 public void setTo(final Date to) { | 35 private static final boolean equalDates(final Date a, final Date b) { |
42 this.to = to; | 36 if (a == null && b != null) |
43 } | 37 return false; |
44 | 38 |
45 private static final boolean equalDates(final Date a, final Date b) { | 39 if (a != null && b == null) |
46 if (a == null && b != null) return false; | 40 return false; |
47 if (a != null && b == null) return false; | 41 |
48 if (a == null) return true; | 42 if (a == null) |
43 return true; | |
44 | |
49 return a.equals(b); | 45 return a.equals(b); |
50 } | 46 } |
51 | 47 |
52 @Override | 48 @Override |
53 public boolean equals(final Object other) { | 49 public boolean equals(final Object other) { |
54 if (!(other instanceof DateRange)) { | 50 if (!(other instanceof DateRange)) |
55 return false; | 51 return false; |
56 } | 52 |
57 final DateRange o = (DateRange)other; | 53 final DateRange o = (DateRange) other; |
58 return equalDates(this.from, o.from) && equalDates(this.to, o.to); | 54 return equalDates(this.from, o.from) && equalDates(this.to, o.to); |
59 } | 55 } |
60 | 56 |
61 public int getFromYear() { | 57 public int getFromYear() { |
62 return DateUtils.getYearFromDate(this.from); | 58 return DateUtils.getYearFromDate(this.from); |
64 | 60 |
65 public int getToYear() { | 61 public int getToYear() { |
66 return DateUtils.getYearFromDate(this.to); | 62 return DateUtils.getYearFromDate(this.to); |
67 } | 63 } |
68 } | 64 } |
69 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |