Skip to content

Commit

Permalink
Check 2 ?
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxonquall committed Mar 29, 2024
1 parent d98284f commit 071a884
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 121 deletions.
18 changes: 16 additions & 2 deletions app/src/main/java/hexlet/code/schemas/BaseSchema.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package hexlet.code.schemas;

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

public abstract class BaseSchema<T> {
protected boolean requiredCalled = false;
protected Map<String, Predicate<T>> checks = new LinkedHashMap<>();

public abstract boolean isValid(T value);

protected final void addCheck(String name, Predicate<T> check) {
checks.put(name, check);
}

public final boolean isValid(T value) {
for (var check : checks.values()) {
if (!check.test(value)) {
return false;
}
}
return true;
}
}

60 changes: 12 additions & 48 deletions app/src/main/java/hexlet/code/schemas/MapSchema.java
Original file line number Diff line number Diff line change
@@ -1,68 +1,32 @@
package hexlet.code.schemas;

import java.util.HashMap;
import java.util.Map;

import java.util.Map;

public class MapSchema extends BaseSchema<Map> {

private int mapSize = 2147483647;
private Map<String, BaseSchema> nestedMap = new HashMap<>();
/**
* Validates a map according to certain criteria.
*
* @param map the map to be validated
* @return true if the map is valid, false otherwise
*/
@Override
public boolean isValid(Map map) {
if (map == null) {
return !requiredCalled;
}
public final class MapSchema extends BaseSchema {

if (!nestedMap.isEmpty()) {
for (var entry : nestedMap.entrySet()) {
var key = entry.getKey();
var value = entry.getValue();
if (!map.containsKey(key) || !value.isValid(map.get(key))) {
return false;
}
}
}

return (mapSize == 2147483647 || map.size() == mapSize);
public MapSchema() {
addCheck("data", value -> value instanceof Map<?, ?> || value == null);
}

/**
* Prohibits the use of null and the empty string.
* @return MapSchema object
*/
public MapSchema required() {
requiredCalled = true;
addCheck("required", value -> value instanceof Map<?, ?>);
return this;
}

/**
* Limits the size of the map.
* @param size
* @return MapSchema object
*/

public MapSchema sizeof(int size) {
mapSize = size;
addCheck("sizeof", value -> value != null && ((Map) value).size() == size);
return this;
}

/**
* Checks the schema for validity.
* @param schemas
* @return MapSchema object
* @param <T>
*/

public <T> MapSchema shape(Map<String, BaseSchema<T>> schemas) {
nestedMap.clear();
nestedMap.putAll(schemas);
addCheck("shape", value -> schemas.entrySet()
.stream()
;


return this;
}
}
}
43 changes: 6 additions & 37 deletions app/src/main/java/hexlet/code/schemas/NumberSchema.java
Original file line number Diff line number Diff line change
@@ -1,54 +1,23 @@
package hexlet.code.schemas;

public class NumberSchema extends BaseSchema<Integer> {
private boolean numberIsPositive = false;
private int minNumber = -2147483648;
private int maxNumber = 2147483647;
public final class NumberSchema extends BaseSchema<Integer> {

/**
* Validates a number according to certain criteria.
* @param number
* @return true if the string is valid, false otherwise
*/
public boolean isValid(Integer number) {
if (number == null) {
return !requiredCalled;
}

return (!numberIsPositive || number > 0)
&& ((number <= maxNumber && number >= minNumber) || maxNumber == 2147483647);
public NumberSchema() {
addCheck("data", value -> value instanceof Integer || value == null);
}

/**
* Prohibits the use of null and the empty string.
* @return NumberSchema object
*/

public NumberSchema required() {
requiredCalled = true;
addCheck("required", value -> value instanceof Integer);
return this;
}

/**
* Checks that a number must be positive.
* @return NumberSchema object
*/

public NumberSchema positive() {
numberIsPositive = true;
addCheck("positive", value -> value == null || value > 0);
return this;
}

/**
* Checks that a number must be in a given range.
* @param min
* @param max
* @return NumberSchema object
*/

public NumberSchema range(int min, int max) {
minNumber = min;
maxNumber = max;
addCheck("range", value -> value >= min && value <= max);
return this;
}
}
40 changes: 6 additions & 34 deletions app/src/main/java/hexlet/code/schemas/StringSchema.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,24 @@
package hexlet.code.schemas;

public class StringSchema extends BaseSchema<String> {
public final class StringSchema extends BaseSchema<String> {

private int minLengthOfString = -1;
private String subString = "";

/**
* Validates a string according to certain criteria.
* @param string
* @return true if the string is valid, false otherwise
*/
public boolean isValid(String string) {
if ((string == null) || string.isEmpty()) {
return !requiredCalled;
}

return (minLengthOfString == -1 || string.length() >= minLengthOfString)
&& (subString.isEmpty() || string.contains(subString));
public StringSchema() {
addCheck("data", value -> value instanceof String || value == null);
}

/**
* Prohibits the use of null and the empty string.
* @return StringSchema object
*/

public StringSchema required() {
addCheck("required", value -> value instanceof String && !value.isEmpty());
requiredCalled = true;
return this;
}

/**
* Limits a string by length.
* @param minLength
* @return StringSchema object
*/

public StringSchema minLength(int minLength) {
minLengthOfString = minLength;
addCheck("minLength", value -> value.length() >= minLength);
return this;
}

/**
* Checks for the presence of a substring in a string.
* @param stringExample
* @return StringSchema object
*/
public StringSchema contains(String stringExample) {
subString = stringExample;
addCheck("contains", value -> value.contains(stringExample));
return this;
}
}

0 comments on commit 071a884

Please sign in to comment.