diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedBy.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedBy.java index 0418be5c..f260be34 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedBy.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedBy.java @@ -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 { - - 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 findAffectedByKeywordIn(final JsonSchema schema) { - final UnaryOperator 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 findAffectedByKeywordIn(JsonSchema schema); } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByKeywordType.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByKeywordType.java index 4f16fd38..8d816539 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByKeywordType.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByKeywordType.java @@ -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 { @@ -65,7 +64,7 @@ public Keyword createKeyword(final JsonSchema schema) { static final class AffectedKeyword extends KeywordRelationship { private final JsonSchema schema; - private final SortedSet affectedBy; + private final Set affectedBy; private final Function keywordCreator; public AffectedKeyword( @@ -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); } diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ExtendedBy.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ExtendedBy.java new file mode 100644 index 00000000..dcecbfa4 --- /dev/null +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ExtendedBy.java @@ -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 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); + } +} diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ReplacedBy.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ReplacedBy.java new file mode 100644 index 00000000..92d119af --- /dev/null +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ReplacedBy.java @@ -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 findAffectedByKeywordIn(final JsonSchema schema) { + final UnaryOperator 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); + } +} diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java index 9d542a89..15a9b376 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java +++ b/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/vocab/applicator/ApplicatorVocabulary.java @@ -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; @@ -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( @@ -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 ) ); diff --git a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectByType.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ExtendedByTest.java similarity index 72% rename from core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectByType.java rename to core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ExtendedByTest.java index de1ef5d6..1027c022 100644 --- a/core/src/main/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectByType.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ExtendedByTest.java @@ -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(); + } } diff --git a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByTest.java b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ReplacedByTest.java similarity index 56% rename from core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByTest.java rename to core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ReplacedByTest.java index bce5b3ab..d3349144 100644 --- a/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/AffectedByTest.java +++ b/core/src/test/java/io/github/sebastiantoepfer/jsonschema/core/keyword/type/ReplacedByTest.java @@ -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(); } }