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

com.phloc.css.media.CSSMediaList Maven / Gradle / Ivy

There is a newer version: 3.7.7
Show newest version
/**
 * Copyright (C) 2006-2014 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * Licensed under the Apache 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://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 com.phloc.css.media;

import java.io.Serializable;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

import com.phloc.commons.IHasSize;
import com.phloc.commons.annotations.ReturnsMutableCopy;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.hash.HashCodeGenerator;
import com.phloc.commons.state.EChange;
import com.phloc.commons.string.ToStringGenerator;

/**
 * Manages an ordered set of {@link ECSSMedium} objects.
 * 
 * @author Philip Helger
 */
@NotThreadSafe
public class CSSMediaList implements Serializable, IHasSize
{
  public static final String DEFAULT_MEDIA_STRING_SEPARATOR = ", ";

  // Ordered but unique
  private final Set  m_aMedia = new LinkedHashSet  ();

  /**
   * Constructor
   */
  public CSSMediaList ()
  {}

  /**
   * Constructor with a single medium
   * 
   * @param eMedium
   *        The medium to be added. May not be null.
   */
  public CSSMediaList (@Nonnull final ECSSMedium eMedium)
  {
    addMedium (eMedium);
  }

  /**
   * Constructor with an array of media.
   * 
   * @param aMedia
   *        The array of media to be added. The array may be null
   *        but it may not contain null elements.
   */
  public CSSMediaList (@Nullable final ECSSMedium... aMedia)
  {
    if (aMedia != null)
      for (final ECSSMedium eMedium : aMedia)
        addMedium (eMedium);
  }

  /**
   * Constructor with a collection of media.
   * 
   * @param aMedia
   *        The collection of media to be added. The collection may be
   *        null but it may not contain null elements.
   */
  public CSSMediaList (@Nullable final Iterable  aMedia)
  {
    if (aMedia != null)
      for (final ECSSMedium eMedium : aMedia)
        addMedium (eMedium);
  }

  /**
   * Add a new medium to the list
   * 
   * @param eMedium
   *        The medium to be added. May not be null.
   * @return this
   */
  @Nonnull
  public CSSMediaList addMedium (@Nonnull final ECSSMedium eMedium)
  {
    if (eMedium == null)
      throw new NullPointerException ("medium");

    m_aMedia.add (eMedium);
    return this;
  }

  /**
   * Remove the passed medium
   * 
   * @param eMedium
   *        The medium to be removed. May be null.
   * @return {@link EChange#CHANGED} if the medium was removed,
   *         {@link EChange#UNCHANGED} otherwise.
   */
  @Nonnull
  public EChange removeMedium (@Nullable final ECSSMedium eMedium)
  {
    return EChange.valueOf (m_aMedia.remove (eMedium));
  }

  /**
   * Remove all media.
   * 
   * @return {@link EChange#CHANGED} if any medium was removed,
   *         {@link EChange#UNCHANGED} otherwise. Never null.
   * @since 3.7.3
   */
  @Nonnull
  public EChange removeAllMedia ()
  {
    if (m_aMedia.isEmpty ())
      return EChange.UNCHANGED;
    m_aMedia.clear ();
    return EChange.CHANGED;
  }

  /**
   * @return The number of contained media. Always ≥ 0.
   */
  @Nonnegative
  public int getMediaCount ()
  {
    return m_aMedia.size ();
  }

  /**
   * @return true if any explicit media is defined,
   *         false if not.
   */
  public boolean hasAnyMedia ()
  {
    return !m_aMedia.isEmpty ();
  }

  /**
   * @return true if no explicit media is defined,
   *         false if a media is defined.
   */
  public boolean hasNoMedia ()
  {
    return m_aMedia.isEmpty ();
  }

  /**
   * @return true if no explicit media is defined or if
   *         {@link ECSSMedium#ALL} is contained.
   */
  public boolean hasNoMediaOrAll ()
  {
    return hasNoMedia () || containsMedium (ECSSMedium.ALL);
  }

  /**
   * Check if the passed medium is explicitly specified
   * 
   * @param eMedium
   *        The medium to be checked. May be null.
   * @return true if it is contained, false otherwise
   */
  public boolean containsMedium (@Nullable final ECSSMedium eMedium)
  {
    return m_aMedia.contains (eMedium);
  }

  /**
   * Check if the passed medium or the {@link ECSSMedium#ALL} is explicitly
   * specified
   * 
   * @param eMedium
   *        The medium to be checked. May be null.
   * @return true if the passed medium or the "all" medium is
   *         contained, false otherwise
   */
  public boolean containsMediumOrAll (@Nullable final ECSSMedium eMedium)
  {
    // Either the specific medium is contained, or the "all" medium is contained
    return containsMedium (eMedium) || containsMedium (ECSSMedium.ALL);
  }

  /**
   * Check if the passed medium is usable for the screen. This is the case if
   * either the "screen" medium, the "all" medium or no medium at all is
   * contained.
   * 
   * @return true if the media list is usable for screen display
   */
  public boolean isForScreen ()
  {
    // Default is "screen" if none is provided
    return hasNoMedia () || containsMediumOrAll (ECSSMedium.SCREEN);
  }

  /**
   * @return A copy of all specified media. Never null but maybe
   *         empty.
   */
  @Nonnull
  @ReturnsMutableCopy
  public Set  getAllMedia ()
  {
    return ContainerHelper.newSet (m_aMedia);
  }

  /**
   * @return A non-null but maybe empty String with all media in
   *         the order they where inserted and separated by
   *         {@value #DEFAULT_MEDIA_STRING_SEPARATOR}
   * @see #getMediaString(String)
   */
  @Nonnull
  public String getMediaString ()
  {
    return getMediaString (DEFAULT_MEDIA_STRING_SEPARATOR);
  }

  /**
   * @param sSeparator
   *        The separator to be used. May not be null.
   * @return A non-null but maybe empty String with all media in
   *         the order they where inserted and separated by a ", "
   */
  @Nonnull
  public String getMediaString (@Nonnull final String sSeparator)
  {
    if (sSeparator == null)
      throw new NullPointerException ("separator");

    if (m_aMedia.isEmpty ())
      return "";

    final StringBuilder aSB = new StringBuilder ();
    for (final ECSSMedium eMedia : m_aMedia)
    {
      if (aSB.length () > 0)
        aSB.append (sSeparator);
      aSB.append (eMedia.getName ());
    }
    return aSB.toString ();
  }

  @Nonnegative
  public int size ()
  {
    return getMediaCount ();
  }

  public boolean isEmpty ()
  {
    return hasNoMedia ();
  }

  @Override
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (o == null || !getClass ().equals (o.getClass ()))
      return false;
    final CSSMediaList rhs = (CSSMediaList) o;
    return m_aMedia.equals (rhs.m_aMedia);
  }

  @Override
  public int hashCode ()
  {
    return new HashCodeGenerator (this).append (m_aMedia).getHashCode ();
  }

  @Override
  public String toString ()
  {
    return new ToStringGenerator (this).append ("media", m_aMedia).toString ();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy