Skip to content

Commit

Permalink
Use Stream.toList directly
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Nov 14, 2024
1 parent aeb119d commit bf723b6
Show file tree
Hide file tree
Showing 46 changed files with 181 additions and 256 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.apache.jena.atlas.iterator.IteratorCloseable;
import org.apache.jena.atlas.iterator.IteratorSlotted;
import org.apache.jena.atlas.lib.InternalErrorException;
Expand Down Expand Up @@ -189,7 +187,7 @@ public static AsyncParserBuilder ofSources(List<RDFParserBuilder> sources) {
private static List<RDFParserBuilder> urlsToSource(List<String> filesOrURLs) {
return filesOrURLs.stream().map(
uriOrFile -> RDFParser.source(uriOrFile).errorHandler(dftErrorHandler))
.collect(Collectors.toList());
.toList();
}

/** Create a source object from the given arguments that is suitable for use with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import java.util.* ;
import java.util.function.BiConsumer ;
import java.util.stream.Collectors ;

import org.apache.jena.atlas.lib.NotImplemented ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple ;
Expand Down Expand Up @@ -116,12 +114,12 @@ void info() {
if ( opHaving != null )
System.out.printf("having: %s\n", opHaving.getExprs()) ;
if ( opExtends != null && !opExtends.isEmpty() ) {
List<VarExprList> z = opExtends.stream().map(x -> x.getVarExprList()).collect(Collectors.toList()) ;
List<VarExprList> z = opExtends.stream().map(x -> x.getVarExprList()).toList() ;
System.out.printf("assigns: %s\n", z) ;
}
if ( opGroup != null ) {
List<ExprAggregator> aggregators = opGroup.getAggregators() ;
List<Var> aggVars = aggregators.stream().map(x -> x.getAggVar().asVar()).collect(Collectors.toList()) ;
List<Var> aggVars = aggregators.stream().map(x -> x.getAggVar().asVar()).toList() ;
System.out.printf("group: %s |-| %s\n", opGroup.getGroupVars(), opGroup.getAggregators()) ;
System.out.printf("group agg vars: %s\n", aggVars) ;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ private Placement placeDisjunction(ExprList exprs, OpDisjunction input) {

if ( false ) {
// Push everything, always
// Left as a safty fall back.
// Left as a safety fall back.
List<Op> x = new ArrayList<>() ;
input.getElements().forEach(op->{
Placement p = transform(exprs, op) ;
Expand All @@ -683,7 +683,6 @@ private Placement placeDisjunction(ExprList exprs, OpDisjunction input) {
// Don't push any expressions that aren't used in any of the arms of the disjunction.
// This is more about being tidy.
List<Expr> unplaced = new ArrayList<>(exprs.getList()) ;
//List<Placement> x = input.getElements().stream().map(op->transform(exprs, op)).collect(Collectors.toList()) ;
List<Placement> placements = new ArrayList<>(exprs.size()) ;
Boolean someChange = Boolean.FALSE ;
for ( Op op : input.getElements() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.json.JsonArray;
import org.apache.jena.atlas.json.JsonObject;
Expand Down Expand Up @@ -169,7 +167,7 @@ public RowSet select() {

private RowSet execute() {
startQueryIterator();
List<Var> vars = query.getResultVars().stream().map(Var::alloc).collect(Collectors.toList());
List<Var> vars = query.getResultVars().stream().map(Var::alloc).toList();
return RowSetStream.create(vars, queryIterator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream ;

import org.apache.jena.atlas.iterator.Iter;
Expand Down Expand Up @@ -64,7 +63,7 @@ public static List<Quad> remapDefaultGraph(List<Quad> quads, final Node dftGraph
Stream<Quad> remappedStream = quads.stream().map(q->
!q.isDefaultGraph() ? q : new Quad(dftGraph, q.getSubject(), q.getPredicate(), q.getObject())
) ;
return remappedStream.collect(Collectors.toList());
return remappedStream.toList();
}

/** Substitute into triple patterns */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.util.*;

import org.apache.jena.atlas.lib.ListUtils;
import org.apache.jena.atlas.lib.SetUtils;
import org.apache.jena.datatypes.RDFDatatype ;
import org.apache.jena.datatypes.xsd.XSDDatatype ;
Expand Down Expand Up @@ -107,9 +106,7 @@ public static List<Node> convertToListNodes(String... namedGraphs) {

/** Convert strings to a List of {@link Node Nodes}. */
public static List<Node> convertToListNodes(List<String> namedGraphs) {
List<Node> nodes = ListUtils.toList(
namedGraphs.stream().map(NodeFactory::createURI)
);
List<Node> nodes = namedGraphs.stream().map(NodeFactory::createURI).toList();
return nodes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.lib.IRILib;
import org.apache.jena.atlas.lib.ListUtils;
import org.apache.jena.datatypes.RDFDatatype;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
Expand Down Expand Up @@ -301,8 +300,7 @@ public static List<Resource> findRootsByType(Model model, Resource atype) {
QuerySolutionMap qsm = new QuerySolutionMap();
qsm.add("ATYPE", atype);
try(QueryExecution qExec = QueryExecution.model(model).query(q).initialBinding(qsm).build() ) {
return ListUtils.toList(
QueryExecUtils.getAll(qExec, "root").stream().map(r->(Resource)r));
return QueryExecUtils.getAll(qExec, "root").stream().map(r->(Resource)r).toList();
}
}

Expand Down
3 changes: 1 addition & 2 deletions jena-arq/src/test/java/org/apache/jena/rdfs/LibTestRDFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.lib.ListUtils;
import org.apache.jena.atlas.lib.Pair;
import org.apache.jena.atlas.lib.StreamOps;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
Expand All @@ -43,7 +42,7 @@ public class LibTestRDFS {
* Remove triples with a predicate which is RDFS schema vocabulary.
*/
public static List<Triple> removeRDFS(List<Triple> x) {
return StreamOps.toList(x.stream().filter(ConstRDFS.filterNotRDFS));
return x.stream().filter(ConstRDFS.filterNotRDFS).toList();
}

/** Create a Jena-rules backed graph */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
import java.io.PrintStream;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import org.apache.jena.atlas.lib.ListUtils;
import org.apache.jena.atlas.lib.StreamOps;
import org.apache.jena.graph.Graph;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.Triple;
import org.apache.jena.rdfs.engine.ConstRDFS;
import org.apache.jena.rdfs.setup.MatchVocabRDFS;
import org.apache.jena.riot.RDFDataMgr;
import org.junit.Assert;
import org.junit.Test;

/** Test the setup matcher that provides access to a setup as a (s,p,o) access */
public class TestMatchVocab {
Expand Down Expand Up @@ -100,6 +100,6 @@ private void test(Node s, Node p, Node o) {
}

private List<Triple> match(Node s, Node p, Node o) {
return StreamOps.toList(matchVocab.match(s, p, o));
return matchVocab.match(s, p, o).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static List<String> allTestFiles(Path DIR) {
.filter(Files::isRegularFile)
.map(Path::toString)
.filter(fn->fn.endsWith(".rdf"))
.collect(Collectors.toList());
.toList();
} catch (IOException ex) {
throw IOX.exception(ex);
}
Expand Down Expand Up @@ -207,7 +207,7 @@ static List<String> localTestFiles() {
// If not enabling entity support in StAX
//.filter(fn-> ( xmlParserType != XMLParser.StAX || !fn.startsWith("entities") ) )
.map(fn->LOCAL_DIR.resolve(fn).toString())
.collect(Collectors.toList());
.toList();

return testfilesAbs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.jena.atlas.iterator.IteratorCloseable;
Expand Down Expand Up @@ -210,7 +209,7 @@ public void testNoEventsAreLost() {

List<EltStreamRDF> expected;
try (Stream<EltStreamRDF> s = AsyncParser.of(new ByteArrayInputStream(goodInputStr.getBytes()), Lang.TURTLE, null).streamElements()) {
expected = s.collect(Collectors.toList());
expected = s.toList();
}

// Validate parser on the good data
Expand All @@ -225,7 +224,7 @@ public void testNoEventsAreLost() {
try (Stream<EltStreamRDF> s = AsyncParser.of(new ByteArrayInputStream(badInputStr.getBytes()), Lang.TURTLE, null)
.setChunkSize(chunkSize)
.streamElements()) {
List<EltStreamRDF> actual = s.collect(Collectors.toList());
List<EltStreamRDF> actual = s.toList();
Assert.assertEquals(expectedGoodEventCount + 1, actual.size());
Assert.assertEquals(expected, actual.subList(0, expectedGoodEventCount));
Assert.assertTrue(actual.get(actual.size() - 1).isException());
Expand All @@ -250,7 +249,7 @@ public void testPrematureDispatch() {
})
.streamElements()
.limit(1000)) { // The limit is just a safety net to prevent infinite parsing in case of malfunction
actual = stream.collect(Collectors.toList());
actual = stream.toList();
}

// Check that only the expected number of elements were dispatched
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import java.util.concurrent.Future;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.jena.graph.Graph;
Expand Down Expand Up @@ -298,7 +297,7 @@ public static void runConcurrentAbort(int taskCount, int maxCancelDelay, Callabl
Random cancelDelayRandom = new Random();
ExecutorService executorService = Executors.newCachedThreadPool();
try {
List<Integer> list = IntStream.range(0, taskCount).boxed().collect(Collectors.toList());
List<Integer> list = IntStream.range(0, taskCount).boxed().toList();
list
.parallelStream()
.forEach(i -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@
import static org.apache.jena.atlas.iterator.Iter.asStream ;
import static org.apache.jena.atlas.iterator.Iter.toList;
import static org.apache.jena.atlas.iterator.Iter.toSet;
import static org.apache.jena.atlas.lib.StreamOps.toList;
import static org.junit.Assert.assertEquals ;
import static org.junit.Assert.assertFalse ;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue ;
import static org.junit.Assert.fail;

import java.util.*;
import java.util.stream.Collectors ;

import org.apache.jena.atlas.iterator.Iter;
import org.apache.jena.atlas.lib.ListUtils;
import org.apache.jena.graph.Node ;
Expand Down Expand Up @@ -205,7 +202,7 @@ protected DatasetGraphBaseFind createFind(DatasetGraph dsg) {
assertEquals(3, x.size()) ;
x.stream().allMatch(q->q.getGraph().equals(Quad.unionGraph)) ;

List<Triple> z = x.stream().map(Quad::asTriple).collect(Collectors.toList()) ;
List<Triple> z = x.stream().map(Quad::asTriple).toList();
assertTrue(z.contains(q4.asTriple())) ;
assertTrue(z.contains(q5.asTriple())) ;
Quad qx = Quad.create(Quad.unionGraph, q4.asTriple()) ;
Expand Down Expand Up @@ -286,14 +283,14 @@ public void find_union_05() {
}

@Test public void stream_dsg_01() {
List<Quad> x = toList(dsg.stream());
List<Quad> x = dsg.stream().toList();
assertEquals(10, x.size()) ;
assertTrue(x.contains(q1)) ;
assertTrue(x.contains(q5)) ;
}

@Test public void stream_dsg_02() {
List<Quad> x = toList(dsg.stream(null, s,p,o)) ;
List<Quad> x = dsg.stream(null, s,p,o).toList();
assertEquals(4, x.size()) ;
assertFalse(x.contains(q2)) ;
assertFalse(x.contains(q4)) ;
Expand All @@ -312,7 +309,7 @@ private static <T> String msg(String msg, List<T> list1, List<T> list2) {
}

static List<Triple> quadsToDistinctTriples(Iterator<Quad> iter) {
return asStream(iter).map(Quad::asTriple).distinct().collect(Collectors.toList()) ;
return asStream(iter).map(Quad::asTriple).distinct().toList() ;
}

static void print(List<Quad> x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static org.junit.Assert.assertEquals;

import java.util.*;
import java.util.stream.Collectors ;

import org.apache.jena.atlas.iterator.Iter ;
import org.apache.jena.graph.NodeFactory ;
import org.apache.jena.sparql.core.Var ;
Expand All @@ -44,7 +42,7 @@ public abstract class AbstractTestDistinctReduced {
private static List<Binding> build(List<String> items) {
return items.stream().sequential()
.map((s)-> BindingFactory.binding(var_a, NodeFactory.createLiteralString(s)))
.collect(Collectors.toList());
.toList();
}

protected abstract QueryIterator createQueryIter(List<Binding> data) ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static org.junit.Assert.fail;

import java.util.List ;
import java.util.stream.Collectors;

import org.apache.jena.atlas.lib.StrUtils;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
Expand Down Expand Up @@ -50,7 +48,7 @@ public void distinctOrderSpill_1() {
cxt.set(ARQ.spillToDiskThreshold, 2L);

List<QuerySolution> x = ResultSetFormatter.toList(qExec.execSelect());
List<Integer> z = x.stream().map(qsoln->qsoln.getLiteral("v").getInt()).collect(Collectors.toList());
List<Integer> z = x.stream().map(qsoln->qsoln.getLiteral("v").getInt()).toList();
for ( int i = 0 ; i < z.size()-1; i++ ) {
int v1 = z.get(i);
int v2 = z.get(i+1);
Expand Down
7 changes: 0 additions & 7 deletions jena-base/src/main/java/org/apache/jena/atlas/lib/Lib.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.stream.Stream;
import java.util.zip.Adler32;
import java.util.zip.CRC32;
import java.util.zip.Checksum;
Expand Down Expand Up @@ -54,12 +53,6 @@ public static String concatPaths(String directory, String path) {
return directory+"/"+path;
}

/** Stream to {@link List} */
public static <X> List<X> toList(Stream<X> stream) {
// Findability.
return StreamOps.toList(stream);
}

/** "ConcurrentHashSet" */
public static final <X> Set<X> concurrentHashSet() {
return SetUtils.concurrentHashSet();
Expand Down
10 changes: 5 additions & 5 deletions jena-base/src/main/java/org/apache/jena/atlas/lib/ListUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

import java.util.ArrayList ;
import java.util.List ;
import java.util.stream.Collectors ;
import java.util.stream.Stream ;
import java.util.stream.Stream;

import org.apache.jena.atlas.io.IndentedWriter ;
import org.apache.jena.atlas.logging.Log ;
Expand All @@ -35,7 +34,7 @@ public class ListUtils
private ListUtils() {}

public static <T> List<T> unique(List<T> list) {
return toList(list.stream().distinct());
return list.stream().distinct().toList();
}

public static List<Integer> asList(int...values) {
Expand All @@ -51,9 +50,10 @@ public static <T> T last(List<T> list) {
return list.get(list.size()-1);
}

// This is commonly needed
/** @deprecated Call {@link Stream#toList} */
@Deprecated(forRemoval = true)
public static <T> List<T> toList(Stream<T> stream) {
return stream.collect(Collectors.toList()) ;
return stream.toList() ;
}

public static <T> String str(T[] array) {
Expand Down
Loading

0 comments on commit bf723b6

Please sign in to comment.