Skip to content

Commit be3098e

Browse files
Allow integer random seeds
Co-Authored-By: Jacob Sznajdman <breakanalysis@gmail.com>
1 parent 6c1b73e commit be3098e

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

config-api/src/main/java/org/neo4j/gds/config/RandomSeedConfig.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,30 @@
1919
*/
2020
package org.neo4j.gds.config;
2121

22+
import java.util.Map;
2223
import java.util.Optional;
2324

25+
import org.jetbrains.annotations.Nullable;
26+
import org.neo4j.gds.annotation.Configuration;
27+
import org.neo4j.gds.core.CypherMapWrapper;
28+
29+
@Configuration
2430
public interface RandomSeedConfig {
31+
@Configuration.ConvertWith(method = "toLong")
2532
Optional<Long> randomSeed();
33+
34+
static Long toLong(Object obj) {
35+
if (obj instanceof Integer) {
36+
return ((Integer) obj).longValue();
37+
} else if (obj instanceof Long) {
38+
return (Long) obj;
39+
} else {
40+
throw new IllegalArgumentException("Object must be of type int, Integer, long, or Long");
41+
}
42+
}
43+
44+
static RandomSeedConfig of(@Nullable Map<String, ?> userInput) {
45+
return new RandomSeedConfigImpl(CypherMapWrapper.create(userInput));
46+
}
2647
}
48+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
import org.neo4j.gds.config.RandomSeedConfig;
24+
25+
import java.util.Collections;
26+
import java.util.Map;
27+
import java.util.Optional;
28+
29+
class RandomSeedConfigTest {
30+
31+
@Test
32+
void shouldAcceptInt() {
33+
var config = RandomSeedConfig.of(Map.of("randomSeed", 42));
34+
Assertions.assertEquals(42L, config.randomSeed().get());
35+
}
36+
37+
@Test
38+
void shouldAcceptLong() {
39+
var config = RandomSeedConfig.of(Map.of("randomSeed", 42L));
40+
Assertions.assertEquals(42L, config.randomSeed().get());
41+
}
42+
43+
@Test
44+
void shouldAcceptEmpty() {
45+
var config = RandomSeedConfig.of(Collections.emptyMap());
46+
Assertions.assertEquals(Optional.empty(), config.randomSeed());
47+
}
48+
}

0 commit comments

Comments
 (0)