Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Log update should be ready to merge in Master branch #7

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
Expand Up @@ -4,83 +4,88 @@

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class StringCalculator {
/* private static final Logger logger = LogManager.getLogger(StringCalculator.class());*/
public StringCalculator() {
}
private static final Logger logger = LogManager.getLogger(StringCalculator.class);

/* private static final Logger LOGGER = LogManager.getLogger(StringCalculator.class());*/
public StringCalculator() {
}

public int add(final String numbers) {
//default delimiter
String delimiter = ",|\n";
String numbersUpdated = numbers;
private static final Logger LOGGER = LogManager.getLogger(StringCalculator.class);

//if it begins by '//' then it's a personalize delimiter(s)
if (numbers.startsWith("//")) {
logger.info("délimiteur sprécial");
delimiter = this.extractDelimiter(numbers);
//"cut" the beginning of the string so that the personalize delimiter is removed
numbersUpdated = numbers.substring(numbers.indexOf("\n") + 1);
}
return add(numbersUpdated, delimiter);

public int add(final String numbers) {
//default delimiter
String delimiter = ",|\n";
String numbersUpdated = numbers;

//if it begins by '//' then it's a personalize delimiter(s)
if (numbers.startsWith("//")) {
delimiter = this.extractDelimiter(numbers);
LOGGER.info("délimiteur sprécial : '"+ delimiter +"'");
//"cut" the beginning of the string so that the personalize delimiter is removed
numbersUpdated = numbers.substring(numbers.indexOf("\n") + 1);
}
return add(numbersUpdated, delimiter);
}

private int add(final String numbersWithoutDelimiter, final String delimiter) {
int returnValue = 0;
List<Integer> negativeNumbers = new ArrayList();

//a string tab containing the numbers without the delimiter(s) is created
String[] numbersArray = numbersWithoutDelimiter.split(delimiter);

for (String number : numbersArray) {
//test if the numbers array is not empty
if (!(number.trim().length() == 0)) {
Integer numberInt = Integer.parseInt(number);
if (numberInt < 0) {
logger.fatal("nombre négatif");
negativeNumbers.add(numberInt);
} else if (numberInt <= 1000) {
logger.warn("plus grand que 1000");
returnValue += numberInt;
}
}
private int add(final String numbersWithoutDelimiter, final String delimiter) {
int returnValue = 0;
List<Integer> negativeNumbers = new ArrayList();

if (negativeNumbers.size() > 0) {
throw new RuntimeException("Negative not allowed: " + negativeNumbers.toString());
//a string tab containing the numbers without the delimiter(s) is created
String[] numbersArray = numbersWithoutDelimiter.split(delimiter);

for (String number : numbersArray) {
//test if the numbers array is not empty
if (!(number.trim().length() == 0)) {
Integer numberInt = Integer.parseInt(number);
if (numberInt < 0) {
negativeNumbers.add(numberInt);
} else if (numberInt <= 1000) {
returnValue += numberInt;
} else {
LOGGER.warn("Nombre plus grand que 1000");
}
}
}

return returnValue;
}
if (negativeNumbers.size() > 0) {
LOGGER.fatal("Attention nombre négatif :" + negativeNumbers.toString());
throw new RuntimeException("Negative not allowed: " + negativeNumbers.toString());

}

private String extractDelimiter(String numbers) {
String[] delimiters;
String returnDelimiter;
int beginDelimiterIndex = numbers.indexOf("//") + 2;
int endDelimiterIndex = numbers.indexOf("\n");
//if it begin with '[' and end by ']' there are multiple delimiters
if(numbers.charAt(beginDelimiterIndex) == '[' && numbers.charAt(endDelimiterIndex - 1) == ']') {
//use an Apache library to get each delimiters between '[' and ']'
delimiters = StringUtils.substringsBetween(numbers.substring(beginDelimiterIndex, endDelimiterIndex), "[", "]");
returnDelimiter = "";
//iterate through the delimiters list and concatenate the delimiter with a '|' between
//them so that the split method is OK with the regex
for (String element: delimiters) {
returnDelimiter += element;
returnDelimiter += "|";
}
returnDelimiter = StringUtils.removeEnd(returnDelimiter, "|");
} else {
//there is only one delimiter
returnDelimiter = numbers.substring(beginDelimiterIndex, endDelimiterIndex);
}
return returnValue;
}

return returnDelimiter;
private String extractDelimiter(String numbers) {
String[] delimiters;
String returnDelimiter;
int beginDelimiterIndex = numbers.indexOf("//") + 2;
int endDelimiterIndex = numbers.indexOf("\n");
//if it begin with '[' and end by ']' there are multiple delimiters
if (numbers.charAt(beginDelimiterIndex) == '['
&& numbers.charAt(endDelimiterIndex - 1) == ']') {
//use an Apache library to get each delimiters between '[' and ']'
delimiters = StringUtils
.substringsBetween(numbers.substring(beginDelimiterIndex, endDelimiterIndex), "[", "]");
returnDelimiter = "";
//iterate through the delimiters list and concatenate the delimiter with a '|' between
//them so that the split method is OK with the regex
for (String element : delimiters) {
returnDelimiter += element;
returnDelimiter += "|";
}
returnDelimiter = StringUtils.removeEnd(returnDelimiter, "|");
} else {
//there is only one delimiter
returnDelimiter = numbers.substring(beginDelimiterIndex, endDelimiterIndex);
}

return returnDelimiter;
}
}
48 changes: 0 additions & 48 deletions src/main/resources/exemple.xml

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/resources/log4j.xml

This file was deleted.

24 changes: 24 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<File name="warn" fileName="log/warning.log">
<LevelRangeFilter minLevel="WARN" maxLevel="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %c - %msg%n"/>
</File>
<File name="fatal" fileName="log/fatal.log">
<LevelRangeFilter minLevel="FATAL" maxLevel="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %c - %msg%n"/>
</File>
<Console name="info" target="SYSTEM_OUT">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%highlight{%d{HH:mm:ss} [%t] %-5level %c - %msg%n}{FATAL=red, WARN=yellow, INFO=black}" disableAnsi="false"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="warn"/>
<AppenderRef ref="fatal"/>
<AppenderRef ref="info"/>
</Root>
</Loggers>
</Configuration>