Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Caesar.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-25-21
|
||||
//Modified: 05-04-23
|
||||
//Modified: 08-11-24
|
||||
/*
|
||||
Copyright (C) 2024 Mattrixwv
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.mattrixwv.cipherstream.monosubstitution;
|
||||
|
||||
|
||||
@@ -11,19 +27,39 @@ import org.slf4j.LoggerFactory;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using the Caesar cipher.
|
||||
*
|
||||
* <p>
|
||||
* The Caesar cipher is a substitution cipher where each letter in the
|
||||
* plaintext is shifted a fixed number of places down or up the alphabet.
|
||||
* This class allows you to encode and decode strings with options to preserve
|
||||
* capitalization, whitespace, and symbols.
|
||||
* </p>
|
||||
*/
|
||||
public class Caesar{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Caesar.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected int shift; //The amount that you need to shift each letter
|
||||
//Settings
|
||||
protected boolean preserveCapitals; //Persist capitals in the output string
|
||||
protected boolean preserveWhitespace; //Persist whitespace in the output string
|
||||
protected boolean preserveSymbols; //Persist symbols in the output string
|
||||
//?Fields
|
||||
/** The string that needs encoded/decoded */
|
||||
protected String inputString;
|
||||
/** The encoded/decoded string */
|
||||
protected String outputString;
|
||||
/** The amount that you need to shift each letter */
|
||||
protected int shift;
|
||||
//?Settings
|
||||
/** Persist capitals in the output string */
|
||||
protected boolean preserveCapitals;
|
||||
/** Persist whitespace in the output string */
|
||||
protected boolean preserveWhitespace;
|
||||
/** Persist symbols in the output string */
|
||||
protected boolean preserveSymbols;
|
||||
|
||||
|
||||
//Sets shift and makes sure it is within the propper bounds
|
||||
/**
|
||||
* Sets the shift amount and ensures it is within the proper bounds (0-25).
|
||||
*
|
||||
* @param shiftAmount the amount to shift each letter
|
||||
*/
|
||||
protected void setShift(int shiftAmount){
|
||||
logger.debug("Setting shift {}", shiftAmount);
|
||||
|
||||
@@ -32,7 +68,13 @@ public class Caesar{
|
||||
|
||||
logger.debug("Cleaned shift {}", shift);
|
||||
}
|
||||
//Sets the input string
|
||||
/**
|
||||
* Sets the input string for encoding or decoding, applying removal options
|
||||
* for case, whitespace, and symbols.
|
||||
*
|
||||
* @param inputString the string to be processed
|
||||
* @throws InvalidInputException if the input string is null or blank after processing
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -63,7 +105,9 @@ public class Caesar{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the input string by shifting letters according to the Caesar cipher.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -111,7 +155,9 @@ public class Caesar{
|
||||
outputString = output.toString();
|
||||
logger.debug("Saving encoded string '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the input string by reversing the shift applied during encoding.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -164,13 +210,23 @@ public class Caesar{
|
||||
logger.debug("Saving decoded string '{}'", outputString);
|
||||
}
|
||||
|
||||
//Constructor
|
||||
//?Constructor
|
||||
/**
|
||||
* Constructs a new {@code Caesar} instance with default settings.
|
||||
*/
|
||||
public Caesar(){
|
||||
reset();
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Caesar} instance with specified settings.
|
||||
*
|
||||
* @param preserveCapitals whether to preserve capitalization in the output
|
||||
* @param preserveWhitespace whether to preserve whitespace in the output
|
||||
* @param preserveSymbols whether to preserve symbols in the output
|
||||
*/
|
||||
public Caesar(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
reset();
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
@@ -178,7 +234,14 @@ public class Caesar{
|
||||
this.preserveSymbols = preserveSymbols;
|
||||
}
|
||||
|
||||
//Sets the shift and inputString and encodes the message
|
||||
/**
|
||||
* Encodes the input string with the specified shift amount.
|
||||
*
|
||||
* @param shiftAmount the amount to shift each letter
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(int shiftAmount, String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
@@ -186,7 +249,14 @@ public class Caesar{
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Sets the shift and inputString and decodes the message
|
||||
/**
|
||||
* Decodes the input string with the specified shift amount.
|
||||
*
|
||||
* @param shiftAmount the amount to shift each letter
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(int shiftAmount, String inputString) throws InvalidInputException{
|
||||
reset();
|
||||
setShift(shiftAmount);
|
||||
@@ -195,17 +265,34 @@ public class Caesar{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current shift amount.
|
||||
*
|
||||
* @return the shift amount
|
||||
*/
|
||||
public int getShift(){
|
||||
return shift;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets the internal fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user