Added logging
This commit is contained in:
@@ -1,25 +1,34 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/CipherStreamJava/monoSubstitution/Atbash.java
|
||||
//Mattrixwv
|
||||
// Created: 07-25-21
|
||||
//Modified: 02-22-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
public class Atbash{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Atbash.class);
|
||||
|
||||
private String inputString; //Holds the string that needs encoded or decoded
|
||||
private String outputString; //The encoded/decoded string
|
||||
private boolean preserveCapitals; //Whether to respect capitals in the output string
|
||||
private boolean preserveWhitespace; //Whether to respect whitespace in the output string
|
||||
private boolean preserveSymbols; //Whether to respect symbols in the output string
|
||||
|
||||
|
||||
//Encodes inputString and stores in outputString
|
||||
private String encode(){
|
||||
logger.debug("Encoding");
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Step through every element in the inputString and shift it the correct amount
|
||||
for(int cnt = 0;cnt < inputString.length();++cnt){
|
||||
char currentChar = inputString.charAt(cnt);
|
||||
logger.debug("Encoding char {}", currentChar);
|
||||
//Decode if the letter is alphabetic
|
||||
if(Character.isAlphabetic(currentChar)){
|
||||
//Use either uppercase or lowercase for the base
|
||||
@@ -37,6 +46,8 @@ public class Atbash{
|
||||
}
|
||||
}
|
||||
|
||||
//Return the output
|
||||
logger.debug("Saving output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
return outputString;
|
||||
}
|
||||
@@ -46,15 +57,23 @@ public class Atbash{
|
||||
throw new NullPointerException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing case");
|
||||
|
||||
//Convert all letters to lowercase
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
//Remove all characters except capital letters
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
//Remove all non-alpha numeric and whitespace symbols
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
@@ -62,12 +81,15 @@ public class Atbash{
|
||||
//Save the string
|
||||
this.inputString = inputString;
|
||||
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
|
||||
if(this.inputString.isBlank()){
|
||||
throw new InvalidInputException("Input must contain at least 1 character");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Constructor
|
||||
public Atbash(){
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
@@ -80,22 +102,32 @@ public class Atbash{
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Encodes inputString and returns the result
|
||||
public String encode(String inputString) throws InvalidInputException{
|
||||
//Make sure everything is empty before you begin
|
||||
reset();
|
||||
setInputString(inputString);
|
||||
return encode();
|
||||
}
|
||||
//Decodes inputString and returns the result
|
||||
public String decode(String inputString) throws InvalidInputException{
|
||||
return encode(inputString);
|
||||
}
|
||||
|
||||
//Returns the cleaned input string
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
//Returns the output string
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all of the variables are empty
|
||||
public void reset(){
|
||||
inputString = outputString = "";
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user