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      if (start != null && end != null && end.before(start))
53        throw new IllegalStateException("The end date is before the start date");
54  
55      this.start = start;
56      this.end = end;
57      this.name = name;
58    }
59  
60    /**
61     * Returns the start date of the period or null, if it has only an upper bound.
62     */
63    public Date getStart() {
64      return start;
65    }
66  
67    /**
68     * Returns the end date of the period or null, if it has only a lower bound.
69     */
70    public Date getEnd() {
71      return end;
72    }
73  
74    /**
75     * Returns the optional name of the period.
76     *
77     * @return the name of the period or null
78     */
79    public String getName() {
80      return name;
81    }
82  
83    /**
84     * Checks if the interval is closed.
85     */
86    public boolean isClosed() {
87      return start != null && end != null;
88    }
89  
90    /**
91     * Checks if the interval has a start boundary.
92     */
93    public boolean hasStart() {
94      return start != null;
95    }
96  
97    /**
98     * Checks if the interval has an end boundary.
99     */
100   public boolean hasEnd() {
101     return end != null;
102   }
103 
104   /**
105    * Checks if the interval has a name.
106    */
107   public boolean hasName() {
108     return name != null;
109   }
110 
111   @Override
112   public String toString() {
113     return "DCMIPeriod{" + "start=" + (start != null ? start : "]") + ", end=" + (end != null ? end : "[")
114             + (name != null ? ", name='" + name + '\'' : "") + '}';
115   }
116 
117 }