com.ajjpj.abase.io.AFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of a-base Show documentation
Show all versions of a-base Show documentation
a-base is a library of basic (hence the name) classes, most notably immutable collection classes with copy-on-write operations
The newest version!
package com.ajjpj.abase.io;
import com.ajjpj.abase.collection.ACollectionHelper;
import com.ajjpj.abase.collection.immutable.AOption;
import com.ajjpj.abase.collection.immutable.ATraversable;
import com.ajjpj.abase.function.*;
import com.ajjpj.abase.util.AUnchecker;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* @author arno
*/
public class AFile implements ATraversable {
private final File file;
private final Charset encoding;
public AFile(String file, Charset encoding) {
this(new File(file), encoding);
}
public AFile(File file, Charset encoding) {
this.file = file;
this.encoding = encoding;
}
public File getFile() {
return file;
}
public List lines() throws IOException {
final List result = new ArrayList<>();
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
String line;
while ((line = r.readLine()) != null) {
result.add(line);
}
}
return result;
}
public void iterate(AStatement1, E> callback) throws E, IOException {
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
final Iterator iter = new Iterator() {
private String line = r.readLine();
@Override
public boolean hasNext() {
return line != null;
}
@Override
public String next() {
return AUnchecker.executeUnchecked(new AFunction0() {
@Override
public String apply() throws IOException {
final String result = line;
line = r.readLine();
return result;
}
});
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
callback.apply(iter);
}
}
public R iterate(AFunction1 super Iterator, ? extends R, E> callback) throws E, IOException {
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
final Iterator iter = new Iterator() {
private String line = r.readLine();
@Override
public boolean hasNext() {
return line != null;
}
@Override
public String next() {
return AUnchecker.executeUnchecked(new AFunction0() {
@Override
public String apply() throws IOException {
final String result = line;
line = r.readLine();
return result;
}
});
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
return callback.apply(iter);
}
}
private Iterator iterator(final BufferedReader r) throws IOException {
return new Iterator() {
private String line = r.readLine();
@Override
public boolean hasNext() {
return line != null;
}
@Override
public String next() {
return AUnchecker.executeUnchecked(new AFunction0() {
@Override
public String apply() throws IOException {
final String result = line;
line = r.readLine();
return result;
}
});
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
@Override public void forEach(AStatement1 super String, E> f) throws E {
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
final Iterator iter = iterator(r);
while(iter.hasNext()) {
f.apply(iter.next());
}
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
}
}
@Override
public ATraversable filter(APredicate super String, E> pred) throws E {
try {
return ACollectionHelper.asACollectionView(ACollectionHelper.filter(lines(), pred));
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
return null; // for the compiler
}
}
@Override
public ATraversable map(AFunction1 super String, ? extends X, E> f) throws E {
try {
return ACollectionHelper.asACollectionView(ACollectionHelper.map(lines(), f));
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
return null; // for the compiler
}
}
@Override
public ATraversable flatMap(AFunction1 super String, ? extends Iterable, E> f) throws E {
try {
return ACollectionHelper.asACollectionView(ACollectionHelper.flatMap(lines(), f));
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
return null; // for the compiler
}
}
@Override public R foldLeft (R startValue, AFunction2 f) throws E {
try {
return ACollectionHelper.foldLeft (lines(), startValue, f);
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
return null; // for the compiler
}
}
@Override public AOption find(APredicate super String, E> pred) throws E {
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
final Iterator iter = iterator(r);
while(iter.hasNext()) {
final String candidate = iter.next();
if(pred.apply(candidate)) {
return AOption.some(candidate);
}
}
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
}
return AOption.none();
}
@Override public ATraversable flatten() {
throw new UnsupportedOperationException();
}
@Override public boolean forAll(APredicate super String, E> pred) throws E {
try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding))) {
final Iterator iter = iterator(r);
while(iter.hasNext()) {
if(! pred.apply(iter.next())) {
return false;
}
}
} catch (IOException e) {
AUnchecker.throwUnchecked(e);
}
return true;
}
@Override public boolean exists(APredicate super String, E> pred) throws E {
return find(pred).isDefined();
}
}