Skip to content

Commit

Permalink
Change methods properties
Browse files Browse the repository at this point in the history
  • Loading branch information
niyatanya committed Apr 15, 2024
1 parent 4d4250c commit 273f6cb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/hexlet/code/Validator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import hexlet.code.schemas.StringSchema;

public class Validator {
public StringSchema string() {
public final StringSchema string() {
return new StringSchema();
}
public NumberSchema number() {
public final NumberSchema number() {
return new NumberSchema();
}
public MapSchema map() {
public final MapSchema map() {
return new MapSchema();
}
}
12 changes: 9 additions & 3 deletions app/src/main/java/hexlet/code/schemas/BaseSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

public abstract class BaseSchema<T> {
protected Map<String, Predicate<T>> checkers = new LinkedHashMap<>();
public void addCheck(String checkName, Predicate<T> fn) {
public final void addCheck(String checkName, Predicate<T> fn) {
checkers.put(checkName, fn);
}

/**
* <p>Добавляет проверку на пустое поле и/или на null.</p>
* @return Возвращает схему, в которую добавлена эта проверка.
*/
public BaseSchema<T> required() {
Predicate<T> requiredFunction = ((value) -> value != null);
checkers.put("required", requiredFunction);
addCheck(
"required",
value -> value != null
);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/hexlet/code/schemas/MapSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
@NoArgsConstructor
public class MapSchema extends BaseSchema<Map<?, ?>> {

public MapSchema sizeof(int size) {
public final MapSchema sizeof(int size) {
addCheck(
"sizeof",
value -> value.size() == size
);
return this;
}

public <T> MapSchema shape(Map<String, BaseSchema<T>> schemas) {
public final <T> MapSchema shape(Map<String, BaseSchema<T>> schemas) {
addCheck(
"shape",
map -> {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/hexlet/code/schemas/NumberSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
@NoArgsConstructor
public class NumberSchema extends BaseSchema<Integer> {

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

public NumberSchema range(int startIndex, int finalIndex) {
public final NumberSchema range(int startIndex, int finalIndex) {
addCheck(
"range",
value -> value >= startIndex && value <= finalIndex
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/hexlet/code/schemas/StringSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
public class StringSchema extends BaseSchema<String> {

@Override
public StringSchema required() {
public final StringSchema required() {
addCheck(
"required",
value -> value != null && !value.isEmpty()
);
return this;
}

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

public StringSchema contains(String content) {
public final StringSchema contains(String content) {
addCheck(
"contains",
value -> value.contains(content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class StringSchemaTest {
private static StringSchema stringSchema;

@BeforeEach
public void preparation() {
public final void preparation() {
Validator val = new Validator();
stringSchema = val.string();
}
Expand Down

0 comments on commit 273f6cb

Please sign in to comment.