Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
filip26 committed Jan 4, 2022
1 parent 8110d59 commit 6af5fe6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/apicatalog/jsonld/framing/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@

public final class Frame {

public static final Frame EMPTY = new Frame(JsonValue.EMPTY_JSON_OBJECT);

private final JsonObject frameObject;

private Frame(final JsonObject frameObject) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/apicatalog/jsonld/framing/FrameMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,12 @@ public boolean match(final Map<String, JsonValue> node) throws JsonLdError {

if (ValueObject.isValueObject(listValue.asJsonArray().get(0))) {

final Frame frame = Frame.of((JsonStructure)listValue);
boolean match = false;

for (final JsonValue value : JsonUtils.toCollection(nodeListValue)) {

match = Frame.of((JsonStructure)listValue).matchValue(value);
match = frame.matchValue(value);
if (match) {
break;
}
Expand All @@ -204,10 +205,12 @@ public boolean match(final Map<String, JsonValue> node) throws JsonLdError {

} else if (NodeObject.isNodeObject(listValue.asJsonArray().get(0)) || NodeObject.isNodeReference(listValue.asJsonArray().get(0))) {

final Frame frame = Frame.of((JsonStructure)listValue);
boolean match = false;

for (final JsonValue value : JsonUtils.toCollection(nodeListValue)) {

match = Frame.of((JsonStructure)listValue).matchNode(state, value, requireAll);
match = frame.matchNode(state, value, requireAll);

if (match) {
break;
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/apicatalog/jsonld/framing/Framing.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void frame() throws JsonLdError {

if (!frame.contains(Keywords.GRAPH)) {
recurse = !Keywords.MERGED.equals(state.getGraphName());
subframe = Frame.of(JsonValue.EMPTY_JSON_OBJECT);
subframe = Frame.EMPTY;

// 4.5.2.
} else {
Expand All @@ -157,11 +157,10 @@ public void frame() throws JsonLdError {
if (JsonUtils.isObject(frame.get(Keywords.GRAPH))
|| JsonUtils.isArray(frame.get(Keywords.GRAPH))
) {

subframe = Frame.of((JsonStructure)frame.get(Keywords.GRAPH));

} else {
subframe = Frame.of(JsonValue.EMPTY_JSON_OBJECT);
subframe = Frame.EMPTY;
}
}

Expand Down Expand Up @@ -233,23 +232,25 @@ public void frame() throws JsonLdError {
// 4.7.3.1.
if (ListObject.isListObject(item)) {

JsonValue listFrame = null;
JsonValue listFrameValue = null;

if (frame.contains(property)
&& !JsonUtils.isEmptyArray(frame.get(property))
&& JsonUtils.isObject(frame.get(property).asJsonArray().get(0))
) {
listFrame = frame.get(property).asJsonArray().get(0).asJsonObject().get(Keywords.LIST);
listFrameValue = frame.get(property).asJsonArray().get(0).asJsonObject().get(Keywords.LIST);
}

if (listFrame == null) {
listFrame = Json.createObjectBuilder()
if (listFrameValue == null) {
listFrameValue = Json.createObjectBuilder()
.add(Keywords.EMBED, "@".concat(embed.name().toLowerCase()))
.add(Keywords.EXPLICIT, explicit)
.add(Keywords.REQUIRE_ALL, requireAll)
.build();
}

final Frame listFrame = Frame.of((JsonStructure)listFrameValue);

final JsonArrayBuilder list = Json.createArrayBuilder();

for (final JsonValue listItem : JsonUtils.toCollection(item.asJsonObject().get(Keywords.LIST))) {
Expand All @@ -266,7 +267,7 @@ public void frame() throws JsonLdError {
Framing.with(
listState,
Arrays.asList(listItem.asJsonObject().getString(Keywords.ID)),
Frame.of((JsonStructure)listFrame),
listFrame,
listResult,
Keywords.LIST)
.ordered(ordered)
Expand Down Expand Up @@ -356,7 +357,7 @@ public void frame() throws JsonLdError {

for (final String reverseProperty : reverseObject.asJsonObject().keySet()) {

final JsonValue subframe = reverseObject.asJsonObject().get(reverseProperty);
final Frame subframe = Frame.of((JsonStructure)reverseObject.asJsonObject().get(reverseProperty));

for (final String subjectProperty : state.getGraphMap().get(state.getGraphName()).map(Map::keySet).orElseGet(() -> Collections.emptySet())) {

Expand All @@ -379,7 +380,7 @@ public void frame() throws JsonLdError {
Framing.with(
reverseState,
Arrays.asList(subjectProperty),
Frame.of((JsonStructure)subframe),
subframe,
reverseResult,
null)
.ordered(ordered)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/apicatalog/jsonld/uri/UriResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private static final String[] resolveAsComponents(final URI base, final URI rela
private static final String removeDotSegments(final String path) {

if (UriUtils.isNotDefined(path)) {
return "";
return null;
}

String input = path;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/apicatalog/jsonld/uri/UriUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static final boolean startsWithScheme(final String uri) {
continue;
}

// a scheme name must be terminated by ';'
// a scheme name must be terminated by ':'
return uri.charAt(i) == ':';
}
return false;
Expand Down

0 comments on commit 6af5fe6

Please sign in to comment.