Updated for SonarQube findings

This commit is contained in:
2022-07-03 17:45:08 -04:00
parent a7333c5f3e
commit 4b738d2817
11 changed files with 3896 additions and 962 deletions

View File

@@ -1,7 +1,7 @@
//Matrix/src/main/java/com/mattrixwv/BigIntegerMatrix.java
//Mattrixwv
// Created: 02-10-22
//Modified: 06-29-22
//Modified: 06-30-22
package com.mattrixwv.matrix;
@@ -13,6 +13,7 @@ import com.mattrixwv.matrix.exceptions.InvalidCoordinatesException;
import com.mattrixwv.matrix.exceptions.InvalidGeometryException;
import com.mattrixwv.matrix.exceptions.InvalidRowSizeException;
import com.mattrixwv.matrix.exceptions.InvalidScalarException;
import com.mattrixwv.matrix.exceptions.NullMatrixException;
public class BigIntegerMatrix{
@@ -142,9 +143,15 @@ public class BigIntegerMatrix{
if(row >= grid.length){
throw new InvalidCoordinatesException("Row cannot be greater than the number of rows");
}
else if(row < 0){
throw new InvalidCoordinatesException("Row cannot be less than 0");
}
else if(col >= grid[0].length){
throw new InvalidCoordinatesException("Column cannot be greater than the number of columns");
}
else if(col < 0){
throw new InvalidCoordinatesException("Column cannot be less than 0");
}
//Return the location in the grid
return grid[row][col];
@@ -163,12 +170,7 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newRow);
}
public int getNumRows(){
if(grid == null){
return 0;
}
else{
return grid.length;
}
return grid.length;
}
public BigIntegerMatrix getCol(int col){
//Make sure the column number is valid
@@ -193,6 +195,7 @@ public class BigIntegerMatrix{
return 0;
}
}
//Sets
public void set(int row, int col, BigInteger value){
//Make sure the row number is valid
@@ -213,7 +216,10 @@ public class BigIntegerMatrix{
throw new InvalidCoordinatesException("Invalid row number " + row);
}
//Make sure the number of elements is valid
if(elements.length != getNumCols()){
if(elements == null){
throw new InvalidGeometryException("Row cannot be null");
}
else if(elements.length != getNumCols()){
throw new InvalidGeometryException(elements.length, getNumCols());
}
@@ -221,6 +227,10 @@ public class BigIntegerMatrix{
grid[row] = Arrays.copyOf(elements, elements.length);
}
public void setRow(int row, BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row
if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Setting a row by Matrix requires the matrix contain a single row");
@@ -235,7 +245,10 @@ public class BigIntegerMatrix{
throw new InvalidCoordinatesException("Invalid column number " + col);
}
//Make sure the number of elements is valid
if(elements.length != grid.length){
if(elements == null){
throw new InvalidGeometryException("Column cannot be null");
}
else if(elements.length != grid.length){
throw new InvalidCoordinatesException(elements.length, grid.length);
}
@@ -245,18 +258,32 @@ public class BigIntegerMatrix{
}
}
public void setCol(int col, BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column
if(matrix.getNumCols() != 1){
else if(matrix.getNumCols() != 1){
throw new InvalidGeometryException("Setting a column by Matrix requires the matrix contain a single column");
}
BigInteger[] vector = new BigInteger[matrix.getNumRows()];
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.grid[cnt][0];
}
//Set the column
setCol(col, matrix.getCol(0));
setCol(col, vector);
}
//Adds
public void addRow(BigInteger[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of columns is valid
if((grid.length == 0) || (getNumCols() == elements.length)){
else if((grid.length == 0) || (getNumCols() == elements.length)){
BigInteger[][] newGrid = new BigInteger[grid.length + 1][elements.length];
//Copy all existing data into the new grid
for(int row = 0;row < grid.length;++row){
@@ -272,8 +299,12 @@ public class BigIntegerMatrix{
grid[grid.length - 1] = Arrays.copyOf(elements, elements.length);
}
public void addRow(BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single row
if(matrix.getNumRows() != 1){
else if(matrix.getNumRows() != 1){
throw new InvalidGeometryException("Adding a row by Matrix requires the matrix contain a single row");
}
@@ -281,9 +312,16 @@ public class BigIntegerMatrix{
addRow(matrix.grid[0]);
}
public void addCol(BigInteger[] elements){
//Make sure the matrix isn't null
if(elements == null){
throw new NullMatrixException();
}
//Make sure the number of rows is valid
if(grid.length == 0){
grid = new BigInteger[elements.length][1];
for(int row = 0;row < grid.length;++row){
grid[row][0] = elements[row];
}
}
else if(grid.length == elements.length){
//Copy all existing data into the new grid
@@ -297,17 +335,30 @@ public class BigIntegerMatrix{
}
}
public void addCol(BigIntegerMatrix matrix){
//Make sure the matrix isn't null
if(matrix == null){
throw new NullMatrixException();
}
//Make sure the matrix has a single column
if(matrix.getNumCols() != 1){
else if(matrix.getNumCols() != 1){
throw new InvalidGeometryException("Adding a column by Matrix requires the matrix contain a single column");
}
BigInteger[] vector = new BigInteger[matrix.getNumRows()];
for(int cnt = 0;cnt < matrix.getNumRows();++cnt){
vector[cnt] = matrix.get(cnt, 0);
}
//Add the column
addCol(matrix.getCol(0));
addCol(vector);
}
public BigIntegerMatrix appendRight(BigIntegerMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of rows
if(getNumRows() != rightSide.getNumRows()){
else if(getNumRows() != rightSide.getNumRows()){
throw new InvalidGeometryException("Invalid number of rows. " + rightSide.getNumRows() + " must be " + getNumRows());
}
@@ -327,8 +378,12 @@ public class BigIntegerMatrix{
return new BigIntegerMatrix(newGrid);
}
public BigIntegerMatrix appendBottom(BigIntegerMatrix rightSide){
//Make sure the matrix isn't null
if(rightSide == null){
throw new NullMatrixException();
}
//Make sure the matrices have the same number of columns
if(getNumCols() != rightSide.getNumCols()){
else if(getNumCols() != rightSide.getNumCols()){
throw new InvalidGeometryException("Invalid number of columns. " + rightSide.getNumCols() + " must be " + getNumCols());
}