View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   *
20   */
21  
22  
23  package org.opencastproject.metadata.dublincore;
24  
25  import java.util.Date;
26  
27  /**
28   * A time interval, representing a DCMI period. They may be open.
29   * <p>
30   * For further information on DCMI periods please refer to <a
31   * href="http://dublincore.org/documents/dcmi-period/">http://dublincore.org/documents/dcmi-period/</a>.
32   */
33  public final class DCMIPeriod {
34  
35    private final Date start;
36    private final Date end;
37    private final String name;
38  
39    /**
40     * Create a new period. To create an open interval you may set one of the boundaries null.
41     */
42    public DCMIPeriod(Date start, Date end) {
43      this(start, end, null);
44    }
45  
46    /**
47     * Create a new period with an optional name. To create an open interval you may set one of the bounbaries null.
48     */
49    public DCMIPeriod(Date start, Date end, String name) {
50      if (start == null && end == null) {
51        throw new IllegalStateException("A period must be bounded at least at one end");
52      }
53      if (start != null && end != null && end.before(start)) {
54        throw new IllegalStateException("The end date is before the start date");
55      }
56  
57      this.start = start;
58      this.end = end;
59      this.name = name;
60    }
61  
62    /**
63     * Returns the start date of the period or null, if it has only an upper bound.
64     */
65    public Date getStart() {
66      return start;
67    }
68  
69    /**
70     * Returns the end date of the period or null, if it has only a lower bound.
71     */
72    public Date getEnd() {
73      return end;
74    }
75  
76    /**
77     * Returns the optional name of the period.
78     *
79     * @return the name of the period or null
80     */
81    public String getName() {
82      return name;
83    }
84  
85    /**
86     * Checks if the interval is closed.
87     */
88    public boolean isClosed() {
89      return start != null && end != null;
90    }
91  
92    /**
93     * Checks if the interval has a start boundary.
94     */
95    public boolean hasStart() {
96      return start != null;
97    }
98  
99    /**
100    * Checks if the interval has an end boundary.
101    */
102   public boolean hasEnd() {
103     return end != null;
104   }
105 
106   /**
107    * Checks if the interval has a name.
108    */
109   public boolean hasName() {
110     return name != null;
111   }
112 
113   @Override
114   public String toString() {
115     return "DCMIPeriod{" + "start=" + (start != null ? start : "]") + ", end=" + (end != null ? end : "[")
116             + (name != null ? ", name='" + name + '\'' : "") + '}';
117   }
118 
119 }