Skip to content

Commit 5c973bf

Browse files
author
Sagi Shadur
committed
Units_Generator - add constant validator
fixes #66
1 parent 9aeaa95 commit 5c973bf

File tree

6 files changed

+78
-5
lines changed

6 files changed

+78
-5
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package units_generator.schema_validator;
2+
3+
import units_generator.schema_validator.exceptions.InvalidConstantDefinition;
4+
import units_generator.schema_validator.exceptions.InvalidSchema;
5+
import units_schema.Constant;
6+
import units_schema.ConstantsGroup;
7+
import units_schema.Schema;
8+
9+
public class ConstantValidator {
10+
11+
public static void validate(
12+
Schema schema,
13+
ConstantsGroup group,
14+
Constant constant) throws InvalidSchema {
15+
validateName(group, constant);
16+
validateScaleDefinition(schema, group, constant);
17+
}
18+
19+
private static void validateScaleDefinition(Schema schema, ConstantsGroup group, Constant constant)
20+
throws InvalidConstantDefinition, InvalidSchema {
21+
String context = getContext(group, constant);
22+
boolean isSimpleScale = constant.getUnitScale() != null;
23+
boolean isRatioScale = constant.getRatio() != null;
24+
if (isSimpleScale && isRatioScale)
25+
throw new InvalidConstantDefinition(context);
26+
if (isSimpleScale)
27+
UnitsExistanceValidator.validateUnitScaleExistanceCount(
28+
schema,
29+
constant.getUnitScale(),
30+
context);
31+
if (isRatioScale)
32+
RatioValidator.validateUnitScalesRatio(
33+
schema,
34+
constant.getRatio(),
35+
context);
36+
}
37+
38+
private static void validateName(ConstantsGroup group, Constant constant) throws InvalidSchema {
39+
String context = getAnonymousContext(group, constant);
40+
NamesValidator.validateName(constant.getName(), context);
41+
}
42+
43+
public static String getAnonymousContext(
44+
ConstantsGroup group,
45+
Constant constant) {
46+
int index = group.getConstants().indexOf(constant) + 1;
47+
return "in constant number " + index +
48+
" of \"" + group.getGroupName() + "\" constants group";
49+
}
50+
51+
private static String getContext(ConstantsGroup group, Constant constant) {
52+
String context = "in \"" + constant.getName() + "\" of the \"" + group.getGroupName() + "\" constants group";
53+
return context;
54+
}
55+
}

units_generator/src/main/java/units_generator/schema_validator/ConstantsGroupValidator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package units_generator.schema_validator;
22

33
import units_generator.schema_validator.exceptions.InvalidSchema;
4+
import units_schema.Constant;
45
import units_schema.ConstantsGroup;
56
import units_schema.Schema;
67

@@ -13,10 +14,13 @@ public static void validate(Schema schema, ConstantsGroup group) throws InvalidS
1314
private static void validateName(Schema schema, ConstantsGroup group) throws InvalidSchema {
1415
String context = getAnonymousContext(schema, group);
1516
NamesValidator.validateName(group.getGroupName(), context);
17+
for (Constant constant : group.getConstants()) {
18+
ConstantValidator.validate(schema, group, constant);
19+
}
1620
}
1721

1822
public static String getAnonymousContext(Schema schema, ConstantsGroup group) {
19-
int index = schema.getConstants().getConstantsGroups().indexOf(group);
23+
int index = schema.getConstants().getConstantsGroups().indexOf(group) + 1;
2024
return "in context group number " + index;
2125
}
2226
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package units_generator.schema_validator.exceptions;
2+
3+
public class InvalidConstantDefinition extends InvalidSchema {
4+
5+
/**
6+
*
7+
*/
8+
private static final long serialVersionUID = 7200036100795696471L;
9+
10+
public InvalidConstantDefinition(String context) {
11+
super("constant can't have both \"unit scale\" and \"ratio\" attributes", context);
12+
}
13+
}

units_generator/src/main/java/units_generator/schema_validator/exceptions/InvalidName.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class InvalidName extends InvalidSchema {
99

1010
public InvalidName(String name, String context) {
1111
super("\"" + name + "\" name is invalid. name must contain only" +
12-
" lowercase letters and no trailing spaces", context);
12+
" lowercase letters and numbers, start with a letter and" +
13+
" have no trailing spaces", context);
1314
}
1415
}

units_generator/src/main/java/units_generator/schema_validator/exceptions/InvalidUnitScaleCount.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class InvalidUnitScaleCount extends InvalidSchema {
88
private static final long serialVersionUID = -4158463001679678815L;
99

1010
public InvalidUnitScaleCount(String unitScaleName, long count, String context) {
11-
super("\"" + unitScaleName + "\" unit type found " + count
12-
+ " times in schema, but there should be only one", context);
11+
super("\"" + unitScaleName + "\" unit scale found " + count
12+
+ " times in schema, but there should be exactly one", context);
1313
}
1414
}

units_generator/src/main/java/units_generator/schema_validator/exceptions/InvalidUnitTypeCount.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ public class InvalidUnitTypeCount extends InvalidSchema {
99

1010
public InvalidUnitTypeCount(String unitTypeName, long count, String context) {
1111
super("\"" + unitTypeName + "\" unit type found " + count
12-
+ " times in schema, but there should be only one", context);
12+
+ " times in schema, but there should be exactly one", context);
1313
}
1414
}

0 commit comments

Comments
 (0)