001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, 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 v1.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 */
014package ch.qos.logback.classic.net;
015
016import ch.qos.logback.core.boolex.EvaluationException;
017import ch.qos.logback.core.boolex.EventEvaluator;
018import ch.qos.logback.core.spi.ContextAwareBase;
019
020/**
021 * A simple EventEvaluator implementation that triggers email transmission after
022 * a given number of events occur, regardless of event level.
023 * 
024 * <p>By default, the limit is 1024.
025 */
026public class CounterBasedEvaluator extends ContextAwareBase implements EventEvaluator<Object> {
027
028    static int DEFAULT_LIMIT = 1024;
029    int limit = DEFAULT_LIMIT;
030    int counter = 0;
031    String name;
032    boolean started;
033
034    public boolean evaluate(Object event) throws NullPointerException, EvaluationException {
035        counter++;
036
037        if (counter == limit) {
038            counter = 0;
039            return true;
040        } else {
041            return false;
042        }
043    }
044
045    public String getName() {
046        return name;
047    }
048
049    public void setName(String name) {
050        this.name = name;
051    }
052
053    public boolean isStarted() {
054        return started;
055    }
056
057    public void start() {
058        started = true;
059    }
060
061    public void stop() {
062        started = false;
063    }
064
065    public int getLimit() {
066        return limit;
067    }
068
069    public void setLimit(int limit) {
070        this.limit = limit;
071    }
072
073}