Skip to content

Commit

Permalink
change BaseSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
gpiento committed May 18, 2024
1 parent f14b80a commit ecd6af5
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 37 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ plugins {
id 'java'
id 'checkstyle'
id 'jacoco'
id "com.adarshr.test-logger" version "4.0.0"
// https://plugins.gradle.org/plugin/com.adarshr.test-logger
id 'com.adarshr.test-logger' version '4.0.0'
}

repositories {
Expand Down
44 changes: 31 additions & 13 deletions app/src/main/java/hexlet/code/schemas/BaseSchema.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package hexlet.code.schemas;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public abstract class BaseSchema<T> {
public class BaseSchema {
/**
* A map of check predicates for validating values.
*/
private HashMap<String, Predicate<T>>
checkPredicates = new LinkedHashMap<>();
private final List<Predicate> checkPredicates = new ArrayList<>();

/**
* A flag to indicate if the value is required.
*/
protected boolean isRequired = false;

/**
* Adds a predicate to the list of check predicates.
*
* @param key the key for the predicate
* @param predicate the predicate to be added
*/
public void addPredicate(final String key, final Predicate<T> predicate) {
checkPredicates.put(key, predicate);
public final void addPredicate(Predicate predicate) {
checkPredicates.add(predicate);
}

/**
* Sets the required flag.
*
* @param isRequired the required flag value
*/
public final void setRequired(final boolean isRequired) {
this.isRequired = isRequired;
}

/**
Expand All @@ -29,10 +41,16 @@ public void addPredicate(final String key, final Predicate<T> predicate) {
* @return true if the value is valid based on all predicates, false
* otherwise
*/
public boolean isValid(final T value) {
return checkPredicates
.values()
.stream()
.allMatch(predicate -> predicate.test(value));
public boolean isValid(final Object value) {
if (value == null || value.equals("")) {
return !isRequired;
} else {
for (Predicate predicate : checkPredicates) {
if (!predicate.test(value)) {
return false;
}
}
}
return true;
}
}
37 changes: 26 additions & 11 deletions app/src/main/java/hexlet/code/schemas/MapSchema.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package hexlet.code.schemas;

import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;

public final class MapSchema extends BaseSchema<Map<?, ?>> {
public final class MapSchema extends BaseSchema {

/**
* A description of the entire Java function.
*
* @return description of return value
*/
public MapSchema() {
Predicate<Object> predicate = x -> x instanceof Map;
addPredicate(predicate);
}

/**
* Adds a predicate to check if the map is not null.
*
* @return the current MapSchema object
*/
public MapSchema required() {
addPredicate("required", Objects::nonNull);
setRequired(true);
return this;
}

Expand All @@ -22,7 +32,8 @@ public MapSchema required() {
* @return the current MapSchema object
*/
public MapSchema sizeof(final int size) {
addPredicate("sizeof", map -> map.size() == size);
Predicate<Map<?, ?>> predicate = m -> m.size() == size;
addPredicate(predicate);
return this;
}

Expand All @@ -33,13 +44,17 @@ public MapSchema sizeof(final int size) {
* @param shape the shape to check against the map
* @return the current MapSchema object
*/
public MapSchema shape(final Map<String, BaseSchema<Map<?, ?>>> shape) {
addPredicate("shape", predicate ->
shape.entrySet().stream().allMatch(entry -> {
Object object = predicate.get(entry.getKey());
return entry.getValue().isValid(object);
}));

public MapSchema shape(final Map<String, BaseSchema> shape) {
Predicate<Map<?, ?>> predicate = m -> {
for (Map.Entry<String, BaseSchema> entry : shape.entrySet()) {
Object object = m.get(entry.getKey());
if (!entry.getValue().isValid(object)) {
return false;
}
}
return true;
};
addPredicate(predicate);
return this;
}
}
16 changes: 12 additions & 4 deletions app/src/main/java/hexlet/code/schemas/NumberSchema.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package hexlet.code.schemas;

import java.util.Objects;
import java.util.function.Predicate;

public final class NumberSchema extends BaseSchema<Integer> {
public final class NumberSchema extends BaseSchema {

public NumberSchema() {
Predicate<Object> predicate = x -> x instanceof Integer;
addPredicate(predicate);
}

/**
* A description of the entire Java function.
*
* @return description of return value
*/
public NumberSchema required() {
addPredicate("required", Objects::nonNull);
setRequired(true);
return this;
}

Expand All @@ -22,7 +28,8 @@ public NumberSchema required() {
* @return description of return value
*/
public NumberSchema range(final int min, final int max) {
addPredicate("range", n -> n >= min && n <= max);
Predicate<Integer> predicate = n -> n >= min && n <= max;
addPredicate(predicate);
return this;
}

Expand All @@ -32,7 +39,8 @@ public NumberSchema range(final int min, final int max) {
* @return description of return value
*/
public NumberSchema positive() {
addPredicate("positive", number -> number > 0);
Predicate<Integer> predicate = n -> n > 0;
addPredicate(predicate);
return this;
}
}
26 changes: 19 additions & 7 deletions app/src/main/java/hexlet/code/schemas/StringSchema.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
package hexlet.code.schemas;

public final class StringSchema extends BaseSchema<String> {
import java.util.Objects;
import java.util.function.Predicate;

public final class StringSchema extends BaseSchema {

/**
* A description of the entire Java function.
*
* @return description of return value
*/
public StringSchema() {
Predicate<Object> predicate = x -> x instanceof String;
addPredicate(predicate);
}

/**
* Adds a predicate to check if the input String is not null and not empty.
*
* @return the current StringSchema object
*/
public StringSchema required() {
addPredicate("required", value -> value != null
&& !((String) value).isEmpty());
setRequired(true);
return this;
}

Expand All @@ -20,8 +32,8 @@ public StringSchema required() {
* @return the current StringSchema object
*/
public StringSchema minLength(final int length) {
addPredicate("minLength", s -> s != null
&& ((String) s).length() >= length);
Predicate<String> predicate = s -> s.length() >= length;
addPredicate(predicate);
return this;
}

Expand All @@ -33,8 +45,8 @@ public StringSchema minLength(final int length) {
* @return the current StringSchema object
*/
public StringSchema contains(final String substring) {
addPredicate("contains", s -> s != null
&& ((String) s).contains(substring));
Predicate<String> predicate = s -> s.contains(substring);
addPredicate(predicate);
return this;
}
}
2 changes: 1 addition & 1 deletion app/src/test/java/hexlet/code/MapSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void shape() {
Validator v = new Validator();
MapSchema schema = v.map();

Map<String, BaseSchema<String>> schemas = new HashMap<>();
Map<String, BaseSchema> schemas = new HashMap<>();
schemas.put("firstName", v.string().required());
schemas.put("lastName", v.string().required().minLength(2));

Expand Down

0 comments on commit ecd6af5

Please sign in to comment.