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.mpeg7;
24  
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.w3c.dom.Node;
28  
29  import java.util.ArrayList;
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * Default implementation of the temporal decomposition.
35   */
36  public class TemporalDecompositionImpl<T extends Segment> implements TemporalDecomposition<T> {
37  
38    /** <code>True</code> if there is a gap */
39    protected boolean gap = false;
40  
41    /** <code>True</code> if the segment overlaps */
42    protected boolean overlap = false;
43  
44    /** The decomposition criteria */
45    protected DecompositionCriteria criteria = DecompositionCriteria.Temporal;
46  
47    /** The list of segments in this temporal decomposition */
48    protected List<T> segments = null;
49  
50    /** Indicator for the presence of gaps in between segments */
51    protected boolean hasGap = false;
52  
53    /** Indicator for the presence of overlapping segments */
54    protected boolean isOverlapping = false;
55  
56    /** The segment type */
57    private Segment.Type segmentType = null;
58  
59    /**
60     * Creates a new temporal decomposition container.
61     *
62     * @param segmentType
63     *          the segment type
64     */
65    public TemporalDecompositionImpl(Segment.Type segmentType) {
66      segments = new ArrayList<T>();
67      this.segmentType = segmentType;
68    }
69  
70    /**
71     * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#setGap(boolean)
72     */
73    public void setGap(boolean hasGap) {
74      this.hasGap = hasGap;
75    }
76  
77    /**
78     * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#hasGap()
79     */
80    public boolean hasGap() {
81      return gap;
82    }
83  
84    @SuppressWarnings("unchecked")
85    public T createSegment(String id) {
86      T segment = (T) new SegmentImpl(segmentType, id);
87      if (segments.contains(segment)) {
88        throw new IllegalArgumentException("Duplicate segment id detected: " + id);
89      }
90      segments.add(segment);
91      return segment;
92    }
93  
94    /**
95     * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#hasSegments()
96     */
97    public boolean hasSegments() {
98      return segments.size() > 0;
99    }
100 
101   /**
102    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#setOverlapping(boolean)
103    */
104   public void setOverlapping(boolean isOverlapping) {
105     this.isOverlapping = isOverlapping;
106   }
107 
108   /**
109    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#setCriteria(
110    * org.opencastproject.metadata.mpeg7.TemporalDecomposition.DecompositionCriteria)
111    */
112   public void setCriteria(DecompositionCriteria criteria) {
113     if (criteria == null) {
114       throw new IllegalArgumentException("Decomposition criteria must not be null");
115     }
116     this.criteria = criteria;
117   }
118 
119   /**
120    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#isOverlapping()
121    */
122   public boolean isOverlapping() {
123     return overlap;
124   }
125 
126   /**
127    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#getCriteria()
128    */
129   public DecompositionCriteria getCriteria() {
130     return DecompositionCriteria.Temporal;
131   }
132 
133   /**
134    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#getSegmentById(java.lang.String)
135    */
136   public T getSegmentById(String segmentId) {
137     for (T segment : segments) {
138       if (segmentId.equals(segment.getIdentifier())) {
139         return segment;
140       }
141     }
142     return null;
143   }
144 
145   /**
146    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#segments()
147    */
148   public Iterator<T> segments() {
149     return segments.iterator();
150   }
151 
152   /**
153    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
154    */
155   public Node toXml(Document document) {
156     Element node = document.createElement("TemporalDecomposition");
157     node.setAttribute("gap", (gap ? "true" : "false"));
158     node.setAttribute("overlap", (overlap ? "true" : "false"));
159     node.setAttribute("criteria", criteria.toString().toLowerCase());
160     for (Segment segment : segments) {
161       node.appendChild(segment.toXml(document));
162     }
163     return node;
164   }
165 
166 }