001/*******************************************************************************
002 * Copyright (c) 2024, 2026, Olivier Ayache.  All rights reserved.
003 *
004 * This file is part of AVPKit.
005 *
006 * AVPKit is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * AVPKit is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with AVPKit.  If not, see <http://www.gnu.org/licenses/>.
018 *******************************************************************************/
019package com.avpkit.mediatool;
020
021import java.util.Collection;
022import java.util.Collections;
023import java.util.concurrent.CopyOnWriteArrayList;
024
025/**
026 * An abstract implementation of all
027 * {@link IMediaGenerator} methods, but does not declare
028 *  {@link IMediaGenerator}.
029 *  
030 * <p>
031 * 
032 * This class manages all attached {@link IMediaListener} objects in a
033 * thread-safe set. The is fast to iterate over -- at the expense of a copy on
034 * {@link #addListener(IMediaListener)} and
035 * {@link #removeListener(IMediaListener)}.
036 * 
037 * </p>
038 * 
039 * <p>
040 * 
041 * Mixin classes can be extended by anyone, but the extending class
042 * gets to decide which, if any, of the interfaces they actually
043 * want to support.
044 * 
045 * </p>
046 * 
047 * @author trebor
048 * @author aclarke
049 *
050 */
051
052public abstract class AMediaGeneratorMixin
053{
054
055  private final Collection<IMediaListener> mListeners = new CopyOnWriteArrayList<IMediaListener>();
056
057  /**
058   * Create an {@link AMediaGeneratorMixin}.
059   */
060  public AMediaGeneratorMixin()
061  {
062    super();
063  }
064
065  /**
066   * Adds this listener to a thread-safe set.
067   * 
068   * @return true if the set was modified when adding this call.
069   */
070  public boolean addListener(IMediaListener listener)
071  {
072    return mListeners.add(listener);
073  }
074
075  /**
076   * Remove this listener from the thread-safe set of {@link IMediaListener} objects.
077   * 
078   * @return true if the set was modified by this call.
079   */
080  public boolean removeListener(IMediaListener listener)
081  {
082    return mListeners.remove(listener);
083  }
084
085  /**
086   * Get a read-only collection of added {@link IMediaListener} objects.
087   * 
088   * @return a read-only collection of {@link IMediaListener} objects.
089   */
090  public Collection<IMediaListener> getListeners()
091  {
092    return Collections.unmodifiableCollection(mListeners);
093  }
094
095}