001/*
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v2.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014
015package ch.qos.logback.access.common.boolex;
016
017import ch.qos.logback.access.common.spi.IAccessEvent;
018import ch.qos.logback.core.boolex.EvaluationException;
019import ch.qos.logback.core.boolex.EventEvaluatorBase;
020
021/**
022 * A very simple {@link ch.qos.logback.core.boolex.EventEvaluator EventEvaluator}
023 * which checks whether the status of {@link IAccessEvent access event} is equal
024 * to a status code given as a parameter.
025 *
026 * @since 2.0.6
027 */
028public class StatusCodeEventEvaluator extends EventEvaluatorBase<IAccessEvent> {
029
030    final static int UNSET = -1;
031    int statusCode = UNSET;
032
033    @Override
034    public void start() {
035        if(statusCode == UNSET) {
036            addWarn("No status code set");
037            return;
038        }
039        super.start();
040    }
041    @Override
042    public boolean evaluate(IAccessEvent iAccessEvent) throws NullPointerException, EvaluationException {
043        if (!isStarted()) {
044            throw new IllegalStateException("Evaluator [" + this + "] was called in stopped state");
045        }
046        boolean result = (iAccessEvent.getStatusCode() == statusCode);
047        return result;
048    }
049
050    public int getStatusCode() {
051        return statusCode;
052    }
053
054    public void setStatusCode(int statusCode) {
055        this.statusCode = statusCode;
056    }
057}