com.feilong.io.LineNumberReaderResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of feilong Show documentation
Show all versions of feilong Show documentation
feilong is a suite of core and expanded libraries that include utility classes, http, excel,cvs, io classes, and much much more.
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.io;
/**
* 定义对 {@link java.io.LineNumberReader} 的解析.
*
* @author feilong
* @since 1.4.1
* @since 4.0.0 mark '@FunctionalInterface'
*/
@FunctionalInterface // 该注解加不加,对于接口是不是函数式接口没有影响;只是提醒编译器去检查该接口是否仅包含一个抽象方法
public interface LineNumberReaderResolver{
/**
* 每行读取的时候的操作.
*
* 如果你以前这么写代码:
*
*
*
*
*
* InputStreamReader read = new InputStreamReader(resourceAsStream, ENCODING);
* try{
* Set{@code } set = new HashSet{@code <>}();
* BufferedReader bufferedReader = new BufferedReader(read);
* String txt = null;
* while ((txt = bufferedReader.readLine()) != null){ // 读取文件,将文件内容放入到set中
* txt = txt.trim();// 忽略前面前后空格
* txt = txt.replace(" ", "");// 文中过滤空格
* set.add(txt);
* }
* }catch (Exception e){
* log.error(e.getMessage());
* }finally{
* read.close(); // 关闭文件流
* }
* return set;
*
*
*
* 现在可以重构为:
*
*
* InputStreamReader read = new InputStreamReader(resourceAsStream, ENCODING);
*
* final Set{@code } set = new HashSet{@code <>}();
*
* IOReaderUtil.resolverFile(read, new LineNumberReaderResolver(){
*
* {@code @Override}
* public boolean resolve(int lineNumber,String line){
* line = line.trim();// 忽略前面前后空格
* line = line.replace(" ", "");// 文中过滤空格
* set.add(line);// 读取文件,将文件内容放入到set中
* return true;
* }
* });
* return set;
*
*
*
*
* @param lineNumber
* 行号,
* 默认从0开始,但是此处由于在循环内部调用,即上面已经开始读取了,那么此处值最小值是1
* ( see {@link java.io.LineNumberReader#read()}), see {@link java.io.LineNumberReader#getLineNumber()}
* @param line
* the line {@link java.io.LineNumberReader#readLine()}
* @return 如果返回 ture ,将会继续循环读取下一行(如果有下一行);
* 否则跳出循环;
* @see java.io.LineNumberReader#getLineNumber()
* @see java.io.LineNumberReader#readLine()
* @see java.io.LineNumberReader#read()
* @since 2.0.0 change method name from "excute" to "resolve"
*/
boolean resolve(int lineNumber,String line);
}