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