Skip to content

Commit

Permalink
Units_Generator - add constant validator
Browse files Browse the repository at this point in the history
fixes #66
  • Loading branch information
Sagi Shadur committed Nov 18, 2017
1 parent 9aeaa95 commit 5c973bf
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package units_generator.schema_validator;

import units_generator.schema_validator.exceptions.InvalidConstantDefinition;
import units_generator.schema_validator.exceptions.InvalidSchema;
import units_schema.Constant;
import units_schema.ConstantsGroup;
import units_schema.Schema;

public class ConstantValidator {

public static void validate(
Schema schema,
ConstantsGroup group,
Constant constant) throws InvalidSchema {
validateName(group, constant);
validateScaleDefinition(schema, group, constant);
}

private static void validateScaleDefinition(Schema schema, ConstantsGroup group, Constant constant)
throws InvalidConstantDefinition, InvalidSchema {
String context = getContext(group, constant);
boolean isSimpleScale = constant.getUnitScale() != null;
boolean isRatioScale = constant.getRatio() != null;
if (isSimpleScale && isRatioScale)
throw new InvalidConstantDefinition(context);
if (isSimpleScale)
UnitsExistanceValidator.validateUnitScaleExistanceCount(
schema,
constant.getUnitScale(),
context);
if (isRatioScale)
RatioValidator.validateUnitScalesRatio(
schema,
constant.getRatio(),
context);
}

private static void validateName(ConstantsGroup group, Constant constant) throws InvalidSchema {
String context = getAnonymousContext(group, constant);
NamesValidator.validateName(constant.getName(), context);
}

public static String getAnonymousContext(
ConstantsGroup group,
Constant constant) {
int index = group.getConstants().indexOf(constant) + 1;
return "in constant number " + index +
" of \"" + group.getGroupName() + "\" constants group";
}

private static String getContext(ConstantsGroup group, Constant constant) {
String context = "in \"" + constant.getName() + "\" of the \"" + group.getGroupName() + "\" constants group";
return context;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package units_generator.schema_validator;

import units_generator.schema_validator.exceptions.InvalidSchema;
import units_schema.Constant;
import units_schema.ConstantsGroup;
import units_schema.Schema;

Expand All @@ -13,10 +14,13 @@ public static void validate(Schema schema, ConstantsGroup group) throws InvalidS
private static void validateName(Schema schema, ConstantsGroup group) throws InvalidSchema {
String context = getAnonymousContext(schema, group);
NamesValidator.validateName(group.getGroupName(), context);
for (Constant constant : group.getConstants()) {
ConstantValidator.validate(schema, group, constant);
}
}

public static String getAnonymousContext(Schema schema, ConstantsGroup group) {
int index = schema.getConstants().getConstantsGroups().indexOf(group);
int index = schema.getConstants().getConstantsGroups().indexOf(group) + 1;
return "in context group number " + index;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package units_generator.schema_validator.exceptions;

public class InvalidConstantDefinition extends InvalidSchema {

/**
*
*/
private static final long serialVersionUID = 7200036100795696471L;

public InvalidConstantDefinition(String context) {
super("constant can't have both \"unit scale\" and \"ratio\" attributes", context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class InvalidName extends InvalidSchema {

public InvalidName(String name, String context) {
super("\"" + name + "\" name is invalid. name must contain only" +
" lowercase letters and no trailing spaces", context);
" lowercase letters and numbers, start with a letter and" +
" have no trailing spaces", context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class InvalidUnitScaleCount extends InvalidSchema {
private static final long serialVersionUID = -4158463001679678815L;

public InvalidUnitScaleCount(String unitScaleName, long count, String context) {
super("\"" + unitScaleName + "\" unit type found " + count
+ " times in schema, but there should be only one", context);
super("\"" + unitScaleName + "\" unit scale found " + count
+ " times in schema, but there should be exactly one", context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class InvalidUnitTypeCount extends InvalidSchema {

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

0 comments on commit 5c973bf

Please sign in to comment.