Skip to content

Commit 27a7549

Browse files
s1ckknutwalker
andcommitted
Add NodeLabelToken#nodeLabels convenience method
Co-authored-by: Paul Horn <paul.horn@neotechnology.com>
1 parent 0f76bba commit 27a7549

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

core/src/main/java/org/neo4j/gds/core/loading/construction/NodeLabelToken.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
import org.jetbrains.annotations.NotNull;
2323
import org.neo4j.gds.NodeLabel;
2424

25+
import java.util.stream.IntStream;
26+
import java.util.stream.Stream;
27+
2528
public interface NodeLabelToken {
2629

2730
/**
@@ -46,6 +49,13 @@ public interface NodeLabelToken {
4649
@NotNull NodeLabel get(int index);
4750

4851
String[] getStrings();
52+
53+
/**
54+
* @return a stream of {@link org.neo4j.gds.NodeLabel}s represented by this token.
55+
*/
56+
default Stream<NodeLabel> nodeLabels() {
57+
return IntStream.range(0, this.size()).mapToObj(this::get);
58+
}
4959
}
5060

5161

core/src/main/java/org/neo4j/gds/core/loading/construction/NodeLabelTokenToPropertyKeys.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.Set;
3030
import java.util.concurrent.ConcurrentHashMap;
3131
import java.util.stream.Collectors;
32-
import java.util.stream.IntStream;
3332

3433
abstract class NodeLabelTokenToPropertyKeys {
3534

@@ -158,7 +157,7 @@ Set<NodeLabel> nodeLabels() {
158157
.keySet()
159158
.stream()
160159
.map(nodeLabelToken -> nodeLabelToken.isEmpty() ? NodeLabelTokens.ofNodeLabel(NodeLabel.ALL_NODES) : nodeLabelToken)
161-
.flatMap(nodeLabelToken -> IntStream.range(0, nodeLabelToken.size()).mapToObj(nodeLabelToken::get))
160+
.flatMap(NodeLabelToken::nodeLabels)
162161
.collect(Collectors.toSet());
163162
}
164163

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [http://neo4j.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Neo4j is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
package org.neo4j.gds.core.loading.construction;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.neo4j.gds.NodeLabel;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
class NodeLabelTokensTest {
28+
29+
@Test
30+
void testStream() {
31+
var aLabel = NodeLabel.of("A");
32+
var bLabel = NodeLabel.of("B");
33+
var cLabel = NodeLabel.of("C");
34+
35+
assertThat(NodeLabelTokens.empty().nodeLabels()).isEmpty();
36+
assertThat(NodeLabelTokens.ofNodeLabel(aLabel).nodeLabels()).contains(aLabel);
37+
assertThat(
38+
NodeLabelTokens.ofNodeLabels(aLabel, bLabel, cLabel).nodeLabels()
39+
).contains(aLabel, bLabel, cLabel);
40+
assertThat(
41+
NodeLabelTokens.ofStrings(aLabel.name(), bLabel.name(), cLabel.name()).nodeLabels()
42+
).contains(aLabel, bLabel, cLabel);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)