Skip to content

Commit a1beb1a

Browse files
committed
add
1 parent b05131f commit a1beb1a

File tree

5 files changed

+13
-14
lines changed

5 files changed

+13
-14
lines changed

app/src/main/java/hexlet/code/App.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package hexlet.code;
22

3-
import hexlet.code.schemas.BaseSchema;
43
import hexlet.code.schemas.MapSchema;
4+
import hexlet.code.schemas.BaseSchema;
55

66
import java.util.HashMap;
77
import java.util.Map;
@@ -22,12 +22,12 @@ public static void main(String[] args) {
2222
human1.put("name", "Alice");
2323
human1.put("age", 25);
2424

25-
System.out.println(schema.isValid(human1));
25+
System.out.println(schema.isValid(human1)); // true
2626

2727
Map<String, Object> human2 = new HashMap<>();
2828
human2.put("name", "Bob");
2929
human2.put("age", -5);
3030

31-
System.out.println(schema.isValid(human2));
31+
System.out.println(schema.isValid(human2)); // false
3232
}
3333
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package hexlet.code.schemas;
22

3-
public abstract class BaseSchema<T> {
3+
public abstract class BaseSchema<T extends BaseSchema<T>> {
44
protected boolean isRequired = false;
55

66
public T required() {
77
this.isRequired = true;
88
return (T) this;
99
}
1010

11-
public boolean isValid(Object value) {
12-
if (isRequired && value == null) {
13-
return false;
14-
}
15-
return true;
11+
protected boolean checkRequired(Object value) {
12+
return !isRequired || value != null;
1613
}
14+
15+
public abstract boolean isValid(Object value);
1716
}

app/src/main/java/hexlet/code/schemas/MapSchema.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public class MapSchema extends BaseSchema<MapSchema> {
88

99
@Override
1010
public boolean isValid(Object value) {
11-
if (!super.isValid(value)) {
11+
if (!checkRequired(value)) {
1212
return false;
1313
}
1414
if (value == null) {
15-
return !isRequired;
15+
return true;
1616
}
1717
if (!(value instanceof Map)) {
1818
return false;

app/src/main/java/hexlet/code/schemas/NumberSchema.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public NumberSchema range(int min, int max) {
1818

1919
@Override
2020
public boolean isValid(Object value) {
21-
if (!super.isValid(value)) {
21+
if (!checkRequired(value)) {
2222
return false;
2323
}
2424
if (value == null) {

app/src/main/java/hexlet/code/schemas/StringSchema.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public StringSchema contains(String substring) {
1616

1717
@Override
1818
public boolean isValid(Object value) {
19-
if (!super.isValid(value)) {
19+
if (!checkRequired(value)) {
2020
return false;
2121
}
2222
if (value == null) {
23-
return !isRequired;
23+
return true;
2424
}
2525
if (!(value instanceof String)) {
2626
return false;

0 commit comments

Comments
 (0)