
jp.go.nict.langrid.commons.util.stream.Stream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jp.go.nict.langrid.commons Show documentation
Show all versions of jp.go.nict.langrid.commons Show documentation
Common and utility library for the Service Grid Server Software and java web services.
The newest version!
/*
* This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services.
* Copyright (C) 2013 NICT Language Grid Project.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package jp.go.nict.langrid.commons.util.stream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import jp.go.nict.langrid.commons.transformer.Transformer;
import jp.go.nict.langrid.commons.util.function.BiFunction;
import jp.go.nict.langrid.commons.util.function.Consumer;
import jp.go.nict.langrid.commons.util.function.Function;
import jp.go.nict.langrid.commons.util.function.Predicate;
public class Stream {
public Stream(Provider provider){
this.provider = provider;
}
public Stream map(final Function func){
assertProviderValid();
return new Stream(new Provider(){
@Override
public U next() {
T v = p.next();
if(v != null) return func.apply(v);
return null;
}
Provider p = provider;
{ provider = null;}
});
}
public Stream map(final Transformer func){
assertProviderValid();
return new Stream(new Provider(){
@Override
public U next() {
T v = p.next();
if(v != null) return func.transform(v);
return null;
}
Provider p = provider;
{ provider = null;}
});
}
public T reduce(T identity, final BiFunction func){
assertProviderValid();
T ret = identity;
T r = null;
while((r = provider.next()) != null){
ret = func.apply(ret, r);
}
return ret;
}
public Stream filter(final Predicate pred){
assertProviderValid();
return new Stream(new Provider(){
@Override
public T next() {
T v = null;
while((v = p.next()) != null){
if(pred.test(v)) return v;
}
return null;
}
private Provider p = provider;
{ provider = null;}
});
}
public Stream expload(final Function> func){
assertProviderValid();
return new Stream(new Provider(){
@Override
public U next() {
if(p == null){
T ov = org.next();
if(ov == null) return null;
p = func.apply(ov);
}
U v = p.next();
if(v != null) return v;
T ov = org.next();
if(ov == null) return null;
p = func.apply(ov);
return p.next();
}
Provider p;
Provider org = provider;
{ provider = null;}
});
}
public void forEach(Consumer consumer){
assertProviderValid();
T v = null;
while((v = provider.next()) != null){
consumer.accept(v);
}
}
public > C into(C collection){
assertProviderValid();
T v = null;
while((v = provider.next()) != null){
collection.add(v);
}
return collection;
}
public List asList(){
List ret = new ArrayList();
into(ret);
return ret;
}
private void assertProviderValid(){
if(provider == null){
throw new IllegalStateException("This stream has been already consumed.");
}
}
private Provider provider;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy