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      segments.add(segment);
90      return segment;
91    }
92  
93    /**
94     * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#hasSegments()
95     */
96    public boolean hasSegments() {
97      return segments.size() > 0;
98    }
99  
100   /**
101    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#setOverlapping(boolean)
102    */
103   public void setOverlapping(boolean isOverlapping) {
104     this.isOverlapping = isOverlapping;
105   }
106 
107   /**
108    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#setCriteria(org.opencastproject.metadata.mpeg7.TemporalDecomposition.DecompositionCriteria)
109    */
110   public void setCriteria(DecompositionCriteria criteria) {
111     if (criteria == null)
112       throw new IllegalArgumentException("Decomposition criteria must not be null");
113     this.criteria = criteria;
114   }
115 
116   /**
117    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#isOverlapping()
118    */
119   public boolean isOverlapping() {
120     return overlap;
121   }
122 
123   /**
124    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#getCriteria()
125    */
126   public DecompositionCriteria getCriteria() {
127     return DecompositionCriteria.Temporal;
128   }
129 
130   /**
131    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#getSegmentById(java.lang.String)
132    */
133   public T getSegmentById(String segmentId) {
134     for (T segment : segments) {
135       if (segmentId.equals(segment.getIdentifier()))
136         return segment;
137     }
138     return null;
139   }
140 
141   /**
142    * @see org.opencastproject.metadata.mpeg7.TemporalDecomposition#segments()
143    */
144   public Iterator<T> segments() {
145     return segments.iterator();
146   }
147 
148   /**
149    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
150    */
151   public Node toXml(Document document) {
152     Element node = document.createElement("TemporalDecomposition");
153     node.setAttribute("gap", (gap ? "true" : "false"));
154     node.setAttribute("overlap", (overlap ? "true" : "false"));
155     node.setAttribute("criteria", criteria.toString().toLowerCase());
156     for (Segment segment : segments) {
157       node.appendChild(segment.toXml(document));
158     }
159     return node;
160   }
161 
162 }