-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Units_Generator - add constant validator
fixes #66
- Loading branch information
Sagi Shadur
committed
Nov 18, 2017
1 parent
9aeaa95
commit 5c973bf
Showing
6 changed files
with
78 additions
and
5 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
units_generator/src/main/java/units_generator/schema_validator/ConstantValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
.../src/main/java/units_generator/schema_validator/exceptions/InvalidConstantDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters