Skip to content

Commit

Permalink
improve isValidFor
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-toepfer committed Jun 15, 2024
1 parent 23cf384 commit 5a0fd4f
Show file tree
Hide file tree
Showing 29 changed files with 884 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
import io.github.sebastiantoepfer.ddd.common.Media;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Alternative;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Concatenation;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Dimension;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.Element;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.NumericCharacter;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleName;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.RuleReference;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.SequenceGroup;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.StringElement;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.ValidateableCodePoint;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.ValueRangeAlternatives;
import io.github.sebastiantoepfer.jsonschema.vocabulary.format.assertion.abnf.element.VariableRepetition;

Expand Down Expand Up @@ -75,7 +76,7 @@ Element definition() {
CRLF() {
@Override
Element definition() {
return Concatenation.of(RuleReference.of(CR.asRuleName()), RuleReference.of(LF.asRuleName()));
return Concatenation.of(CR, LF);
}
},
CTL() {
Expand Down Expand Up @@ -134,14 +135,7 @@ Element definition() {
LWSP() {
@Override
Element definition() {
return VariableRepetition.of(
SequenceGroup.of(
Concatenation.of(
Alternative.of(RuleReference.of(WSP.asRuleName()), RuleReference.of(CRLF.asRuleName())),
RuleReference.of(WSP.asRuleName())
)
)
);
return VariableRepetition.of(SequenceGroup.of(Concatenation.of(Alternative.of(WSP, CRLF), WSP)));
}
},
OCTET() {
Expand Down Expand Up @@ -175,7 +169,7 @@ Element definition() {
}
};

public RuleName asRuleName() {
public final RuleName asRuleName() {
return RuleName.of(name());
}

Expand All @@ -184,10 +178,19 @@ public final <T extends Media<T>> T printOn(final T media) {
return Rule.of(asRuleName(), definition()).printOn(media).withValue("type", "corerule");
}

@Override
public final boolean isValidFor(final int codePoint) {
return isValidFor(ValidateableCodePoint.of(0, codePoint));
}

@Override
public final boolean isValidFor(final ValidateableCodePoint codePoint) {
return definition().isValidFor(codePoint);
}

@Override
public final Dimension dimension() {
return definition().dimension();
}

abstract Element definition();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ public <T extends Media<T>> T printOn(final T media) {
}

@Override
public boolean isValidFor(final int codePoint) {
public boolean isValidFor(final ValidateableCodePoint codePoint) {
return alternatives.stream().anyMatch(e -> e.isValidFor(codePoint));
}

@Override
public Dimension dimension() {
return alternatives.stream().map(Element::dimension).sorted().reduce(Dimension::expandTo).orElseThrow();
}

@Override
public int hashCode() {
int hash = 7;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,26 @@ public <T extends Media<T>> T printOn(final T media) {
}

@Override
public boolean isValidFor(final int codePoint) {
throw new UnsupportedOperationException("Not supported yet.");
public Dimension dimension() {
return concatenations.stream().map(Element::dimension).reduce(Dimension::plus).orElseThrow();
}

@Override
public boolean isValidFor(final ValidateableCodePoint codePoint) {
final boolean result;
if (dimension().isInRange(codePoint)) {
final Element first = concatenations.get(0);
if (first.dimension().isInRange(codePoint)) {
result = first.isValidFor(codePoint);
} else {
result =
of(concatenations.subList(1, concatenations.size()))
.isValidFor(codePoint.repositionBackBy(first.dimension()));
}
} else {
result = false;
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* The MIT License
*
* Copyright 2023 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.vocabulary.format.assertion.abnf.element;

import static java.util.Comparator.comparingLong;

public final class Dimension implements Comparable<Dimension> {

private static final Dimension ZERO = Dimension.of(0);
private static final Dimension ONE = Dimension.of(1);

public static Dimension zero() {
return ZERO;
}

public static Dimension one() {
return ONE;
}

public static Dimension of(final long size) {
return of(size, size);
}

public static Dimension of(final long minSize, final long maxSize) {
if (minSize < 0) {
throw new IllegalArgumentException("minSize must be greather or equals than 0!");
}
if (maxSize < minSize) {
throw new IllegalArgumentException("MaxSize must be greater or equals minSize!");
}
return new Dimension(minSize, maxSize);
}

private final long minSize;
private final long maxSize;

private Dimension(final long minSize, final long maxSize) {
this.minSize = minSize;
this.maxSize = maxSize;
}

public boolean isInRange(final ValidateableCodePoint codepoint) {
return codepoint.position() < maxSize;
}

Dimension expandTo(final Dimension newMax) {
return of(Math.min(minSize, newMax.minSize), Math.max(maxSize, newMax.maxSize));
}

Dimension plus(final Dimension toAdd) {
return of(minSize + toAdd.minSize, maxSize + toAdd.maxSize);
}

Dimension multipliesBy(final int multiplier) {
return of(minSize * multiplier, maxSize * multiplier);
}

@Override
public int compareTo(final Dimension t) {
return comparingLong(Dimension::length).thenComparing(comparingLong(Dimension::minSize)).compare(this, t);
}

private long minSize() {
return minSize;
}

long length() {
return maxSize;
}

@Override
public String toString() {
return "Dimension{" + "minSize=" + minSize + ", maxSize=" + maxSize + '}';
}

@Override
public int hashCode() {
int hash = 7;
hash = 53 * hash + (int) (this.minSize ^ (this.minSize >>> 32));
hash = 53 * hash + (int) (this.maxSize ^ (this.maxSize >>> 32));
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 Dimension other = (Dimension) obj;
if (this.minSize != other.minSize) {
return false;
}
return this.maxSize == other.maxSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import io.github.sebastiantoepfer.ddd.common.Printable;

public interface Element extends Printable {
//no no, but let get start simple
boolean isValidFor(int codePoint);
boolean isValidFor(ValidateableCodePoint codePoint);

default Dimension dimension() {
return Dimension.one();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ private NumericCharacter(final BASE base, final int value) {
}

@Override
public boolean isValidFor(final int codePoint) {
return value == codePoint;
public boolean isValidFor(final ValidateableCodePoint codePoint) {
return codePoint.isEqualsTo(value);
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue("type", "num-val").withValue("base", base.name()).withValue("value", value);
}

boolean lessThanOrEquals(final int codePoint) {
return value <= codePoint;
boolean lessThanOrEquals(final ValidateableCodePoint codePoint) {
return codePoint.isGreaterOrEqualsThan(value);
}

boolean greatherThaneOrEquals(final int codePoint) {
return value >= codePoint;
boolean greatherThaneOrEquals(final ValidateableCodePoint codePoint) {
return codePoint.isLessOrEqualsThan(value);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ public <T extends Media<T>> T printOn(final T media) {
}

@Override
public boolean isValidFor(final int codePoint) {
throw new UnsupportedOperationException("Not supported yet.");
public boolean isValidFor(final ValidateableCodePoint codePoint) {
return true;
}

@Override
public Dimension dimension() {
return Dimension
.zero()
.expandTo(
optionalElements.stream().map(Element::dimension).sorted().reduce(Dimension.zero(), Dimension::plus)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ private RuleName(final String name) {
this.name = Objects.requireNonNull(name);
}

String name() {
return name;
}

@Override
public <T extends Media<T>> T printOn(final T media) {
return media.withValue("name", name).withValue("type", "rulename");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ private RuleReference(final RuleName name) {
}

@Override
public boolean isValidFor(final int codePoint) {
throw new UnsupportedOperationException("Not supported yet.");
public boolean isValidFor(final ValidateableCodePoint codePoint) {
return ruleNameAsStringElement().isValidFor(codePoint);
}

@Override
public Dimension dimension() {
return ruleNameAsStringElement().dimension();
}

Element ruleNameAsStringElement() {
return StringElement.of(name.name());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,25 @@ public <T extends Media<T>> T printOn(final T media) {
}

@Override
public boolean isValidFor(final int codePoint) {
throw new UnsupportedOperationException("Not supported yet.");
public boolean isValidFor(final ValidateableCodePoint codePoint) {
final boolean result;
if (dimension().isInRange(codePoint)) {
final Element first = elements.get(0);
if (first.dimension().isInRange(codePoint)) {
result = first.isValidFor(codePoint);
} else {
result =
of(elements.subList(1, elements.size())).isValidFor(codePoint.repositionBackBy(first.dimension()));
}
} else {
result = false;
}
return result;
}

@Override
public Dimension dimension() {
return elements.stream().map(Element::dimension).reduce(Dimension.zero(), Dimension::plus);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,24 @@ public <T extends Media<T>> T printOn(final T media) {
}

@Override
public boolean isValidFor(final int codePoint) {
throw new UnsupportedOperationException("Not supported yet.");
public boolean isValidFor(final ValidateableCodePoint codePoint) {
final boolean result;
if (dimension().isInRange(codePoint)) {
if (elementToRepeat.dimension().isInRange(codePoint)) {
result = elementToRepeat.isValidFor(codePoint);
} else {
result =
of(elementToRepeat, occurences).isValidFor(codePoint.repositionBackBy(elementToRepeat.dimension()));
}
} else {
result = false;
}
return result;
}

@Override
public Dimension dimension() {
return elementToRepeat.dimension().multipliesBy(occurences);
}

@Override
Expand Down
Loading

0 comments on commit 5a0fd4f

Please sign in to comment.