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 related algebraic data type.
29   */
30  public abstract class Temporal {
31  
32    // prohibit inheritance from outside this class
33    private Temporal() {
34    }
35  
36    /**
37     * An instant in time.
38     */
39    public static Temporal instant(final Date instant) {
40      if (instant == null)
41        throw new IllegalArgumentException("instant must not be null");
42      return new Temporal() {
43        public Date getInstant() {
44          return instant;
45        }
46  
47        @Override
48        public <A> A fold(Match<A> v) {
49          return v.instant(instant);
50        }
51      };
52    }
53  
54    /**
55     * A period in time limited by at least one instant.
56     */
57    public static Temporal period(final DCMIPeriod period) {
58      if (period == null)
59        throw new IllegalArgumentException("period must not be null");
60      return new Temporal() {
61        public DCMIPeriod getPeriod() {
62          return period;
63        }
64  
65        @Override
66        public <A> A fold(Match<A> v) {
67          return v.period(period);
68        }
69      };
70    }
71  
72    /**
73     * A time span measured in milliseconds.
74     */
75    public static Temporal duration(final long duration) {
76      if (duration < 0)
77        throw new IllegalArgumentException("duration must be positive or zero");
78      return new Temporal() {
79        public long getDuration() {
80          return duration;
81        }
82  
83        @Override
84        public <A> A fold(Match<A> v) {
85          return v.duration(duration);
86        }
87      };
88    }
89  
90    /**
91     * Safe decomposition.
92     */
93    public abstract <A> A fold(Match<A> v);
94  
95    /**
96     * Safe temporal decomposition.
97     */
98    public interface Match<A> {
99      A period(DCMIPeriod period);
100 
101     A instant(Date instant);
102 
103     A duration(long duration);
104   }
105 
106 }