Fixed sonarqube findings

This commit is contained in:
Mattrixwv
2022-07-04 01:04:06 -04:00
parent 6f300a430a
commit b4817e8bb3
47 changed files with 289 additions and 311 deletions

View File

@@ -0,0 +1,151 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 01-16-22
package com.mattrixwv.cipherstream.monosubstitution;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringJoiner;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Baconian{
private static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
));
private String inputString; //The string that needs encoded/decoded
private String outputString; //The encoded/decoded string
private boolean preserveCapitals; //Whether to respect capitals in the output string
//Sets the input string
private void setInputStringEncode(String inputString) throws InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
//Remove all whitespace and symbols
inputString = inputString.replaceAll("[^A-Za-z]", "");
if(!preserveCapitals){
inputString = inputString.toLowerCase();
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
}
private void setInputStringDecode(String inputString) throws InvalidCharacterException, InvalidInputException{
if(inputString == null){
throw new NullPointerException("Input cannot be null");
}
if(!preserveCapitals){
inputString = inputString.toLowerCase();
}
//Check each Baconian Cipher letter for validity
for(String str : inputString.split(" ")){
//Make sure each letter contains 5 characters
if(str.length() != 5){
throw new InvalidCharacterException("All Baconian letters contain exactly 5 characters: " + str);
}
//Make sure the letter contains only a's and b's
String temp = str.replaceAll("[^abAB]", "");
if(!temp.equals(str)){
throw new InvalidCharacterException("Baconian letters contain only a's and b's: " + str);
}
}
this.inputString = inputString;
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be empty");
}
}
//Encodes the inputString and stores the result in outputString
private String encode(){
StringJoiner output = new StringJoiner(" ");
//Go through every character in the inputString and encode it
for(char ch : inputString.toCharArray()){
//Convert the character to a binary string with A = 0
String binary = null;
if(Character.isUpperCase(ch)){
binary = code.get(ch - 'A').toUpperCase();
}
else{
binary = code.get(ch - 'a').toLowerCase();
}
//Add the encoded character to the output
output.add(binary);
}
//Save and return the output
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
StringBuilder output = new StringBuilder();
//Go through every Baconian Cipher character in the inputString and decode it
for(String baconianCharacter : inputString.split(" ")){
//Get the location of the Baconian character in the array
int location = code.indexOf(baconianCharacter.toLowerCase());
//Convert the Baconian character to an ASCII character
char ch;
if(Character.isUpperCase(baconianCharacter.charAt(0))){
ch = (char)(location + 'A');
}
else{
ch = (char)(location + 'a');
}
//Add the decoded character to the output
output.append(ch);
}
//Save and return the output
outputString = output.toString();
return outputString;
}
//Constructor
public Baconian(){
reset();
preserveCapitals = false;
}
public Baconian(boolean preserveCapitals){
reset();
this.preserveCapitals = preserveCapitals;
}
//Returns the outputString
public String getOutputString(){
return outputString;
}
//Returns the inputString
public String getInputString(){
return inputString;
}
//Sets the inputString and encodes the message
public String encode(String inputString) throws InvalidInputException{
reset();
setInputStringEncode(inputString);
return encode();
}
//Sets the inputString and decodes the message
public String decode(String inputString) throws InvalidCharacterException, InvalidInputException{
reset();
setInputStringDecode(inputString);
return decode();
}
//Makes sure all of the variables are empty
public void reset(){
inputString = "";
outputString = "";
}
}