`
kanpiaoxue
  • 浏览: 1782050 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

简单的filter过滤实现类

 
阅读更多

 

 

 

/**
 * <pre>
 * Copyright kanpiaoxue.org [2000-2016]
 * </pre>
 */
package org.kanpiaoxue.test;

import org.apache.commons.lang3.StringUtils;

import com.google.common.base.Preconditions;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Lists;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <pre>
 * FilterTest.java
 * @author kanpiaoxue<br>
 * @version 1.0
 * Create Time 2016年8月10日 下午4:39:42<br>
 * Description : 简单的filter过滤实现类
 * 设计思路:
 *      通过统一的过滤器接口提供各种过滤服务实现类。
 *      如果过滤过程中出现不满足条件的元素需要抛出指定的异常,没有抛出异常的原始就是通过过滤条件的元素。
 *      过滤的执行顺序取决于该过滤器在过滤器列表中的位置。
 *      不符合过滤条件的元素被抛弃的时候可以打印出“为什么不能通过过滤器的原因”,方便进行debug跟踪。
 * </pre>
 */
public class FilterTest {

    abstract class AbstractSchoolPerson implements Person {
        protected String name;
        protected int age;
        protected City city;
        protected Profession profession;

        /**
         * <pre>
         * @param name
         * @param age
         * @param city
         * @param profession
         * </pre>
         */
        public AbstractSchoolPerson(String name, int age, City city) {
            super();
            this.name = name;
            this.age = age;
            this.city = city;
        }

        /**
         * <pre>
         * @return the age
         * </pre>
         */
        @Override
        public int getAge() {
            return age;
        }

        /**
         * <pre>
         * @return the city
         * </pre>
         */
        @Override
        public City getCity() {
            return city;
        }

        /**
         * <pre>
         * @return the name
         * </pre>
         */
        @Override
        public String getName() {
            return name;
        }

        /*
         * (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", city=" + city + ", profession="
                    + getProfession() + "]";
        }

    }

    class AgeFilter implements ConditionFilter {

        private int minAge;
        private int maxAge;

        /**
         * <pre>
         * @param minAge
         * @param maxAge
         * </pre>
         */
        public AgeFilter(int minAge, int maxAge) {
            super();
            this.minAge = minAge;
            this.maxAge = maxAge;
        }

        /*
         * (non-Javadoc)
         * @see
         * org.kanpiaoxue.test.FilterTest.Filter#filter(org.kanpiaoxue.test.
         * FilterTest.Person)
         */
        @Override
        public void filter(Person person) throws StopException {
            Preconditions.checkNotNull(person);
            int age = person.getAge();
            if (age < minAge || age > maxAge) {
                throw new StopException(new KeyValue<Integer, String>(ERROR_CODE,
                        String.format("%s's age is %s. It's out of the range[%s-%s]", person.getName(), age,
                                minAge, maxAge)));
            }
        }
    }

    enum City {
        BEIJING, TIANJIN, SHANGHAI, NANJING
    }

    class CityFilter implements ConditionFilter {
        private City city;

        /**
         * <pre>
         * @param city
         * </pre>
         */
        public CityFilter(City city) {
            super();
            this.city = city;
        }

        /*
         * (non-Javadoc)
         * @see
         * org.kanpiaoxue.test.FilterTest.Filter#filter(org.kanpiaoxue.test.
         * FilterTest.Person)
         */
        @Override
        public void filter(Person person) throws StopException {
            Preconditions.checkNotNull(person);
            City city = person.getCity();
            if (!this.city.equals(city)) {
                throw new StopException(new KeyValue<Integer, String>(ERROR_CODE, String.format(
                        "%s does not live in %s. He lives in %s", person.getName(), this.city, city)));
            }
        }
    }

    interface ConditionFilter {
        void filter(Person person) throws StopException;
    }

    class Key<K> implements Serializable {
        /**
         * <pre>
         * </pre>
         */

        private static final long serialVersionUID = 7532585206735018941L;
        protected K key;

        /**
         * <pre>
         * </pre>
         */
        public Key() {
            super();
        }

        /**
         * <pre>
         * @param key
         * </pre>
         */
        public Key(K key) {
            super();
            this.key = key;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            @SuppressWarnings("rawtypes")
            Key other = (Key) obj;
            if (key == null) {
                if (other.key != null) {
                    return false;
                }
            } else if (!key.equals(other.key)) {
                return false;
            }
            return true;
        }

        public K getKey() {
            return key;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((key == null) ? 0 : key.hashCode());
            return result;
        }

        public void setKey(K key) {
            this.key = key;
        }

        @Override
        public String toString() {
            return "Key [key=" + key + "]";
        }
    }

    class KeyValue<K, V> extends Key<K> {
        /**
         * <pre>
         * </pre>
         */

        private static final long serialVersionUID = -5805488471492686888L;
        private V value;

        /**
         * <pre>
         * </pre>
         */
        public KeyValue() {
            super();
        }

        /**
         * <pre>
         * @param key
         * </pre>
         */
        public KeyValue(K key) {
            super(key);
        }

        /**
         * <pre>
         * @param key
         * @param value
         * </pre>
         */
        public KeyValue(K key, V value) {
            super();
            this.key = key;
            this.value = value;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            @SuppressWarnings("rawtypes")
            KeyValue other = (KeyValue) obj;
            if (key == null) {
                if (other.key != null) {
                    return false;
                }
            } else if (!key.equals(other.key)) {
                return false;
            }
            if (value == null) {
                if (other.value != null) {
                    return false;
                }
            } else if (!value.equals(other.value)) {
                return false;
            }
            return true;
        }

        @Override
        public K getKey() {
            return key;
        }

        public V getValue() {
            return value;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((key == null) ? 0 : key.hashCode());
            result = prime * result + ((value == null) ? 0 : value.hashCode());
            return result;
        }

        @Override
        public void setKey(K key) {
            this.key = key;
        }

        public void setValue(V value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "KeyValue [key=" + key + ", value=" + value + "]";
        }

    }

    class NameFilter implements ConditionFilter {
        private final Pattern pattern;

        /**
         * <pre>
         * @param pattern
         * </pre>
         */
        public NameFilter(String regex) {
            super();
            Preconditions.checkArgument(StringUtils.isNotBlank(regex));
            this.pattern = Pattern.compile(regex);
        }

        /*
         * (non-Javadoc)
         * @see
         * org.kanpiaoxue.test.FilterTest.Filter#filter(org.kanpiaoxue.test.
         * FilterTest.Person)
         */
        @Override
        public void filter(Person person) throws StopException {
            Preconditions.checkNotNull(person);
            String name = person.getName();
            Matcher m = this.pattern.matcher(name);
            if (!m.find()) {
                throw new StopException(new KeyValue<Integer, String>(ERROR_CODE, String.format(
                        "name:%s does not match expression : %s", name, this.pattern)));
            }
        }
    }

    interface Person {
        int getAge();

        String getName();

        City getCity();

        Profession getProfession();
    }

    enum Profession {
        TEACHER, STUDENT, POLICE
    }

    class ProfessionFilter implements ConditionFilter {
        private Profession profession;

        /**
         * <pre>
         * @param profession
         * </pre>
         */
        public ProfessionFilter(Profession profession) {
            super();
            this.profession = profession;
        }

        /*
         * (non-Javadoc)
         * @see
         * org.kanpiaoxue.test.FilterTest.ConditionFilter#filter(org.kanpiaoxue
         * .test.FilterTest.Person)
         */
        @Override
        public void filter(Person person) throws StopException {
            Preconditions.checkNotNull(person);
            Profession profession = person.getProfession();
            if (!this.profession.equals(profession)) {
                throw new StopException(new KeyValue<Integer, String>(ERROR_CODE, String.format(
                        "%s is a %s, not is a %s", person.getName(), profession, this.profession)));
            }

        }

    }

    class StopException extends Exception {

        /**
         * <pre>
         * </pre>
         */

        private static final long serialVersionUID = -4469063439657658711L;

        private KeyValue<Integer, String> result;

        public StopException() {
            super();
        }

        public StopException(KeyValue<Integer, String> result) {
            super();
            this.result = result;
        }

        public StopException(String message) {
            super(message);
        }

        /**
         * <pre>
         * @return the result
         * </pre>
         */
        public KeyValue<Integer, String> getResult() {
            return result;
        }

    }

    class Student extends AbstractSchoolPerson {

        /**
         * <pre>
         * @param name
         * @param age
         * @param city
         * </pre>
         */
        public Student(String name, int age, City city) {
            super(name, age, city);
        }

        /*
         * (non-Javadoc)
         * @see org.kanpiaoxue.test.FilterTest.Person#getProfession()
         */
        @Override
        public Profession getProfession() {
            return Profession.STUDENT;
        }
    }

    class Teacher extends AbstractSchoolPerson {

        /**
         * <pre>
         * @param name
         * @param age
         * @param city
         * </pre>
         */
        public Teacher(String name, int age, City city) {
            super(name, age, city);
        }

        /*
         * (non-Javadoc)
         * @see org.kanpiaoxue.test.FilterTest.Person#getProfession()
         */
        @Override
        public Profession getProfession() {
            return Profession.TEACHER;
        }
    }

    private static final int ERROR_CODE = 0;

    /**
     * <pre>
     * @param args
     * </pre>
     */
    public static void main(String[] args) {
        FilterTest test = new FilterTest();

        // 形成测试数据
        int size = 200;
        List<Person> list = Lists.newLinkedList();
        for (int i = 0; i < size; i++) {
            Person p = null;
            boolean flag = i % 2 == 0;
            City city = flag ? City.BEIJING : City.SHANGHAI;
            if (flag) {
                p = test.new Teacher("teacher-" + i, i % 35 + 20, city);
            } else {
                p = test.new Student("student-" + i, i % 15 + 10, City.BEIJING);
            }
            list.add(p);
        }

        // 构建过滤器
        List<ConditionFilter> filters = Lists.newArrayList();
        filters.add(test.new AgeFilter(10, 20));
        filters.add(test.new CityFilter(City.BEIJING));
        filters.add(test.new NameFilter("t"));
        filters.add(test.new ProfessionFilter(Profession.TEACHER));

        // 执行过滤动作
        List<Person> leftList = Lists.newLinkedList();
        for (Person person : list) {
            try {
                Stopwatch w = Stopwatch.createStarted();
                for (ConditionFilter f : filters) {
                    f.filter(person);
                }
                System.out.println(String.format("a person passed thougth all filters consumes %s.", w));
                leftList.add(person);
            } catch (StopException e) {
                KeyValue<Integer, String> result = e.getResult();
                if (ERROR_CODE == result.getKey().intValue()) {
                    System.err.println(String.format("remove person : %s , because %s", person,
                            result.getValue()));
                }
            }
        }

        // 打印过滤结果
        System.out.println();
        System.out.println(String.format("There are %s person left though the filters.", leftList.size()));
        for (Person person : leftList) {
            System.out.println(String.format("left: %s", person));
        }

    }
}

 

输出:

写道
remove person : Person [name=student-1, age=11, city=BEIJING, profession=STUDENT] , because student-1 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-2, age=22, city=BEIJING, profession=TEACHER] , because teacher-2's age is 22. It's out of the range[10-20]
remove person : Person [name=student-3, age=13, city=BEIJING, profession=STUDENT] , because student-3 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-4, age=24, city=BEIJING, profession=TEACHER] , because teacher-4's age is 24. It's out of the range[10-20]
remove person : Person [name=student-5, age=15, city=BEIJING, profession=STUDENT] , because student-5 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-6, age=26, city=BEIJING, profession=TEACHER] , because teacher-6's age is 26. It's out of the range[10-20]
remove person : Person [name=student-7, age=17, city=BEIJING, profession=STUDENT] , because student-7 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-8, age=28, city=BEIJING, profession=TEACHER] , because teacher-8's age is 28. It's out of the range[10-20]
remove person : Person [name=student-9, age=19, city=BEIJING, profession=STUDENT] , because student-9 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-10, age=30, city=BEIJING, profession=TEACHER] , because teacher-10's age is 30. It's out of the range[10-20]
remove person : Person [name=student-11, age=21, city=BEIJING, profession=STUDENT] , because student-11's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-12, age=32, city=BEIJING, profession=TEACHER] , because teacher-12's age is 32. It's out of the range[10-20]
remove person : Person [name=student-13, age=23, city=BEIJING, profession=STUDENT] , because student-13's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-14, age=34, city=BEIJING, profession=TEACHER] , because teacher-14's age is 34. It's out of the range[10-20]
remove person : Person [name=student-15, age=10, city=BEIJING, profession=STUDENT] , because student-15 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-16, age=36, city=BEIJING, profession=TEACHER] , because teacher-16's age is 36. It's out of the range[10-20]
remove person : Person [name=student-17, age=12, city=BEIJING, profession=STUDENT] , because student-17 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-18, age=38, city=BEIJING, profession=TEACHER] , because teacher-18's age is 38. It's out of the range[10-20]
remove person : Person [name=student-19, age=14, city=BEIJING, profession=STUDENT] , because student-19 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-20, age=40, city=BEIJING, profession=TEACHER] , because teacher-20's age is 40. It's out of the range[10-20]
remove person : Person [name=student-21, age=16, city=BEIJING, profession=STUDENT] , because student-21 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-22, age=42, city=BEIJING, profession=TEACHER] , because teacher-22's age is 42. It's out of the range[10-20]
remove person : Person [name=student-23, age=18, city=BEIJING, profession=STUDENT] , because student-23 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-24, age=44, city=BEIJING, profession=TEACHER] , because teacher-24's age is 44. It's out of the range[10-20]
remove person : Person [name=student-25, age=20, city=BEIJING, profession=STUDENT] , because student-25 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-26, age=46, city=BEIJING, profession=TEACHER] , because teacher-26's age is 46. It's out of the range[10-20]
remove person : Person [name=student-27, age=22, city=BEIJING, profession=STUDENT] , because student-27's age is 22. It's out of the range[10-20]
remove person : Person [name=teacher-28, age=48, city=BEIJING, profession=TEACHER] , because teacher-28's age is 48. It's out of the range[10-20]
remove person : Person [name=student-29, age=24, city=BEIJING, profession=STUDENT] , because student-29's age is 24. It's out of the range[10-20]
remove person : Person [name=teacher-30, age=50, city=BEIJING, profession=TEACHER] , because teacher-30's age is 50. It's out of the range[10-20]
remove person : Person [name=student-31, age=11, city=BEIJING, profession=STUDENT] , because student-31 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-32, age=52, city=BEIJING, profession=TEACHER] , because teacher-32's age is 52. It's out of the range[10-20]
remove person : Person [name=student-33, age=13, city=BEIJING, profession=STUDENT] , because student-33 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-34, age=54, city=BEIJING, profession=TEACHER] , because teacher-34's age is 54. It's out of the range[10-20]
remove person : Person [name=student-35, age=15, city=BEIJING, profession=STUDENT] , because student-35 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-36, age=21, city=BEIJING, profession=TEACHER] , because teacher-36's age is 21. It's out of the range[10-20]
remove person : Person [name=student-37, age=17, city=BEIJING, profession=STUDENT] , because student-37 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-38, age=23, city=BEIJING, profession=TEACHER] , because teacher-38's age is 23. It's out of the range[10-20]
remove person : Person [name=student-39, age=19, city=BEIJING, profession=STUDENT] , because student-39 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-40, age=25, city=BEIJING, profession=TEACHER] , because teacher-40's age is 25. It's out of the range[10-20]
remove person : Person [name=student-41, age=21, city=BEIJING, profession=STUDENT] , because student-41's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-42, age=27, city=BEIJING, profession=TEACHER] , because teacher-42's age is 27. It's out of the range[10-20]
remove person : Person [name=student-43, age=23, city=BEIJING, profession=STUDENT] , because student-43's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-44, age=29, city=BEIJING, profession=TEACHER] , because teacher-44's age is 29. It's out of the range[10-20]
remove person : Person [name=student-45, age=10, city=BEIJING, profession=STUDENT] , because student-45 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-46, age=31, city=BEIJING, profession=TEACHER] , because teacher-46's age is 31. It's out of the range[10-20]
remove person : Person [name=student-47, age=12, city=BEIJING, profession=STUDENT] , because student-47 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-48, age=33, city=BEIJING, profession=TEACHER] , because teacher-48's age is 33. It's out of the range[10-20]
remove person : Person [name=student-49, age=14, city=BEIJING, profession=STUDENT] , because student-49 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-50, age=35, city=BEIJING, profession=TEACHER] , because teacher-50's age is 35. It's out of the range[10-20]
remove person : Person [name=student-51, age=16, city=BEIJING, profession=STUDENT] , because student-51 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-52, age=37, city=BEIJING, profession=TEACHER] , because teacher-52's age is 37. It's out of the range[10-20]
remove person : Person [name=student-53, age=18, city=BEIJING, profession=STUDENT] , because student-53 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-54, age=39, city=BEIJING, profession=TEACHER] , because teacher-54's age is 39. It's out of the range[10-20]
remove person : Person [name=student-55, age=20, city=BEIJING, profession=STUDENT] , because student-55 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-56, age=41, city=BEIJING, profession=TEACHER] , because teacher-56's age is 41. It's out of the range[10-20]
remove person : Person [name=student-57, age=22, city=BEIJING, profession=STUDENT] , because student-57's age is 22. It's out of the range[10-20]
remove person : Person [name=teacher-58, age=43, city=BEIJING, profession=TEACHER] , because teacher-58's age is 43. It's out of the range[10-20]
remove person : Person [name=student-59, age=24, city=BEIJING, profession=STUDENT] , because student-59's age is 24. It's out of the range[10-20]
remove person : Person [name=teacher-60, age=45, city=BEIJING, profession=TEACHER] , because teacher-60's age is 45. It's out of the range[10-20]
remove person : Person [name=student-61, age=11, city=BEIJING, profession=STUDENT] , because student-61 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-62, age=47, city=BEIJING, profession=TEACHER] , because teacher-62's age is 47. It's out of the range[10-20]
remove person : Person [name=student-63, age=13, city=BEIJING, profession=STUDENT] , because student-63 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-64, age=49, city=BEIJING, profession=TEACHER] , because teacher-64's age is 49. It's out of the range[10-20]
remove person : Person [name=student-65, age=15, city=BEIJING, profession=STUDENT] , because student-65 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-66, age=51, city=BEIJING, profession=TEACHER] , because teacher-66's age is 51. It's out of the range[10-20]
remove person : Person [name=student-67, age=17, city=BEIJING, profession=STUDENT] , because student-67 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-68, age=53, city=BEIJING, profession=TEACHER] , because teacher-68's age is 53. It's out of the range[10-20]
remove person : Person [name=student-69, age=19, city=BEIJING, profession=STUDENT] , because student-69 is a STUDENT, not is a TEACHER
remove person : Person [name=student-71, age=21, city=BEIJING, profession=STUDENT] , because student-71's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-72, age=22, city=BEIJING, profession=TEACHER] , because teacher-72's age is 22. It's out of the range[10-20]
remove person : Person [name=student-73, age=23, city=BEIJING, profession=STUDENT] , because student-73's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-74, age=24, city=BEIJING, profession=TEACHER] , because teacher-74's age is 24. It's out of the range[10-20]
remove person : Person [name=student-75, age=10, city=BEIJING, profession=STUDENT] , because student-75 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-76, age=26, city=BEIJING, profession=TEACHER] , because teacher-76's age is 26. It's out of the range[10-20]
remove person : Person [name=student-77, age=12, city=BEIJING, profession=STUDENT] , because student-77 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-78, age=28, city=BEIJING, profession=TEACHER] , because teacher-78's age is 28. It's out of the range[10-20]
remove person : Person [name=student-79, age=14, city=BEIJING, profession=STUDENT] , because student-79 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-80, age=30, city=BEIJING, profession=TEACHER] , because teacher-80's age is 30. It's out of the range[10-20]
remove person : Person [name=student-81, age=16, city=BEIJING, profession=STUDENT] , because student-81 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-82, age=32, city=BEIJING, profession=TEACHER] , because teacher-82's age is 32. It's out of the range[10-20]
remove person : Person [name=student-83, age=18, city=BEIJING, profession=STUDENT] , because student-83 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-84, age=34, city=BEIJING, profession=TEACHER] , because teacher-84's age is 34. It's out of the range[10-20]
remove person : Person [name=student-85, age=20, city=BEIJING, profession=STUDENT] , because student-85 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-86, age=36, city=BEIJING, profession=TEACHER] , because teacher-86's age is 36. It's out of the range[10-20]
remove person : Person [name=student-87, age=22, city=BEIJING, profession=STUDENT] , because student-87's age is 22. It's out of the range[10-20]
remove person : Person [name=teacher-88, age=38, city=BEIJING, profession=TEACHER] , because teacher-88's age is 38. It's out of the range[10-20]
remove person : Person [name=student-89, age=24, city=BEIJING, profession=STUDENT] , because student-89's age is 24. It's out of the range[10-20]
remove person : Person [name=teacher-90, age=40, city=BEIJING, profession=TEACHER] , because teacher-90's age is 40. It's out of the range[10-20]
remove person : Person [name=student-91, age=11, city=BEIJING, profession=STUDENT] , because student-91 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-92, age=42, city=BEIJING, profession=TEACHER] , because teacher-92's age is 42. It's out of the range[10-20]
remove person : Person [name=student-93, age=13, city=BEIJING, profession=STUDENT] , because student-93 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-94, age=44, city=BEIJING, profession=TEACHER] , because teacher-94's age is 44. It's out of the range[10-20]
remove person : Person [name=student-95, age=15, city=BEIJING, profession=STUDENT] , because student-95 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-96, age=46, city=BEIJING, profession=TEACHER] , because teacher-96's age is 46. It's out of the range[10-20]
remove person : Person [name=student-97, age=17, city=BEIJING, profession=STUDENT] , because student-97 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-98, age=48, city=BEIJING, profession=TEACHER] , because teacher-98's age is 48. It's out of the range[10-20]
a person passed thougth all filters consumes 73.34 ms.
a person passed thougth all filters consumes 193.6 μs.
remove person : Person [name=student-99, age=19, city=BEIJING, profession=STUDENT] , because student-99 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-100, age=50, city=BEIJING, profession=TEACHER] , because teacher-100's age is 50. It's out of the range[10-20]
remove person : Person [name=student-101, age=21, city=BEIJING, profession=STUDENT] , because student-101's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-102, age=52, city=BEIJING, profession=TEACHER] , because teacher-102's age is 52. It's out of the range[10-20]
remove person : Person [name=student-103, age=23, city=BEIJING, profession=STUDENT] , because student-103's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-104, age=54, city=BEIJING, profession=TEACHER] , because teacher-104's age is 54. It's out of the range[10-20]
remove person : Person [name=student-105, age=10, city=BEIJING, profession=STUDENT] , because student-105 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-106, age=21, city=BEIJING, profession=TEACHER] , because teacher-106's age is 21. It's out of the range[10-20]
remove person : Person [name=student-107, age=12, city=BEIJING, profession=STUDENT] , because student-107 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-108, age=23, city=BEIJING, profession=TEACHER] , because teacher-108's age is 23. It's out of the range[10-20]
remove person : Person [name=student-109, age=14, city=BEIJING, profession=STUDENT] , because student-109 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-110, age=25, city=BEIJING, profession=TEACHER] , because teacher-110's age is 25. It's out of the range[10-20]
remove person : Person [name=student-111, age=16, city=BEIJING, profession=STUDENT] , because student-111 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-112, age=27, city=BEIJING, profession=TEACHER] , because teacher-112's age is 27. It's out of the range[10-20]
remove person : Person [name=student-113, age=18, city=BEIJING, profession=STUDENT] , because student-113 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-114, age=29, city=BEIJING, profession=TEACHER] , because teacher-114's age is 29. It's out of the range[10-20]
remove person : Person [name=student-115, age=20, city=BEIJING, profession=STUDENT] , because student-115 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-116, age=31, city=BEIJING, profession=TEACHER] , because teacher-116's age is 31. It's out of the range[10-20]
remove person : Person [name=student-117, age=22, city=BEIJING, profession=STUDENT] , because student-117's age is 22. It's out of the range[10-20]
remove person : Person [name=teacher-118, age=33, city=BEIJING, profession=TEACHER] , because teacher-118's age is 33. It's out of the range[10-20]
remove person : Person [name=student-119, age=24, city=BEIJING, profession=STUDENT] , because student-119's age is 24. It's out of the range[10-20]
remove person : Person [name=teacher-120, age=35, city=BEIJING, profession=TEACHER] , because teacher-120's age is 35. It's out of the range[10-20]
remove person : Person [name=student-121, age=11, city=BEIJING, profession=STUDENT] , because student-121 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-122, age=37, city=BEIJING, profession=TEACHER] , because teacher-122's age is 37. It's out of the range[10-20]
remove person : Person [name=student-123, age=13, city=BEIJING, profession=STUDENT] , because student-123 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-124, age=39, city=BEIJING, profession=TEACHER] , because teacher-124's age is 39. It's out of the range[10-20]
remove person : Person [name=student-125, age=15, city=BEIJING, profession=STUDENT] , because student-125 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-126, age=41, city=BEIJING, profession=TEACHER] , because teacher-126's age is 41. It's out of the range[10-20]
remove person : Person [name=student-127, age=17, city=BEIJING, profession=STUDENT] , because student-127 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-128, age=43, city=BEIJING, profession=TEACHER] , because teacher-128's age is 43. It's out of the range[10-20]
remove person : Person [name=student-129, age=19, city=BEIJING, profession=STUDENT] , because student-129 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-130, age=45, city=BEIJING, profession=TEACHER] , because teacher-130's age is 45. It's out of the range[10-20]
remove person : Person [name=student-131, age=21, city=BEIJING, profession=STUDENT] , because student-131's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-132, age=47, city=BEIJING, profession=TEACHER] , because teacher-132's age is 47. It's out of the range[10-20]
remove person : Person [name=student-133, age=23, city=BEIJING, profession=STUDENT] , because student-133's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-134, age=49, city=BEIJING, profession=TEACHER] , because teacher-134's age is 49. It's out of the range[10-20]
remove person : Person [name=student-135, age=10, city=BEIJING, profession=STUDENT] , because student-135 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-136, age=51, city=BEIJING, profession=TEACHER] , because teacher-136's age is 51. It's out of the range[10-20]
remove person : Person [name=student-137, age=12, city=BEIJING, profession=STUDENT] , because student-137 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-138, age=53, city=BEIJING, profession=TEACHER] , because teacher-138's age is 53. It's out of the range[10-20]
remove person : Person [name=student-139, age=14, city=BEIJING, profession=STUDENT] , because student-139 is a STUDENT, not is a TEACHER
remove person : Person [name=student-141, age=16, city=BEIJING, profession=STUDENT] , because student-141 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-142, age=22, city=BEIJING, profession=TEACHER] , because teacher-142's age is 22. It's out of the range[10-20]
remove person : Person [name=student-143, age=18, city=BEIJING, profession=STUDENT] , because student-143 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-144, age=24, city=BEIJING, profession=TEACHER] , because teacher-144's age is 24. It's out of the range[10-20]
remove person : Person [name=student-145, age=20, city=BEIJING, profession=STUDENT] , because student-145 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-146, age=26, city=BEIJING, profession=TEACHER] , because teacher-146's age is 26. It's out of the range[10-20]
remove person : Person [name=student-147, age=22, city=BEIJING, profession=STUDENT] , because student-147's age is 22. It's out of the range[10-20]
remove person : Person [name=teacher-148, age=28, city=BEIJING, profession=TEACHER] , because teacher-148's age is 28. It's out of the range[10-20]
remove person : Person [name=student-149, age=24, city=BEIJING, profession=STUDENT] , because student-149's age is 24. It's out of the range[10-20]
remove person : Person [name=teacher-150, age=30, city=BEIJING, profession=TEACHER] , because teacher-150's age is 30. It's out of the range[10-20]
remove person : Person [name=student-151, age=11, city=BEIJING, profession=STUDENT] , because student-151 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-152, age=32, city=BEIJING, profession=TEACHER] , because teacher-152's age is 32. It's out of the range[10-20]
remove person : Person [name=student-153, age=13, city=BEIJING, profession=STUDENT] , because student-153 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-154, age=34, city=BEIJING, profession=TEACHER] , because teacher-154's age is 34. It's out of the range[10-20]
remove person : Person [name=student-155, age=15, city=BEIJING, profession=STUDENT] , because student-155 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-156, age=36, city=BEIJING, profession=TEACHER] , because teacher-156's age is 36. It's out of the range[10-20]
remove person : Person [name=student-157, age=17, city=BEIJING, profession=STUDENT] , because student-157 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-158, age=38, city=BEIJING, profession=TEACHER] , because teacher-158's age is 38. It's out of the range[10-20]
remove person : Person [name=student-159, age=19, city=BEIJING, profession=STUDENT] , because student-159 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-160, age=40, city=BEIJING, profession=TEACHER] , because teacher-160's age is 40. It's out of the range[10-20]
remove person : Person [name=student-161, age=21, city=BEIJING, profession=STUDENT] , because student-161's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-162, age=42, city=BEIJING, profession=TEACHER] , because teacher-162's age is 42. It's out of the range[10-20]
remove person : Person [name=student-163, age=23, city=BEIJING, profession=STUDENT] , because student-163's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-164, age=44, city=BEIJING, profession=TEACHER] , because teacher-164's age is 44. It's out of the range[10-20]
remove person : Person [name=student-165, age=10, city=BEIJING, profession=STUDENT] , because student-165 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-166, age=46, city=BEIJING, profession=TEACHER] , because teacher-166's age is 46. It's out of the range[10-20]
remove person : Person [name=student-167, age=12, city=BEIJING, profession=STUDENT] , because student-167 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-168, age=48, city=BEIJING, profession=TEACHER] , because teacher-168's age is 48. It's out of the range[10-20]
remove person : Person [name=student-169, age=14, city=BEIJING, profession=STUDENT] , because student-169 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-170, age=50, city=BEIJING, profession=TEACHER] , because teacher-170's age is 50. It's out of the range[10-20]
remove person : Person [name=student-171, age=16, city=BEIJING, profession=STUDENT] , because student-171 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-172, age=52, city=BEIJING, profession=TEACHER] , because teacher-172's age is 52. It's out of the range[10-20]
remove person : Person [name=student-173, age=18, city=BEIJING, profession=STUDENT] , because student-173 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-174, age=54, city=BEIJING, profession=TEACHER] , because teacher-174's age is 54. It's out of the range[10-20]
remove person : Person [name=student-175, age=20, city=BEIJING, profession=STUDENT] , because student-175 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-176, age=21, city=BEIJING, profession=TEACHER] , because teacher-176's age is 21. It's out of the range[10-20]
remove person : Person [name=student-177, age=22, city=BEIJING, profession=STUDENT] , because student-177's age is 22. It's out of the range[10-20]
remove person : Person [name=teacher-178, age=23, city=BEIJING, profession=TEACHER] , because teacher-178's age is 23. It's out of the range[10-20]
remove person : Person [name=student-179, age=24, city=BEIJING, profession=STUDENT] , because student-179's age is 24. It's out of the range[10-20]
remove person : Person [name=teacher-180, age=25, city=BEIJING, profession=TEACHER] , because teacher-180's age is 25. It's out of the range[10-20]
remove person : Person [name=student-181, age=11, city=BEIJING, profession=STUDENT] , because student-181 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-182, age=27, city=BEIJING, profession=TEACHER] , because teacher-182's age is 27. It's out of the range[10-20]
remove person : Person [name=student-183, age=13, city=BEIJING, profession=STUDENT] , because student-183 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-184, age=29, city=BEIJING, profession=TEACHER] , because teacher-184's age is 29. It's out of the range[10-20]
remove person : Person [name=student-185, age=15, city=BEIJING, profession=STUDENT] , because student-185 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-186, age=31, city=BEIJING, profession=TEACHER] , because teacher-186's age is 31. It's out of the range[10-20]
remove person : Person [name=student-187, age=17, city=BEIJING, profession=STUDENT] , because student-187 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-188, age=33, city=BEIJING, profession=TEACHER] , because teacher-188's age is 33. It's out of the range[10-20]
remove person : Person [name=student-189, age=19, city=BEIJING, profession=STUDENT] , because student-189 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-190, age=35, city=BEIJING, profession=TEACHER] , because teacher-190's age is 35. It's out of the range[10-20]
remove person : Person [name=student-191, age=21, city=BEIJING, profession=STUDENT] , because student-191's age is 21. It's out of the range[10-20]
remove person : Person [name=teacher-192, age=37, city=BEIJING, profession=TEACHER] , because teacher-192's age is 37. It's out of the range[10-20]
remove person : Person [name=student-193, age=23, city=BEIJING, profession=STUDENT] , because student-193's age is 23. It's out of the range[10-20]
remove person : Person [name=teacher-194, age=39, city=BEIJING, profession=TEACHER] , because teacher-194's age is 39. It's out of the range[10-20]
remove person : Person [name=student-195, age=10, city=BEIJING, profession=STUDENT] , because student-195 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-196, age=41, city=BEIJING, profession=TEACHER] , because teacher-196's age is 41. It's out of the range[10-20]
remove person : Person [name=student-197, age=12, city=BEIJING, profession=STUDENT] , because student-197 is a STUDENT, not is a TEACHER
remove person : Person [name=teacher-198, age=43, city=BEIJING, profession=TEACHER] , because teacher-198's age is 43. It's out of the range[10-20]
remove person : Person [name=student-199, age=14, city=BEIJING, profession=STUDENT] , because student-199 is a STUDENT, not is a TEACHER
a person passed thougth all filters consumes 92.75 μs.

There are 3 person left though the filters.
left: Person [name=teacher-0, age=20, city=BEIJING, profession=TEACHER]
left: Person [name=teacher-70, age=20, city=BEIJING, profession=TEACHER]
left: Person [name=teacher-140, age=20, city=BEIJING, profession=TEACHER]
 
 

 

分享到:
评论

相关推荐

    jsp中Filter类实现过滤器功能

    ### JSP中Filter类实现过滤器功能 #### 概述 在Java Web开发中,`Filter`是一种常用的技术,它可以对请求和响应进行预处理或后处理,常用于执行一些通用的操作,例如权限验证、编码统一设置、敏感词过滤等。本文...

    ExtJS Filter 实现表格过滤

    这可以通过`Ext.state.Provider`类来实现,保存用户设定的过滤器状态到cookie或服务器端。 ### 性能优化 **7. 性能考虑** 对于大数据量的Grid,直接过滤所有数据可能会导致性能问题。为了避免这种情况,可以采用...

    java 中如何使用filter过滤器

    本文将详细介绍如何在Java中使用Filter过滤器,以及如何配置相关的配置文件,让你一目了然。 ### 1. Filter接口与生命周期 Filter是Servlet API中的一个接口,位于`javax.servlet`包下。它定义了三个关键方法: -...

    Filter过滤器的代码及其web.xml配置代码

    1. 创建Filter类:我们需要创建一个实现了`javax.servlet.Filter`接口的类。这个接口中有三个方法:`init()`, `doFilter()`, 和 `destroy()`。 ```java public class MyFilter implements Filter { @Override ...

    如何配置Filter过滤器处理JSP中文乱码

    首先,声明一个新的过滤器,给它一个唯一的名称,例如`EncodingFilter`,并指定过滤器的实现类,如`com.filters.SetCharacterEncodingFilter`。在这个例子中,`&lt;init-param&gt;`标签用于设置字符编码和忽略大小写的参数...

    filter过滤器的简单使用.rar

    在标题提到的"filter过滤器的简单使用.rar"压缩包中,很显然包含了一个关于如何在Maven项目和Spring Boot项目中使用Filter的示例。下面将详细介绍Filter的基本概念、工作原理以及如何在两种项目环境中应用。 **...

    filter过滤器防止恶意注入

    本示例中的`URLfilter`类就是一个简单的过滤器,它的主要目的是防止SQL注入攻击,这是一种常见的恶意攻击手段,攻击者试图通过输入恶意SQL语句来获取未经授权的数据或者破坏数据库。 SQL注入攻击通常是由于应用程序...

    Lucene5学习之Filter过滤器

    总结起来,Lucene5中的Filter过滤器是实现精确、高效搜索的关键工具。通过理解并熟练运用各种Filter,开发者可以构建出更加灵活和精准的搜索系统。在深入研究源码和实践过程中,我们不仅能掌握Lucene的基本原理,还...

    过滤器实现图片过滤功能

    实现图片过滤功能,我们需要自定义一个Filter类,继承`javax.servlet.Filter`并实现其方法。在`doFilter()`方法中,我们可以获取到请求的URL和HTTP方法,判断是否与图片资源相关。如果请求的目标是一个图片文件,...

    过滤器Filter的全面解析

    `&lt;filter&gt;`元素用于定义过滤器类,而`&lt;filter-mapping&gt;`则用于指定过滤器要拦截的URL模式。 2. **过滤器链**: 在一个Web应用中,可以定义多个过滤器,它们会形成一个过滤器链。当请求匹配到某个过滤器时,过滤器...

    过滤器(filter) 例子源码

    本篇文章将基于提供的标题和描述,详细讲解过滤器的概念、工作原理以及如何通过源码实现一个简单的过滤器示例。 ### 一、过滤器(Filter)概念 过滤器是Servlet规范的一部分,它定义了一种机制,可以在请求到达...

    Filter(过滤器)简介和工作原理

    Filter 程序是一个实现了 Filter 接口的 Java 类,与 Servlet 程序相似,它由 Servlet 容器进行调用和执行。Filter 程序需要在 web.xml 文件中进行注册和设置它所能拦截的资源,例如 Jsp, Servlet, 静态图片文件和...

    管道过滤器程序,主要用java实现

    1. **Filter接口**:这是所有过滤器组件需要实现的接口,定义了处理数据的方法,例如`filter()`。每个过滤器类都需实现此接口,提供其特定的处理逻辑。 2. **Filter链**:多个过滤器通过实例化并链接起来形成一个链...

    Filter过滤器1

    Filter 程序是一个实现 Filter 接口的 Java 类,类似于 Servlet 程序,由 Servlet 容器进行调用和执行。需要在 web.xml 文件中进行注册和设置它所能拦截的资源。 创建一个简单的 Filter 需要实现 Filter 接口,提供...

    jsp filter 过滤器判断用户是否登录

    通过以上分析,我们可以看到JSP Filter过滤器是一种非常实用的技术,它可以有效地帮助我们在Web应用中实现登录状态的检查以及其他多种过滤逻辑。在实际项目中,还可以根据需要扩展过滤器的功能,比如增加更复杂的...

    15_尚硅谷_Filter过滤器_王振国 - 课堂笔记1

    在实现 Filter 过滤器时,需要编写一个类去实现 Filter 接口,实现过滤方法 doFilter(),然后到 web.xml 中去配置 Filter 的拦截路径。下面是一个简单的示例代码: public class AdminFilter implements Filter { ...

    Socket通信,封包处理, Filter过滤器处理

    Filter过滤器处理则是在数据传输或处理流程中添加的一层逻辑,用于拦截、修改或者增强数据。 首先,我们来深入理解Socket通信。Socket可以视为网络上的两个程序之间的一个连接点,通过这个连接点,数据可以在它们...

    spring boot 拦截器拦截/Filter 过滤session案例

    - 在`WebMvcConfigurer`的实现类中,重写`addInterceptors()`方法,将拦截器添加到拦截器链中。 3. **创建过滤器**: - 实现`Filter`接口,覆盖`doFilter()`方法。 - 在`doFilter()`中检查session中的登录信息。...

Global site tag (gtag.js) - Google Analytics