    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.io.input;
    018
    019import java.io.Reader;
    020import java.util.Arrays;
    021import java.util.Collections;
    022import java.util.HashSet;
    023import java.util.Set;
    024import java.util.function.IntPredicate;
    025
    026/**
    027 * A filter reader that removes a given set of characters represented as {@code int} code points, handy to remove known
    028 * junk characters from CSV files for example.
    029 * <p>
    030 * This class must convert each {@code int} read to an {@code Integer}. You can increase the Integer cache with a system
    031 * property, see {@link Integer}.
    032 * </p>
    033 */
    034public class CharacterSetFilterReader extends AbstractCharacterFilterReader {
    035
    036    private static IntPredicate toIntPredicate(final Set<Integer> skip) {
    037        if (skip == null) {
    038            return SKIP_NONE;
    039        }
    040        final Set<Integer> unmodifiableSet = Collections.unmodifiableSet(skip);
    041        return c -> unmodifiableSet.contains(Integer.valueOf(c));
    042    }
    043
    044    /**
    045     * Constructs a new reader.
    046     *
    047     * @param reader the reader to filter.
    048     * @param skip the set of characters to filter out.
    049     * @since 2.9.0
    050     */
    051    public CharacterSetFilterReader(final Reader reader, final Integer... skip) {
    052        this(reader, new HashSet<>(Arrays.asList(skip)));
    053    }
    054
    055    /**
    056     * Constructs a new reader.
    057     *
    058     * @param reader the reader to filter.
    059     * @param skip the set of characters to filter out.
    060     */
    061    public CharacterSetFilterReader(final Reader reader, final Set<Integer> skip) {
    062        super(reader, toIntPredicate(skip));
    063    }
    064
    065}