All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opencastproject.mediapackage.selector.StreamElementSelector Maven / Gradle / Ivy

There is a newer version: 16.7
Show newest version
/**
 * Licensed to The Apereo Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 *
 * The Apereo Foundation licenses this file to you under the Educational
 * Community License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License
 * at:
 *
 *   http://opensource.org/licenses/ecl2.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

package org.opencastproject.mediapackage.selector;

import org.opencastproject.mediapackage.MediaPackage;
import org.opencastproject.mediapackage.MediaPackageElementFlavor;
import org.opencastproject.mediapackage.Stream;
import org.opencastproject.mediapackage.Track;
import org.opencastproject.mediapackage.TrackSupport;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * This MediaPackageElementSelector selects all tracks from a MediaPackage that contain at
 * least an audio stream while optionally matching other requirements such as flavors and tags.
 */
public class StreamElementSelector extends AbstractMediaPackageElementSelector {

  /**
   * Creates a new selector.
   */
  public StreamElementSelector() {
  }

  /**
   * Creates a new selector that will restrict the result of select() to the given flavor.
   *
   * @param flavor
   *          the flavor
   */
  public StreamElementSelector(String flavor) {
    this(MediaPackageElementFlavor.parseFlavor(flavor));
  }

  /**
   * Creates a new selector that will restrict the result of select() to the given flavor.
   *
   * @param flavor
   *          the flavor
   */
  public StreamElementSelector(MediaPackageElementFlavor flavor) {
    addFlavor(flavor);
  }

  /**
   * Returns all tracks from a MediaPackage that contain at least a Stream of the parametrized
   * type while optionally matching other requirements such as flavors and tags. If no such combination can be found, i.
   * g. there is no audio or video at all, an empty array is returned.
   *
   * @see org.opencastproject.mediapackage.selector.AbstractMediaPackageElementSelector#select(org.opencastproject.mediapackage.MediaPackage, boolean)
   */
  @SuppressWarnings("unchecked")
  @Override
  public Collection select(MediaPackage mediaPackage, boolean withTagsAndFlavors) {
    Collection candidates = super.select(mediaPackage, withTagsAndFlavors);
    List result = new ArrayList();
    for (Track t : candidates) {
      if (TrackSupport.byType(t.getStreams(), getParametrizedStreamType()).length > 0) {
        result.add(t);
      }
    }
    return result;
  }

  /**
   * This constructor tries to determine the entity type from the type argument used by a concrete implementation of
   * GenericHibernateDao.
   */
  @SuppressWarnings("unchecked")
  private Class getParametrizedStreamType() {
    Class current = getClass();
    Type superclass;
    Class entityClass = null;
    while ((superclass = current.getGenericSuperclass()) != null) {
      if (superclass instanceof ParameterizedType) {
        entityClass = (Class) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        break;
      } else if (superclass instanceof Class) {
        current = (Class) superclass;
      } else {
        break;
      }
    }
    if (entityClass == null) {
      throw new IllegalStateException("DAO creation exception: Cannot determine entity type because "
              + getClass().getName() + " does not specify any type parameter.");
    }
    return entityClass.getClass();
  }

}