Skip to content

Commit

Permalink
Refactoring to reduce cyclomatic complexity on methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariana Azevedo committed May 28, 2019
1 parent 4cb4247 commit ef790e7
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ You must import .jar into the classpath of your project. If your project is a ma
<dependency>
<groupId>io.github.mariazevedo88</groupId>
<artifactId>json-formatter-validator-java7</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<connection>scm:git:git://github.com/mariazevedo88/json-formatter-validator-java7.git</connection>
<developerConnection>scm:git:git@github.com:mariazevedo88/json-formatter-validator-java7.git</developerConnection>
<url>https://github.com/mariazevedo88/json-formatter-validator-java7</url>
<tag>json-formatter-validator-java7-2.0.1</tag>
<tag>json-formatter-validator-java7-2.0.2</tag>
</scm>

<distributionManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,17 +442,17 @@ private String removeJSONObjectFromString(String invalidJson, String jsonObjectP
int numberRightBrackets = 0;

for (int i = lastIndexOf; i < builderModified.length(); i++) {
String next = builderModified.substring(i,i+1);
if(next.equals(DelimitersEnum.LEFT_KEY.getValue())) numberLeftKeys++;

if(next.equals(DelimitersEnum.RIGHT_KEY.getValue())) numberRightKeys++;

if(next.equals(DelimitersEnum.LEFT_BRACKETS.getValue())) numberLeftBrackets++;
String next = builderModified.substring(i,i+1);

if(next.equals(DelimitersEnum.RIGHT_BRACKETS.getValue())) numberRightBrackets++;
numberLeftKeys = checkNumberOfLeftKeys(numberLeftKeys, next);
numberRightKeys = checkIfExpressionisARightKey(numberRightKeys, next);
numberLeftBrackets = checkIfExpressionIsALeftBracket(numberLeftBrackets, next);
numberRightBrackets = checkIfExpressionIsARightBracket(numberRightBrackets, next);

if((next.equals(DelimitersEnum.COMMA.getValue()) && hasEqualNumberOfKeysOrBrackets(numberLeftKeys, numberRightKeys) && hasEqualNumberOfKeysOrBrackets(numberLeftBrackets, numberRightBrackets))
|| (next.equals(DelimitersEnum.RIGHT_KEY.getValue()) && hasMoreRightKeys(numberLeftKeys, numberRightKeys))) {
if(checkIfJsonHasEqualNumberExpressionsBeforeComma(numberLeftKeys, numberRightKeys, numberLeftBrackets,
numberRightBrackets, next) || checkIfLastKeyIsARightKey(numberLeftKeys, numberRightKeys, next)) {

if (next.equals(DelimitersEnum.COMMA.getValue())) jsonObjectPattern = jsonObjectPattern.concat(next);
break;
}
Expand Down Expand Up @@ -488,16 +488,14 @@ private String filterJSONObjectFromString(String invalidJson, String jsonObjectP

String next = builderModified.substring(i,i+1);

if(next.equals(DelimitersEnum.LEFT_KEY.getValue())) numberLeftKeys++;

if(next.equals(DelimitersEnum.RIGHT_KEY.getValue())) numberRightKeys++;
numberLeftKeys = checkNumberOfLeftKeys(numberLeftKeys, next);
numberRightKeys = checkIfExpressionisARightKey(numberRightKeys, next);
numberLeftBrackets = checkIfExpressionIsALeftBracket(numberLeftBrackets, next);
numberRightBrackets = checkIfExpressionIsARightBracket(numberRightBrackets, next);

if(next.equals(DelimitersEnum.LEFT_BRACKETS.getValue())) numberLeftBrackets++;

if(next.equals(DelimitersEnum.RIGHT_BRACKETS.getValue())) numberRightBrackets++;

if((next.equals(DelimitersEnum.COMMA.getValue()) && hasEqualNumberOfKeysOrBrackets(numberLeftKeys, numberRightKeys) && hasEqualNumberOfKeysOrBrackets(numberLeftBrackets, numberRightBrackets))
|| (next.equals(DelimitersEnum.RIGHT_KEY.getValue()) && hasMoreRightKeys(numberLeftKeys, numberRightKeys))) {
if(checkIfJsonHasEqualNumberExpressionsBeforeComma(numberLeftKeys, numberRightKeys, numberLeftBrackets,
numberRightBrackets, next) || checkIfLastKeyIsARightKey(numberLeftKeys, numberRightKeys, next)) {

if (next.equals(DelimitersEnum.COMMA.getValue())) jsonObjectPattern = jsonObjectPattern.concat(next);
break;
}
Expand All @@ -509,6 +507,103 @@ private String filterJSONObjectFromString(String invalidJson, String jsonObjectP
return new StringBuilder(jsonObjectPattern).toString();
}

/**
* Method that checks if the last character of json is a right key and if the
* expression has more right keys than left keys.
*
* @author Mariana Azevedo
* @since 27/05/2019
*
* @param numberLeftKeys
* @param numberRightKeys
* @param next
* @return
*/
private boolean checkIfLastKeyIsARightKey(int numberLeftKeys, int numberRightKeys, String next) {
return next.equals(DelimitersEnum.RIGHT_KEY.getValue()) && hasMoreRightKeys(numberLeftKeys, numberRightKeys);
}

/**
* Method that checks if the json has a equal number of right keys and left keys or left brackets and right brackets,
* before a comma. If the test is true, it means that the analysis has reached the end of the original string.
*
* @author Mariana Azevedo
* @since 27/05/2019
*
* @param numberLeftKeys
* @param numberRightKeys
* @param numberLeftBrackets
* @param numberRightBrackets
* @param next
* @return
*/
private boolean checkIfJsonHasEqualNumberExpressionsBeforeComma(int numberLeftKeys, int numberRightKeys, int numberLeftBrackets,
int numberRightBrackets, String next) {

return next.equals(DelimitersEnum.COMMA.getValue()) && hasEqualNumberOfKeysOrBrackets(numberLeftKeys, numberRightKeys)
&& hasEqualNumberOfKeysOrBrackets(numberLeftBrackets, numberRightBrackets);
}

/**
* Method that checks if the expression to be read is a right bracket.
*
* @author Mariana Azevedo
* @since 27/05/2019
*
* @param numberRightBrackets
* @param next
* @return
*/
private int checkIfExpressionIsARightBracket(int numberRightBrackets, String next) {
if(next.equals(DelimitersEnum.RIGHT_BRACKETS.getValue())) numberRightBrackets++;
return numberRightBrackets;
}

/**
* Method that checks if the expression to be read is a left bracket.
*
* @author Mariana Azevedo
* @since 27/05/2019
*
* @param numberLeftBrackets
* @param next
* @return
*/
private int checkIfExpressionIsALeftBracket(int numberLeftBrackets, String next) {
if(next.equals(DelimitersEnum.LEFT_BRACKETS.getValue())) numberLeftBrackets++;
return numberLeftBrackets;
}

/**
* Method that checks if the expression to be read is a right key.
*
* @author Mariana Azevedo
* @since 27/05/2019
*
* @param numberRightKeys
* @param next
* @return
*/
private int checkIfExpressionisARightKey(int numberRightKeys, String next) {
if(next.equals(DelimitersEnum.RIGHT_KEY.getValue())) numberRightKeys++;
return numberRightKeys;
}

/**
* Method that checks if the expression to be read is a left key.
*
* @author Mariana Azevedo
* @since 27/05/2019
*
* @param numberLeftKeys
* @param next
* @return
*/
private int checkNumberOfLeftKeys(int numberLeftKeys, String next) {
if(next.equals(DelimitersEnum.LEFT_KEY.getValue())) numberLeftKeys++;
return numberLeftKeys;
}

/**
* Method that remove a list of json object/json array patterns from the string
*
Expand Down

0 comments on commit ef790e7

Please sign in to comment.