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.Collections;
021import java.util.Set;
022
023/**
024 * A filter reader that removes a given set of characters represented as <code>int</code> code points, handy to remove
025 * known junk characters from CSV files for example.
026 * <p>
027 * This class must convert each <code>int</code> read to an <code>Integer</code>. You can increase the Integer cache
028 * with a system property, see {@link Integer}.
029 * </p>
030 */
031public class CharacterSetFilterReader extends AbstractCharacterFilterReader {
032
033    private static final Set<Integer> EMPTY_SET = Collections.emptySet();
034    private final Set<Integer> skipSet;
035
036    /**
037     * Constructs a new reader.
038     *
039     * @param reader
040     *            the reader to filter.
041     * @param skip
042     *            the set of characters to filter out.
043     */
044    public CharacterSetFilterReader(final Reader reader, final Set<Integer> skip) {
045        super(reader);
046        this.skipSet = skip == null ? EMPTY_SET : Collections.unmodifiableSet(skip);
047    }
048
049    @Override
050    protected boolean filter(final int ch) {
051        // Note WRT Integer.valueOf(): You can increase the Integer cache with a system property, see {@link Integer}.
052        return skipSet.contains(Integer.valueOf(ch));
053    }
054
055}