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

vuegwt.shaded.com.helger.css.decl.AbstractHasTopLevelRules Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (C) 2014-2018 Philip Helger (www.helger.com)
 * philip[at]helger[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.helger.css.decl;

import java.io.Serializable;
import java.util.function.Predicate;

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

import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.state.EChange;

/**
 * Abstract base class for all classes having top-level rules.
 *
 * @author Philip Helger
 * @since 5.0.5
 */
@NotThreadSafe
public abstract class AbstractHasTopLevelRules implements Serializable
{
  protected final ICommonsList  m_aRules = new CommonsArrayList <> ();

  public AbstractHasTopLevelRules ()
  {}

  /**
   * Check if any top-level rule. This method only considers top-level rules and
   * not @import and @namespace rules!
   *
   * @return true if at least one top-level rule is present,
   *         false if otherwise.
   */
  public boolean hasRules ()
  {
    return m_aRules.isNotEmpty ();
  }

  /**
   * Get the number of total contained top-level rules. This method only
   * considers top-level rules and not @import and
   * @namespace rules!
   *
   * @return The number of total contained top-level rules. Always ≥ 0.
   */
  @Nonnegative
  public int getRuleCount ()
  {
    return m_aRules.size ();
  }

  /**
   * Get the top-level rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   * @deprecated Use {@link #getRuleAtIndex(int)} instead
   */
  @Nullable
  @Deprecated
  public ICSSTopLevelRule getRule (@Nonnegative final int nIndex)
  {
    return getRuleAtIndex (nIndex);
  }

  /**
   * Get the top-level rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public ICSSTopLevelRule getRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndex (nIndex);
  }

  /**
   * Add a new top-level rule at the end. This method only considers top-level
   * rules and not @import and @namespace rules!
   *
   * @param aRule
   *        The rule to be added. May not be null.
   * @return this
   */
  @Nonnull
  public AbstractHasTopLevelRules addRule (@Nonnull final ICSSTopLevelRule aRule)
  {
    ValueEnforcer.notNull (aRule, "Rule");

    m_aRules.add (aRule);
    return this;
  }

  /**
   * Add a new top-level rule at the specified index. This method only considers
   * top-level rules and not @import and @namespace
   * rules!
   *
   * @param nIndex
   *        The index where the top-level rule should be added. Must be ≥ 0.
   * @param aRule
   *        The rule to be added. May not be null.
   * @return this
   */
  @Nonnull
  public AbstractHasTopLevelRules addRule (@Nonnegative final int nIndex, @Nonnull final ICSSTopLevelRule aRule)
  {
    ValueEnforcer.isGE0 (nIndex, "Index");
    ValueEnforcer.notNull (aRule, "Rule");

    if (nIndex >= getRuleCount ())
      m_aRules.add (aRule);
    else
      m_aRules.add (nIndex, aRule);
    return this;
  }

  /**
   * Remove the specified top-level rule. This method only considers top-level
   * rules and not @import and @namespace rules!
   *
   * @param aRule
   *        The rule to be removed. May be null.
   * @return {@link EChange#CHANGED} if the rule was successfully removed,
   *         {@link EChange#UNCHANGED} otherwise. Never null.
   */
  @Nonnull
  public EChange removeRule (@Nullable final ICSSTopLevelRule aRule)
  {
    return m_aRules.removeObject (aRule);
  }

  /**
   * Remove the rule at the specified index. This method only considers
   * top-level rules and not @import and @namespace
   * rules!
   *
   * @param nRuleIndex
   *        The index of the rule to be removed. Should be ≥ 0.
   * @return {@link EChange#CHANGED} if the rule at the specified index was
   *         successfully removed, {@link EChange#UNCHANGED} otherwise. Never
   *         null.
   */
  @Nonnull
  public EChange removeRule (@Nonnegative final int nRuleIndex)
  {
    return m_aRules.removeAtIndex (nRuleIndex);
  }

  /**
   * Remove all rules matching the passed predicate.
   *
   * @param aFilter
   *        The predicate to apply for deletion. May not be null.
   * @return {@link EChange#CHANGED} it at least one rule was removed,
   *         {@link EChange#UNCHANGED} otherwise.
   * @since 5.0.0
   */
  @Nonnull
  public EChange removeRules (@Nonnull final Predicate  aFilter)
  {
    return EChange.valueOf (m_aRules.removeIf (aFilter));
  }

  /**
   * Remove all top-level rules.
   *
   * @return {@link EChange#CHANGED} if any rule was removed,
   *         {@link EChange#UNCHANGED} otherwise. Never null.
   * @since 3.7.3
   */
  @Nonnull
  public EChange removeAllRules ()
  {
    return m_aRules.removeAll ();
  }

  /**
   * Get a copy of all contained top-level rules. This method only considers
   * top-level rules and not @import and @namespace
   * rules!
   *
   * @return A copy of all contained top-level rules. Never null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllRules ()
  {
    return m_aRules.getClone ();
  }

  /**
   * Get a copy of all contained top-level rules. This method only considers
   * top-level rules and not @import and @namespace
   * rules!
   *
   * @param aFilter
   *        The predicate to be applied
   * @return A copy of all contained top-level rules. Never null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllRules (@Nonnull final Predicate  aFilter)
  {
    return m_aRules.getAll (aFilter);
  }

  /**
   * Check if at least one of the top-level rules is a style rule (implementing
   * {@link CSSStyleRule}).
   *
   * @return true if at least one style rule is contained,
   *         false otherwise.
   */
  public boolean hasStyleRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSStyleRule);
  }

  /**
   * Get the number of top-level rules that are style rules (implementing
   * {@link CSSStyleRule}).
   *
   * @return The number of contained style rules. Always ≥ 0.
   */
  @Nonnegative
  public int getStyleRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSStyleRule);
  }

  /**
   * Get the style rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getStyleRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSStyleRule getStyleRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSStyleRule, nIndex, r -> (CSSStyleRule) r);
  }

  /**
   * Get a list of all top-level rules that are style rules (implementing
   * {@link CSSStyleRule}).
   *
   * @return A copy of all contained style rules. Never null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllStyleRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSStyleRule, r -> (CSSStyleRule) r);
  }

  /**
   * Check if at least one of the top-level rules is a page rule (implementing
   * {@link CSSPageRule}).
   *
   * @return true if at least one @page rule is
   *         contained, false otherwise.
   */
  public boolean hasPageRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSPageRule);
  }

  /**
   * Get the number of top-level rules that are page rules (implementing
   * {@link CSSPageRule}).
   *
   * @return The number of contained @page rules. Always ≥ 0.
   */
  @Nonnegative
  public int getPageRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSPageRule);
  }

  /**
   * Get the @page rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getPageRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSPageRule getPageRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSPageRule, nIndex, r -> (CSSPageRule) r);
  }

  /**
   * Get a list of all top-level rules that are page rules (implementing
   * {@link CSSPageRule}).
   *
   * @return A copy of all contained @page rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllPageRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSPageRule, r -> (CSSPageRule) r);
  }

  /**
   * Check if at least one of the top-level rules is a media rule (implementing
   * {@link CSSMediaRule}).
   *
   * @return true if at least one @media rule is
   *         contained, false otherwise.
   */
  public boolean hasMediaRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSMediaRule);
  }

  /**
   * Get the number of top-level rules that are media rules (implementing
   * {@link CSSMediaRule}).
   *
   * @return The number of contained @media rules. Always ≥ 0.
   */
  @Nonnegative
  public int getMediaRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSMediaRule);
  }

  /**
   * Get the @media rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getMediaRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSMediaRule getMediaRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSMediaRule, nIndex, r -> (CSSMediaRule) r);
  }

  /**
   * Get a list of all top-level rules that are media rules (implementing
   * {@link CSSMediaRule}).
   *
   * @return A copy of all contained @media rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllMediaRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSMediaRule, r -> (CSSMediaRule) r);
  }

  /**
   * Check if at least one of the top-level rules is a font-face rule
   * (implementing {@link CSSFontFaceRule}).
   *
   * @return true if at least one @font-face rule is
   *         contained, false otherwise.
   */
  public boolean hasFontFaceRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSFontFaceRule);
  }

  /**
   * Get the number of top-level rules that are font-face rules (implementing
   * {@link CSSFontFaceRule}).
   *
   * @return The number of contained @font-face rules. Always ≥
   *         0.
   */
  @Nonnegative
  public int getFontFaceRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSFontFaceRule);
  }

  /**
   * Get the @font-face rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getFontFaceRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSFontFaceRule getFontFaceRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSFontFaceRule, nIndex, r -> (CSSFontFaceRule) r);
  }

  /**
   * Get a list of all top-level rules that are font-face rules (implementing
   * {@link CSSFontFaceRule}).
   *
   * @return A copy of all contained @font-face rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllFontFaceRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSFontFaceRule, r -> (CSSFontFaceRule) r);
  }

  /**
   * Check if at least one of the top-level rules is a keyframes rule
   * (implementing {@link CSSKeyframesRule}).
   *
   * @return true if at least one @keyframes rule is
   *         contained, false otherwise.
   */
  public boolean hasKeyframesRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSKeyframesRule);
  }

  /**
   * Get the number of top-level rules that are keyframes rules (implementing
   * {@link CSSKeyframesRule}).
   *
   * @return The number of contained @keyframes rules. Always ≥
   *         0.
   */
  @Nonnegative
  public int getKeyframesRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSKeyframesRule);
  }

  /**
   * Get the @keyframes rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getKeyframesRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSKeyframesRule getKeyframesRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSKeyframesRule, nIndex, r -> (CSSKeyframesRule) r);
  }

  /**
   * Get a list of all top-level rules that are keyframes rules (implementing
   * {@link CSSKeyframesRule}).
   *
   * @return A copy of all contained @keyframes rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllKeyframesRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSKeyframesRule, r -> (CSSKeyframesRule) r);
  }

  /**
   * Check if at least one of the top-level rules is a viewport rule
   * (implementing {@link CSSViewportRule}).
   *
   * @return true if at least one @viewport rule is
   *         contained, false otherwise.
   */
  public boolean hasViewportRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSViewportRule);
  }

  /**
   * Get the number of top-level rules that are viewport rules (implementing
   * {@link CSSViewportRule}).
   *
   * @return The number of contained @viewport rules. Always ≥
   *         0.
   */
  @Nonnegative
  public int getViewportRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSViewportRule);
  }

  /**
   * Get the @viewport rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getViewportRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSViewportRule getViewportRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSViewportRule, nIndex, r -> (CSSViewportRule) r);
  }

  /**
   * Get a list of all top-level rules that are viewport rules (implementing
   * {@link CSSViewportRule}).
   *
   * @return A copy of all contained @viewport rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllViewportRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSViewportRule, r -> (CSSViewportRule) r);
  }

  /**
   * Check if at least one of the top-level rules is a supports rule
   * (implementing {@link CSSSupportsRule}).
   *
   * @return true if at least one @supports rule is
   *         contained, false otherwise.
   */
  public boolean hasSupportsRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSSupportsRule);
  }

  /**
   * Get the number of top-level rules that are support rules (implementing
   * {@link CSSSupportsRule}).
   *
   * @return The number of contained @supports rules. Always ≥
   *         0.
   */
  @Nonnegative
  public int getSupportsRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSSupportsRule);
  }

  /**
   * Get the @supports rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getSupportsRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSSupportsRule getSupportsRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSSupportsRule, nIndex, r -> (CSSSupportsRule) r);
  }

  /**
   * Get a list of all top-level rules that are support rules (implementing
   * {@link CSSSupportsRule}).
   *
   * @return A copy of all contained @supports rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllSupportsRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSSupportsRule, r -> (CSSSupportsRule) r);
  }

  /**
   * Check if at least one of the top-level rules is an unknown rule
   * (implementing {@link CSSUnknownRule}).
   *
   * @return true if at least one unknown @ rule is
   *         contained, false otherwise.
   */
  public boolean hasUnknownRules ()
  {
    return m_aRules.containsAny (r -> r instanceof CSSUnknownRule);
  }

  /**
   * Get the number of top-level rules that are unknown rules (implementing
   * {@link CSSUnknownRule}).
   *
   * @return The number of contained unknown @ rules. Always ≥
   *         0.
   */
  @Nonnegative
  public int getUnknownRuleCount ()
  {
    return m_aRules.getCount (r -> r instanceof CSSUnknownRule);
  }

  /**
   * Get the unknown rule at the specified index.
   *
   * @param nIndex
   *        The index to be resolved. Should be ≥ 0 and <
   *        {@link #getUnknownRuleCount()}.
   * @return null if an invalid index was specified.
   * @since 3.7.4
   */
  @Nullable
  public CSSUnknownRule getUnknownRuleAtIndex (@Nonnegative final int nIndex)
  {
    return m_aRules.getAtIndexMapped (r -> r instanceof CSSUnknownRule, nIndex, r -> (CSSUnknownRule) r);
  }

  /**
   * Get a list of all top-level rules that are unknown rules (implementing
   * {@link CSSUnknownRule}).
   *
   * @return A copy of all contained unknown @ rules. Never
   *         null.
   */
  @Nonnull
  @ReturnsMutableCopy
  public ICommonsList  getAllUnknownRules ()
  {
    return m_aRules.getAllMapped (r -> r instanceof CSSUnknownRule, r -> (CSSUnknownRule) r);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy