Added Javadoc comments to the classes
This commit is contained in:
@@ -1,7 +1,23 @@
|
||||
//CipherStreamJava/src/main/java/com/mattrixwv/cipherstream/monosubstitution/Substitution.java
|
||||
//Mattrixwv
|
||||
// Created: 02-22-22
|
||||
//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;
|
||||
|
||||
|
||||
@@ -12,19 +28,41 @@ import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
|
||||
|
||||
/**
|
||||
* A class for encoding and decoding strings using a substitution cipher.
|
||||
* This cipher can handle both alphabetic and alphanumeric keys and supports
|
||||
* preserving or removing capitalization, whitespace, and symbols.
|
||||
*
|
||||
* <p>
|
||||
* The substitution cipher replaces each letter or digit in the input string
|
||||
* with a corresponding character from a key. The key must contain all letters
|
||||
* of the alphabet and optionally digits, without duplicates.
|
||||
* </p>
|
||||
*/
|
||||
public class Substitution{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Substitution.class);
|
||||
//Fields
|
||||
protected String inputString; //The string that needs encoded/decoded
|
||||
protected String outputString; //The encoded/decoded string
|
||||
protected String keyword; //The keyword used to encode/decode the input
|
||||
//Getters
|
||||
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 keyword used to encode/decode the input */
|
||||
protected String keyword;
|
||||
//?Getters
|
||||
/** 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;
|
||||
|
||||
|
||||
//Ensures key constraints are followed
|
||||
/**
|
||||
* Ensures all key constraints are followed.
|
||||
*
|
||||
* @param key the key to be used for encoding/decoding
|
||||
* @throws InvalidKeywordException if the key is null, contains duplicates, or has invalid length
|
||||
*/
|
||||
protected void setKeyword(String key) throws InvalidKeywordException{
|
||||
if(key == null){
|
||||
throw new InvalidKeywordException("Key cannot be null");
|
||||
@@ -71,7 +109,12 @@ public class Substitution{
|
||||
logger.debug("Cleaned key '{}'", key);
|
||||
this.keyword = key;
|
||||
}
|
||||
//Ensure intput constraints are followed
|
||||
/**
|
||||
* Ensures all input constraints are followed.
|
||||
*
|
||||
* @param inputString the string to be encoded/decoded
|
||||
* @throws InvalidInputException if the input string is null or blank
|
||||
*/
|
||||
protected void setInputString(String inputString) throws InvalidInputException{
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
@@ -105,7 +148,9 @@ public class Substitution{
|
||||
throw new InvalidInputException("Input must contain at least 1 letter");
|
||||
}
|
||||
}
|
||||
//Encodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Encodes the inputString using the provided key and stores the result in outputString.
|
||||
*/
|
||||
protected void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
@@ -137,7 +182,9 @@ public class Substitution{
|
||||
this.outputString = output.toString();
|
||||
logger.debug("Encoded message '{}'", outputString);
|
||||
}
|
||||
//Decodes the inputString and stores the result in outputString
|
||||
/**
|
||||
* Decodes the inputString using the provided key and stores the result in outputString.
|
||||
*/
|
||||
protected void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
@@ -173,13 +220,23 @@ public class Substitution{
|
||||
logger.debug("Decoded message '{}'", outputString);
|
||||
}
|
||||
|
||||
//Constructors
|
||||
//?Constructors
|
||||
/**
|
||||
* Constructs a new {@code Substitution} instance with default settings.
|
||||
*/
|
||||
public Substitution(){
|
||||
preserveCapitals = false;
|
||||
preserveWhitespace = false;
|
||||
preserveSymbols = false;
|
||||
reset();
|
||||
}
|
||||
/**
|
||||
* Constructs a new {@code Substitution} 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 Substitution(boolean preserveCapitals, boolean preserveWhitespace, boolean preserveSymbols){
|
||||
this.preserveCapitals = preserveCapitals;
|
||||
this.preserveWhitespace = preserveWhitespace;
|
||||
@@ -187,14 +244,30 @@ public class Substitution{
|
||||
reset();
|
||||
}
|
||||
|
||||
//Encodes inputString using the provided key
|
||||
/**
|
||||
* Encodes the inputString using the provided key.
|
||||
*
|
||||
* @param key the key to use for encoding
|
||||
* @param inputString the string to be encoded
|
||||
* @return the encoded string
|
||||
* @throws InvalidKeywordException if the key is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String encode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKeyword(key);
|
||||
setInputString(inputString);
|
||||
encode();
|
||||
return outputString;
|
||||
}
|
||||
//Decodes inputString using the provided key
|
||||
/**
|
||||
* Decodes the inputString using the provided key.
|
||||
*
|
||||
* @param key the key to use for decoding
|
||||
* @param inputString the string to be decoded
|
||||
* @return the decoded string
|
||||
* @throws InvalidKeywordException if the key is invalid
|
||||
* @throws InvalidInputException if the input string is invalid
|
||||
*/
|
||||
public String decode(String key, String inputString) throws InvalidKeywordException, InvalidInputException{
|
||||
setKeyword(key);
|
||||
setInputString(inputString);
|
||||
@@ -202,17 +275,34 @@ public class Substitution{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
//Getters
|
||||
//?Getters
|
||||
/**
|
||||
* Gets the current input string.
|
||||
*
|
||||
* @return the input string
|
||||
*/
|
||||
public String getInputString(){
|
||||
return inputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current output string.
|
||||
*
|
||||
* @return the output string
|
||||
*/
|
||||
public String getOutputString(){
|
||||
return outputString;
|
||||
}
|
||||
/**
|
||||
* Gets the current keyword.
|
||||
*
|
||||
* @return the keyword
|
||||
*/
|
||||
public String getKeyword(){
|
||||
return keyword;
|
||||
}
|
||||
//Makes sure all variables are empty
|
||||
/**
|
||||
* Resets all fields to their default values.
|
||||
*/
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user