task4/solution11/src/org/apidesign/apifest08/currency/DateUtil.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 25 Oct 2008 20:53:00 +0200
changeset 84 2ae6e4aa7aef
permissions -rw-r--r--
Solutions by Petr Smid
     1 package org.apidesign.apifest08.currency;
     2 
     3 import java.util.Date;
     4 
     5 /**
     6  * Date util helper class.
     7  * @author ked
     8  */
     9 final class DateUtil {
    10 
    11     private DateUtil() {};
    12 
    13     static boolean isInRange(Date instant, Date from, Date till) {
    14         if ((from == null || instant.equals(from) || instant.after(from)) &&
    15                 (till == null || instant.before(till))) {
    16             return true;
    17         } else {
    18             return false;
    19         }
    20     }
    21 
    22     static boolean isRangesOverlapping(Date fromA, Date tillA, Date fromB, Date tillB) {
    23         if ((fromA == null && tillA == null) || (fromB == null && tillB == null)) {
    24             return true;
    25         }
    26         if (fromA != null && isInRange(fromA, fromB, tillB)) {
    27             return true;
    28         }
    29         if (tillA != null && !tillA.equals(fromB) && isInRange(tillA, fromB, tillB)) {
    30             return true;
    31         }
    32         if (fromB != null && isInRange(fromB, fromA, tillA)) {
    33             return true;
    34         }
    35         if (tillB != null && !tillB.equals(fromA) && isInRange(tillB, fromA, tillA)) {
    36             return true;
    37         }
    38         return false;
    39     }
    40 
    41     static Date getRangesIntersectionBottom(Date fromA, Date fromB) {
    42         if (fromA == null) {
    43             return fromB;
    44         }
    45         if (fromB == null) {
    46             return fromA;
    47         }
    48         if (fromA.after(fromB)) {
    49             return fromA;
    50         } else {
    51             return fromB;
    52         }
    53     }
    54 
    55     static Date getRangesIntersectionTop(Date tillA, Date tillB) {
    56         if (tillA == null) {
    57             return tillB;
    58         }
    59         if (tillB == null) {
    60             return tillA;
    61         }
    62         if (tillA.before(tillB)) {
    63             return tillA;
    64         } else {
    65             return tillB;
    66         }
    67     }
    68 }