Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing out a different HashSet implementation #5

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@
<maven.compiler.source>11</maven.compiler.source>
<envSources>src/main/java11</envSources>
</properties>
<dependencies>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<version>8.5.12</version>
</dependency>
</dependencies>
</project>
14 changes: 9 additions & 5 deletions src/main/java/no/hasmac/jsonld/context/InverseContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
*/
package no.hasmac.jsonld.context;

import java.util.LinkedHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;

import java.util.Map;
import java.util.Optional;

Expand All @@ -24,13 +28,13 @@ public final class InverseContext {
private final Map<String, Map<String, Map<String, Map<String, String>>>> context;

public InverseContext() {
this.context = new LinkedHashMap<>();
this.context = new Object2ObjectOpenHashMap<>();
}

private void set(final String variable, final String container, final String type, final String key, final String value) {
context.computeIfAbsent(variable, x -> new LinkedHashMap<>())
.computeIfAbsent(container, x -> new LinkedHashMap<>())
.computeIfAbsent(type, x -> new LinkedHashMap<>())
context.computeIfAbsent(variable, x -> new Object2ObjectOpenHashMap<>())
.computeIfAbsent(container, x -> new Object2ObjectOpenHashMap<>())
.computeIfAbsent(type, x -> new Object2ObjectLinkedOpenHashMap<>())
.put(key, value);
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/no/hasmac/jsonld/flattening/NodeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Objects;
import java.util.Optional;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import no.hasmac.jsonld.json.JsonProvider;
import no.hasmac.jsonld.json.JsonUtils;
import no.hasmac.jsonld.lang.Keywords;
Expand All @@ -36,7 +38,7 @@ public final class NodeMap {
private final BlankNodeIdGenerator generator = new BlankNodeIdGenerator();

public NodeMap() {
this.index = new LinkedHashMap<>();
this.index = new Object2ObjectArrayMap<>(1);
this.index.put(Keywords.DEFAULT, new LinkedHashMap<>());
}

Expand All @@ -48,7 +50,7 @@ public void set(String graphName, String subject, String property, JsonValue val

index
.computeIfAbsent(graphName, x -> new LinkedHashMap<>())
.computeIfAbsent(subject, x -> new LinkedHashMap<>())
.computeIfAbsent(subject, x -> new Object2ObjectArrayMap<>(1))
.put(property, value);
}

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/no/hasmac/jsonld/flattening/NodeMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package no.hasmac.jsonld.flattening;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
import no.hasmac.jsonld.JsonLdError;
import no.hasmac.jsonld.JsonLdErrorCode;
import no.hasmac.jsonld.ModifiableJsonArray;
Expand All @@ -33,7 +35,6 @@
import jakarta.json.JsonValue.ValueType;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -129,7 +130,7 @@ public NodeMap build() throws JsonLdError {
}

// 2.
final Map<String, JsonValue> elementObject = new LinkedHashMap<>(element.asJsonObject());
final Map<String, JsonValue> elementObject = new Object2ObjectArrayMap<>(element.asJsonObject());

// 3.
if (elementObject.containsKey(Keywords.TYPE)) {
Expand Down Expand Up @@ -175,7 +176,7 @@ public NodeMap build() throws JsonLdError {
// 5.
else if (elementObject.containsKey(Keywords.LIST)) {
// 5.1.
Map<String, JsonValue> result = new LinkedHashMap<>();
Map<String, JsonValue> result = new Object2ObjectLinkedOpenHashMap<>();
result.put(Keywords.LIST, JsonValue.EMPTY_JSON_ARRAY);

// 5.2.
Expand Down Expand Up @@ -350,7 +351,7 @@ else if (NodeObject.isNodeObject(element)) {
// 6.9.
if (elementObject.containsKey(Keywords.REVERSE)) {
// 6.9.1.
Map<String, JsonValue> referenced = new LinkedHashMap<>();
Map<String, JsonValue> referenced = new Object2ObjectLinkedOpenHashMap<>();
referenced.put(Keywords.ID, JsonProvider.instance().createValue(id));

// 6.9.2.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/no/hasmac/jsonld/json/JsonMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Optional;
import java.util.Set;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import no.hasmac.jsonld.lang.Keywords;

import jakarta.json.JsonArray;
Expand Down Expand Up @@ -85,7 +86,7 @@ public static JsonMapBuilder create(Map<String, JsonValue> object) {
}

public static JsonMapBuilder create() {
return new JsonMapBuilder(new LinkedHashMap<>());
return new JsonMapBuilder(new Object2ObjectArrayMap<>());
}

public Optional<JsonValue> get(String key) {
Expand Down
42 changes: 21 additions & 21 deletions src/main/java/no/hasmac/jsonld/json/JsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
*/
package no.hasmac.jsonld.json;

import no.hasmac.jsonld.StringUtils;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.json.JsonString;
import jakarta.json.JsonValue;
import jakarta.json.JsonValue.ValueType;
import no.hasmac.jsonld.StringUtils;

import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -79,57 +79,57 @@ public static boolean isNotScalar(final JsonValue value) {
}

public static boolean isNull(final JsonValue value) {
return value == null || ValueType.NULL.equals(value.getValueType());
return value == null || ValueType.NULL == value.getValueType();
}

public static boolean isNotNull(final JsonValue value) {
return !isNull(value);
}

public static boolean isString(JsonValue value) {
return value != null && ValueType.STRING.equals(value.getValueType());
return value != null && ValueType.STRING == value.getValueType();
}

public static boolean isNotString(JsonValue value) {
return value == null || !ValueType.STRING.equals(value.getValueType());
return value == null || ValueType.STRING != value.getValueType();
}

public static boolean isNotArray(JsonValue value) {
return value == null || !ValueType.ARRAY.equals(value.getValueType());
return value == null || ValueType.ARRAY != value.getValueType();
}

public static boolean isArray(JsonValue value) {
return value != null && ValueType.ARRAY.equals(value.getValueType());
return value != null && ValueType.ARRAY == value.getValueType();
}

public static boolean isObject(JsonValue value) {
return value != null && ValueType.OBJECT.equals(value.getValueType());
return value != null && ValueType.OBJECT == value.getValueType();
}

public static boolean isNotObject(JsonValue value) {
return value == null || !ValueType.OBJECT.equals(value.getValueType());
return value == null || ValueType.OBJECT != value.getValueType();
}

public static boolean isNumber(JsonValue value) {
return value != null && ValueType.NUMBER.equals(value.getValueType());
return value != null && ValueType.NUMBER == value.getValueType();
}

public static boolean isNotBoolean(JsonValue value) {
return value == null
|| (!ValueType.TRUE.equals(value.getValueType())
&& !ValueType.FALSE.equals(value.getValueType()));
|| ValueType.TRUE != value.getValueType()
&& ValueType.FALSE != value.getValueType();
}

public static boolean isNotNumber(JsonValue value) {
return value == null || !ValueType.NUMBER.equals(value.getValueType());
return value == null || ValueType.NUMBER != value.getValueType();
}

public static boolean isTrue(JsonValue value) {
return value != null && ValueType.TRUE.equals(value.getValueType());
return value != null && ValueType.TRUE == value.getValueType();
}

public static boolean isFalse(JsonValue value) {
return value != null && ValueType.FALSE.equals(value.getValueType());
return value != null && ValueType.FALSE == value.getValueType();
}

public static boolean isEmptyObject(JsonValue value) {
Expand All @@ -143,15 +143,15 @@ public static boolean isEmptyArray(JsonValue value) {
public static JsonObject toJsonObject(Map<String, JsonValue> map) {
final JsonObjectBuilder builder = JsonProvider.instance().createObjectBuilder();

map.entrySet().forEach(e -> builder.add(e.getKey(), e.getValue()));
map.forEach(builder::add);

return builder.build();
}

public static JsonObject merge(JsonObject target, JsonObject source) {
Map<String, JsonValue> targetMap = (new LinkedHashMap<>(target));
Map<String, JsonValue> targetMap = new LinkedHashMap<>(target);

source.forEach(targetMap::put);
targetMap.putAll(source);

return toJsonObject(targetMap);
}
Expand All @@ -162,7 +162,7 @@ public static Collection<JsonValue> toCollection(JsonValue value) {
return Collections.emptyList();
}

if (JsonValue.ValueType.ARRAY.equals(value.getValueType())) {
if (JsonValue.ValueType.ARRAY == value.getValueType()) {
return value.asJsonArray();
}

Expand All @@ -175,7 +175,7 @@ public static Stream<JsonValue> toStream(JsonValue value) {
return Stream.empty();
}

if (JsonValue.ValueType.ARRAY.equals(value.getValueType())) {
if (JsonValue.ValueType.ARRAY == value.getValueType()) {
return value.asJsonArray().stream();
}

Expand Down Expand Up @@ -232,7 +232,7 @@ public static JsonValue flatten(JsonValue value, String key) {
}

public static void withStrings(JsonValue value, Consumer<String> addContainerMapping) {
if (JsonValue.ValueType.ARRAY.equals(value.getValueType())) {
if (JsonValue.ValueType.ARRAY == value.getValueType()) {

JsonArray asJsonArray = value.asJsonArray();
for (int i = 0, asJsonArraySize = asJsonArray.size(); i < asJsonArraySize; i++) {
Expand All @@ -248,7 +248,7 @@ public static void withStrings(JsonValue value, Consumer<String> addContainerMap
}

public static List<String> optimizedGetStrings(JsonValue value) {
if (JsonValue.ValueType.ARRAY.equals(value.getValueType())) {
if (JsonValue.ValueType.ARRAY == value.getValueType()) {
JsonArray jsonArray = value.asJsonArray();
if (jsonArray.isEmpty()) {
return List.of();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/no/hasmac/rdf/impl/RdfGraphImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import java.util.Set;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import no.hasmac.rdf.RdfGraph;
import no.hasmac.rdf.RdfResource;
import no.hasmac.rdf.RdfTriple;
Expand All @@ -45,8 +47,8 @@ public void add(final RdfTriple triple) {
}

index
.computeIfAbsent(triple.getSubject(), x -> new HashMap<>(1))
.computeIfAbsent(triple.getPredicate(), x -> new HashSet<>(1))
.computeIfAbsent(triple.getSubject(), x -> new Object2ObjectArrayMap<>(1))
.computeIfAbsent(triple.getPredicate(), x -> new ObjectOpenHashSet<>(1))
.add(triple.getObject());

triples.add(triple);
Expand Down
Loading