//______________________________________________________________________________
//   |       |
//   | knipp |           Knipp Medien und Kommunikation GmbH
//    -------
//
//
//     Knipp RRI Toolkit
//
//
//     Copyright (C) 2005 by Knipp Medien und Kommunikation GmbH
//
//     This library is free software; you can redistribute it and/or
//     modify it under the terms of the GNU Lesser General Public License 
//     as published by the Free Software Foundation; either version 2.1 
//     of the License, or any later version.
// 
//     This library is distributed in the hope that it will be useful, 
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
//     GNU Lesser General Public License for more details.
//
//     You should have received a copy of the GNU Lesser General
//     Public License along with this library; if not, write to the 
//     Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
//     Boston, MA 02111-1307 USA 
//
//
//     initial author:    Klaus Malorny
//
//     version:           1.3 (15112), 2011/05/27 16:00:14
//                        $Revision: 14936 $ 
//                        $Date: 2010-11-05 15:57:40 +0100 (Fri, 05 Nov 2010) $
//                        $Author: Klaus.Malorny $
//

package de.knipp.rritoolkit.response;


/**
 *  class that allows to match groups of error codes
 *
 *  @version  $Revision: 14936 $
 *  @author   Klaus Malorny
 */

public class GroupPattern implements ErrorPattern
{
  /** the accepted classes */

  private String[] classCodes;


  /** the accepted accesses */

  private String[] accessCodes;


  /** the accepted application codes */

  private String[] applicationCodes;


  /** the accepted components */

  private String[] componentCodes;


  /** the accepted error codes */

  private String[] errorCodes;



  /**
   *  empty constructor
   */

  public GroupPattern ()
  {
    // empty
  }


  /**
   *  constructor
   *
   *  @param classCode                the class code to test for; ignored if
   *                                  {@code null}
   *  @param accessCode               the access code to test for; ignored if
   *                                  {@code null}
   *  @param applicationCode          the application code to test for; 
   *                                  ignored if {@code null}
   *  @param componentCode            the component code to test for; ignored
   *                                  if {@code null}
   *  @param errorCode                the error code to test for; ignored if
   *                                  {@code null}
   */

  public GroupPattern (String classCode, String accessCode, 
    String applicationCode, String componentCode, String errorCode)
  {
    classCodes = classCode == null ? null : new String[] { classCode };
    accessCodes = accessCode == null ? null : new String[] { accessCode };

    applicationCodes = applicationCode == null ? 
      null : new String[] { applicationCode };

    componentCodes = componentCode == null ? 
      null : new String[] { componentCode };

    errorCodes = errorCode == null ? null : new String[] { errorCode };
  }


  /**
   *  set the class codes to match
   *
   *  @param codes                  the codes
   *  @return                       this instance
   */

  public GroupPattern setClassCodes (String... codes)
  {
    classCodes = codes;
    return this;
  }

  
  
  /**
   *  return the class codes
   *
   *  @return                       the codes, {@code null} if they are
   *                                not tested at all
   */
  
  public String[] getClassCodes ()
  {
    return classCodes;
  }
  
  
  

  /**
   *  set the access codes to match
   *
   *  @param codes                  the codes
   *  @return                       this instance
   */

  public GroupPattern setAccessCodes (String... codes)
  {
    accessCodes = codes;
    return this;
  }
  
  
  
  /**
   *  return the access codes
   *
   *  @return                       the codes, {@code null} if they are
   *                                not tested at all
   */
  
  public String[] getAccessCodes ()
  {
    return accessCodes;
  }


  
  /**
   *  set the application codes to match
   *
   *  @param codes                  the codes
   *  @return                       this instance
   */
  
  public GroupPattern setApplicationCodes (String... codes)
  {
    applicationCodes = codes;
    return this;
  }
  
  
  
  /**
   *  return the application codes
   *
   *  @return                       the codes, {@code null} if they are
   *                                not tested at all
   */
  
  public String[] getApplicationCodes ()
  {
    return applicationCodes;
  }
  
  
  /**
   *  set the component codes to match
   *
   *  @param codes                  the codes
   *  @return                       this instance
   */

  public GroupPattern setComponentCodes (String... codes)
  {
    componentCodes = codes;
    return this;
  }

  
  
  /**
   *  return the component codes
   *
   *  @return                       the codes, {@code null} if they are
   *                                not tested at all
   */
  
  public String[] getComponentCodes ()
  {
    return componentCodes;
  }


  /**
   *  set the error codes to match
   *
   *  @param codes                  the codes
   *  @return                       this instance
   */

  public GroupPattern setErrorCodes (String... codes)
  {
    errorCodes = codes;
    return this;
  }
  
  
  /**
   *  return the error codes to match
   *
   *  @return                       the codes, {@code null} if they are
   *                                not tested at all
   */
  
  public String[] getErrorCodes ()
  {
    return errorCodes;
  }

  //--- implementation of ErrorPattern ---------------------------------------

  /**
   *  test whether the given error code matches the pattern
   *
   *  @param code                the code to test, may be {@code null}
   *  @return                    {@code true} if it matches
   */

  @Override
  public boolean matches (ErrorCode code)
  {
    if (code == null) return false;

    if (classCodes != null)
    {
      boolean found = false;
      String value = code.getErrorClass ();

      for (String c: classCodes)
      {
        if (c.equals (value))
        {
          found = true;
          break;
        }
      }

      if (!found) return false;
    }

    if (accessCodes != null)
    {
      boolean found = false;
      String value = code.getInterfaceAccess ();

      for (String c: accessCodes)
      {
        if (c.equals (value))
        {
          found = true;
          break;
        }
      }

      if (!found) return false;
    }

    if (applicationCodes != null)
    {
      boolean found = false;
      String value = code.getInterfaceApplication ();

      for (String c: applicationCodes)
      {
        if (c.equals (value))
        {
          found = true;
          break;
        }
      }

      if (!found) return false;
    }

    if (componentCodes != null)
    {
      boolean found = false;
      String value = code.getComponent ();

      for (String c: componentCodes)
      {
        if (c.equals (value))
        {
          found = true;
          break;
        }
      }

      if (!found) return false;
    }

    if (errorCodes != null)
    {
      boolean found = false;
      String value = code.getErrorCode ();

      for (String c: errorCodes)
      {
        if (c.equals (value))
        {
          found = true;
          break;
        }
      }

      if (!found) return false;
    }

    return true;
  }

  
  /**
   *  accept a visitor
   *
   *  @param visitor             the visitor
   */

  @Override
  public void accept (ErrorPatternVisitor visitor)
  {
    visitor.visit (this);
  }



}  


