Skip to content

Instantly share code, notes, and snippets.

@deyindra
Created October 20, 2016 21:26
Show Gist options
  • Save deyindra/89f9d4d684719e1017af7793a3721645 to your computer and use it in GitHub Desktop.
Save deyindra/89f9d4d684719e1017af7793a3721645 to your computer and use it in GitHub Desktop.
FileContentIterartor
import java.io.Closeable;
import java.util.Iterator;
/**
* Iterator which used some resource needs to be closed after execution
* <em>Any concrete {@link ClosableResourceIterator} class either must be initialized with
* try with resource or should call {@link ClosableResourceIterator#close()} after complele
* execution.</em>
* @see Iterator
* @see Closeable
*/
public interface ClosableResourceIterator<T> extends Iterator<T>, Closeable{
@Override
default void remove() {
throw new UnsupportedOperationException("Invalid Operation");
}
}
/**
*
* @param <E> Deserialize Object E from {@link String}
* <pre>
* DeSerialize&lt;Integer&gt; deserializer = str -&gt; Integer.parseInt(str);
* int i = (deserializer.deserialize("1");
* //print 1
* System.out.println(i+2);
* </pre>
*/
public interface DeSerialize<E>{
E deserialize(String str);
}
import org.idey.algo.util.AssertJ;
import org.idey.algo.util.DeSerialize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.NoSuchElementException;
/**
* Iterator which will read the content of text file line by line and deserialize line
* based on {@link DeSerialize#deserialize(String)} specification
* Content of file "abc.txt" is set of integers
* 1
* 2
* 3
* 4
* <pre>
* DeSerialize&lt;Integer&gt; deserializer = str -&gt; Integer.parseInt(str);
* try(ClosableResourceIterator&lt;Integer&gt; it = new FileContentIterator&lt;&gt;("abc.txt",deserializer)){
* while(it.hasNext()){
* // print content 1,2,3,4
* System.out.println(it.next());
* }
* }
* </pre>
* <em>{@link FileContentResourceIterator} must be initialized by try with resource or
* one must call {@link FileContentResourceIterator#close()} method explicitly
* </em>
*/
public class FileContentResourceIterator<T> implements ClosableResourceIterator<T> {
private static final Logger log = LoggerFactory.getLogger(FileContentResourceIterator.class);
private BufferedReader reader;
private DeSerialize<T> deSerialize;
//Content of each line in the text file
private String line;
/**
* @param fileName File Name of the text file including path
* @param deSerialize {@link DeSerialize} which will deserialize the {@link String}
* content into an Objecr
* @throws FileNotFoundException in case fileName does exists or not a valid file
* @throws IllegalArgumentException in case of fileName is null or empty or deSerialize is null
*/
public FileContentResourceIterator(String fileName, DeSerialize<T> deSerialize) throws FileNotFoundException {
AssertJ.assertTrue(s-> !(s == null || ("").equals(s.trim())),
fileName, "File name can not be null");
AssertJ.notNull(deSerialize, "Deserializer can not be null");
fileName = fileName.trim();
this.reader = new BufferedReader(new FileReader(fileName));
this.deSerialize = deSerialize;
}
/**
*
* @return true if the line is not null else false
* @throws IllegalStateException in case there is any error while reading
*/
@Override
public boolean hasNext() {
if (line != null) {
return true;
} else {
try {
line = reader.readLine();
return (line != null);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}
/**
* @return T in afrer {@link DeSerialize#deserialize(String)} method call
* @throws NoSuchElementException in case of no more line
* @throws IllegalStateException in case there is any problem in reading line
*/
@Override
public T next() {
if (line != null || hasNext()) {
String prevLine = line;
line = null;
return deSerialize.deserialize(prevLine);
} else {
throw new NoSuchElementException();
}
}
@Override
public void close() throws IOException {
if(reader!=null){
log.info("Closing Reader");
reader.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment