Skip to content

Commit

Permalink
prepare affectedBy for linkedWith
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 14, 2024
1 parent 1001a5a commit 86c6dc1
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,8 @@

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;

public final class AffectedBy implements Comparable<AffectedBy> {

private final AffectByType type;
private final String name;

public AffectedBy(final AffectByType type, final String name) {
this.type = Objects.requireNonNull(type);
this.name = Objects.requireNonNull(name);
}

@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.type);
hash = 83 * hash + Objects.hashCode(this.name);
return hash;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
return compareTo((AffectedBy) obj) == 0;
}

@Override
public int compareTo(final AffectedBy other) {
final int result;
if (type.compareTo(other.type) == 0) {
result = name.compareTo(other.name);
} else {
result = type.compareTo(other.type);
}
return result;
}

Function<Keyword, Keyword> findAffectedByKeywordIn(final JsonSchema schema) {
final UnaryOperator<Keyword> result;
if (schema.keywordByName(name).isPresent()) {
result = type::affect;
} else {
result = k -> k;
}
return result;
}

@Override
public String toString() {
return "AffectedBy{" + "type=" + type + ", name=" + name + '}';
}
public interface AffectedBy {
Function<Keyword, Keyword> findAffectedByKeywordIn(JsonSchema schema);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Set;
import java.util.function.Function;

public final class AffectedByKeywordType implements KeywordType {
Expand Down Expand Up @@ -65,7 +64,7 @@ public Keyword createKeyword(final JsonSchema schema) {
static final class AffectedKeyword extends KeywordRelationship {

private final JsonSchema schema;
private final SortedSet<AffectedBy> affectedBy;
private final Set<AffectedBy> affectedBy;
private final Function<JsonSchema, Keyword> keywordCreator;

public AffectedKeyword(
Expand All @@ -76,7 +75,7 @@ public AffectedKeyword(
) {
super(name);
this.schema = Objects.requireNonNull(schema);
this.affectedBy = new TreeSet<>(affectedBy);
this.affectedBy = Set.copyOf(affectedBy);
this.keywordCreator = Objects.requireNonNull(keywordCreator);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The MIT License
*
* Copyright 2024 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.keyword.type;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import java.util.Objects;
import java.util.function.Function;

public final class ExtendedBy implements AffectedBy {

private final String name;

public ExtendedBy(final String name) {
this.name = name;
}

@Override
public Function<Keyword, Keyword> findAffectedByKeywordIn(final JsonSchema schema) {
return k -> k;
}

@Override
public String toString() {
return "Extends{" + "name=" + name + '}';
}

@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + Objects.hashCode(this.name);
return hash;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ExtendedBy other = (ExtendedBy) obj;
return Objects.equals(this.name, other.name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License
*
* Copyright 2024 sebastian.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package io.github.sebastiantoepfer.jsonschema.core.keyword.type;

import io.github.sebastiantoepfer.jsonschema.JsonSchema;
import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;

public final class ReplacedBy implements AffectedBy {

private final String name;

public ReplacedBy(final String name) {
this.name = name;
}

@Override
public Function<Keyword, Keyword> findAffectedByKeywordIn(final JsonSchema schema) {
final UnaryOperator<Keyword> result;
if (schema.keywordByName(name).isPresent()) {
result = ReplacingKeyword::new;
} else {
result = k -> k;
}
return result;
}

@Override
public String toString() {
return "Replace{" + "name=" + name + '}';
}

@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + Objects.hashCode(this.name);
return hash;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ReplacedBy other = (ReplacedBy) obj;
return Objects.equals(this.name, other.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
package io.github.sebastiantoepfer.jsonschema.core.vocab.applicator;

import io.github.sebastiantoepfer.jsonschema.Vocabulary;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.AffectByType;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.AffectedBy;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.AffectedByKeywordType;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.Affects;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.AffectsKeywordType;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.ExtendedBy;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.NamedJsonSchemaKeywordType;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.ReplacedBy;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SchemaArrayKeywordType;
import io.github.sebastiantoepfer.jsonschema.core.keyword.type.SubSchemaKeywordType;
import io.github.sebastiantoepfer.jsonschema.keyword.KeywordType;
Expand Down Expand Up @@ -79,10 +79,7 @@ public ApplicatorVocabulary(final JsonProvider provider) {
//this example shows my missunderstanding from affects, affectedBy and keywordtypes :(
new AffectedByKeywordType(
ItemsKeyword.NAME,
List.of(
new AffectedBy(AffectByType.EXTENDS, "minItems"),
new AffectedBy(AffectByType.EXTENDS, "maxItems")
),
List.of(new ExtendedBy("minItems"), new ExtendedBy("maxItems")),
//nomally affectedBy too ... but we had the needed function only in affects :(
schema ->
new AffectsKeywordType(
Expand All @@ -98,10 +95,7 @@ public ApplicatorVocabulary(final JsonProvider provider) {
new SchemaArrayKeywordType(PrefixItemsKeyword.NAME, PrefixItemsKeyword::new),
new AffectedByKeywordType(
ContainsKeyword.NAME,
List.of(
new AffectedBy(AffectByType.REPLACE, "minContains"),
new AffectedBy(AffectByType.EXTENDS, "maxContains")
),
List.of(new ReplacedBy("minContains"), new ExtendedBy("maxContains")),
new SubSchemaKeywordType(ContainsKeyword.NAME, ContainsKeyword::new)::createKeyword
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.keyword.type;

import io.github.sebastiantoepfer.jsonschema.keyword.Keyword;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

public enum AffectByType {
EXTENDS {
@Override
Keyword affect(final Keyword affectedKeyword) {
return affectedKeyword;
}
},
REPLACE {
@Override
Keyword affect(final Keyword affectedKeyword) {
return new ReplacingKeyword(affectedKeyword);
}
};
class ExtendedByTest {

abstract Keyword affect(final Keyword affectedKeyword);
@Test
void equalsContract() {
EqualsVerifier.forClass(ExtendedBy.class).verify();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,13 @@
*/
package io.github.sebastiantoepfer.jsonschema.core.keyword.type;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

import java.util.List;
import java.util.TreeSet;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.jupiter.api.Test;

class AffectedByTest {

@Test
void should_fullfil_equals_contract() {
EqualsVerifier.forClass(AffectedBy.class).withNonnullFields("type", "name").verify();
}
class ReplacedByTest {

@Test
void should_be_sorted_correctly() {
assertThat(
new TreeSet<>(
List.of(
new AffectedBy(AffectByType.REPLACE, "d"),
new AffectedBy(AffectByType.EXTENDS, "c"),
new AffectedBy(AffectByType.EXTENDS, "b"),
new AffectedBy(AffectByType.REPLACE, "a")
)
),
contains(
new AffectedBy(AffectByType.EXTENDS, "b"),
new AffectedBy(AffectByType.EXTENDS, "c"),
new AffectedBy(AffectByType.REPLACE, "a"),
new AffectedBy(AffectByType.REPLACE, "d")
)
);
void equalsContract() {
EqualsVerifier.forClass(ReplacedBy.class).verify();
}
}

0 comments on commit 86c6dc1

Please sign in to comment.