benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/Filter.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 24 Jan 2016 12:08:45 +0100
changeset 1854 826eb936c9a8
permissions -rw-r--r--
The sieve benchmark makes the testing more real than the fake matrix multiplication
jaroslav@1854
     1
/**
jaroslav@1854
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1854
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1854
     4
 *
jaroslav@1854
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1854
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1854
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1854
     8
 *
jaroslav@1854
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1854
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1854
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1854
    12
 * GNU General Public License for more details.
jaroslav@1854
    13
 *
jaroslav@1854
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1854
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1854
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1854
    17
 */
jaroslav@1854
    18
package org.apidesign.benchmark.sieve;
jaroslav@1854
    19
jaroslav@1854
    20
final class Filter {
jaroslav@1854
    21
    private final int number;
jaroslav@1854
    22
    private final Filter next;
jaroslav@1854
    23
jaroslav@1854
    24
    public Filter(int number, Filter next) {
jaroslav@1854
    25
        this.number = number;
jaroslav@1854
    26
        this.next = next;
jaroslav@1854
    27
    }
jaroslav@1854
    28
jaroslav@1854
    29
    public boolean accept(int n) {
jaroslav@1854
    30
        Filter filter = this;
jaroslav@1854
    31
        for (;;) {
jaroslav@1854
    32
            if (n % filter.number == 0) {
jaroslav@1854
    33
                return false;
jaroslav@1854
    34
            }
jaroslav@1854
    35
            filter = filter.next;
jaroslav@1854
    36
            if (filter == null) {
jaroslav@1854
    37
                return true;
jaroslav@1854
    38
            }
jaroslav@1854
    39
        }
jaroslav@1854
    40
    }
jaroslav@1854
    41
}