Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

# Reformat all Java sources with palantir-java-format
f490a4f2627cc093ad2cfa4ba93809556254e7de

# Add final to parameters, locals, fields and static-only classes
5c6d4e98af79d60fc12d93bbfba6b9dac8406b8f
15 changes: 15 additions & 0 deletions config/pmd/final-modifiers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="final-modifiers"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Use final wherever a variable or class is never changed. The "could be final" rules only flag parameters and
locals that are never reassigned, so code that deliberately reassigns stays as it is.
</description>

<rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal"/>
<rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal"/>
<rule ref="category/java/design.xml/ImmutableField"/>
<rule ref="category/java/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal"/>
</ruleset>
33 changes: 33 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<jacoco-maven-plugin.version>0.8.16</jacoco-maven-plugin.version>
<spotless-maven-plugin.version>3.11.0</spotless-maven-plugin.version>
<palantir-java-format.version>2.103.0</palantir-java-format.version>
<maven-pmd-plugin.version>3.28.0</maven-pmd-plugin.version>
<pmd.version>7.29.0</pmd.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -115,6 +117,37 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven-pmd-plugin.version}</version>
<configuration>
<rulesets>
<ruleset>config/pmd/final-modifiers.xml</ruleset>
</rulesets>
<includeTests>true</includeTests>
<printFailingErrors>true</printFailingErrors>
<linkXRef>false</linkXRef>
</configuration>
<dependencies>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-core</artifactId>
<version>${pmd.version}</version>
</dependency>
<dependency>
<groupId>net.sourceforge.pmd</groupId>
<artifactId>pmd-java</artifactId>
<version>${pmd.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${maven-enforcer-plugin.version}</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

public class Element {

private String name;
private final String name;

public Element(String name) {
public Element(final String name) {
this.name = name;
}

Expand All @@ -13,11 +13,11 @@ public String getName() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
if (obj == null) {
return false;
}
Element other = (Element) obj;
final Element other = (Element) obj;
return other.getName().equals(this.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import java.util.Random;

public class ArrayUtils {
public final class ArrayUtils {

private ArrayUtils() {}

private static Random rand = new Random();
private static final Random rand = new Random();

/**
* Function for doing an in-place ↴ shuffle of an array.
Expand All @@ -19,7 +19,7 @@ private ArrayUtils() {}
*
* @param theArray the array to shuffle
*/
public static void shuffle(int[] theArray) {
public static void shuffle(final int[] theArray) {
// if it's 1 or 0 items, just return
if (theArray.length <= 1) {
return;
Expand All @@ -32,18 +32,18 @@ public static void shuffle(int[] theArray) {
// (could also be the item currently in that spot)
// must be an item AFTER the current item, because the stuff
// before has all already been placed
int randomChoiceIndex = getRandom(indexWeAreChoosingFor, theArray.length - 1);
final int randomChoiceIndex = getRandom(indexWeAreChoosingFor, theArray.length - 1);

// place our random choice in the spot by swapping
if (randomChoiceIndex != indexWeAreChoosingFor) {
int valueAtIndexWeChoseFor = theArray[indexWeAreChoosingFor];
final int valueAtIndexWeChoseFor = theArray[indexWeAreChoosingFor];
theArray[indexWeAreChoosingFor] = theArray[randomChoiceIndex];
theArray[randomChoiceIndex] = valueAtIndexWeChoseFor;
}
}
}

private static int getRandom(int floor, int ceiling) {
private static int getRandom(final int floor, final int ceiling) {
return rand.nextInt((ceiling - floor) + 1) + floor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ public class BinaryTreeNode {
public BinaryTreeNode left;
public BinaryTreeNode right;

public BinaryTreeNode(int value) {
public BinaryTreeNode(final int value) {
this.value = value;
}

public BinaryTreeNode insertLeft(int leftValue) {
public BinaryTreeNode insertLeft(final int leftValue) {
this.left = new BinaryTreeNode(leftValue);
return this.left;
}

public BinaryTreeNode insertRight(int rightValue) {
public BinaryTreeNode insertRight(final int rightValue) {
this.right = new BinaryTreeNode(rightValue);
return this.right;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class LinkedList {

public void addAtStart(final Element element) {
if (this.totalElements == 0) {
LinkedListNode newNode = new LinkedListNode(element);
final LinkedListNode newNode = new LinkedListNode(element);
this.first = newNode;
this.last = newNode;
} else {
LinkedListNode newNode = new LinkedListNode(element, this.first);
final LinkedListNode newNode = new LinkedListNode(element, this.first);
this.first.setPrevious(newNode);
this.first = newNode;
}
Expand All @@ -26,7 +26,7 @@ public void addAtEnd(final Element element) {
if (this.totalElements == 0) {
addAtStart(element);
} else {
LinkedListNode newNode = new LinkedListNode(element);
final LinkedListNode newNode = new LinkedListNode(element);
this.last.setNext(newNode);
newNode.setPrevious(this.last);
this.last = newNode;
Expand All @@ -41,10 +41,10 @@ public void add(final int position, final Element element) {
} else if (position == this.totalElements) {
addAtEnd(element);
} else {
LinkedListNode previous = this.getNode(position - 1);
LinkedListNode next = previous.getNext();
final LinkedListNode previous = this.getNode(position - 1);
final LinkedListNode next = previous.getNext();

LinkedListNode newNode = new LinkedListNode(element, previous.getNext());
final LinkedListNode newNode = new LinkedListNode(element, previous.getNext());
newNode.setPrevious(previous);
previous.setNext(newNode);
next.setPrevious(newNode);
Expand Down Expand Up @@ -93,7 +93,7 @@ public void removeFromEnd() {
if (this.totalElements == 1) {
this.removeFromStart();
} else {
LinkedListNode previous = this.last.getPrevious();
final LinkedListNode previous = this.last.getPrevious();
previous.setNext(null);
this.last = previous;
this.totalElements--;
Expand All @@ -106,9 +106,9 @@ public void remove(final int position) {
} else if (position == totalElements - 1) {
this.removeFromEnd();
} else {
LinkedListNode previous = this.getNode(position - 1);
LinkedListNode current = previous.getNext();
LinkedListNode next = current.getNext();
final LinkedListNode previous = this.getNode(position - 1);
final LinkedListNode current = previous.getNext();
final LinkedListNode next = current.getNext();
previous.setNext(next);
next.setPrevious(previous);

Expand All @@ -120,7 +120,7 @@ public int size() {
return this.totalElements;
}

public boolean contains(Element element) {
public boolean contains(final Element element) {
LinkedListNode current = this.first;
while (current != null) {
if (current.getElement().equals(element)) {
Expand All @@ -138,7 +138,7 @@ public String toString() {
}
LinkedListNode current = this.first;

StringBuilder sb = new StringBuilder("[");
final StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < this.totalElements; i++) {
sb.append(current.getElement().getName());
sb.append(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ public Element getElement() {
return element;
}

public void setElement(Element element) {
public void setElement(final Element element) {
this.element = element;
}

public LinkedListNode getNext() {
return next;
}

public void setNext(LinkedListNode next) {
public void setNext(final LinkedListNode next) {
this.next = next;
}

public LinkedListNode getPrevious() {
return previous;
}

public void setPrevious(LinkedListNode previous) {
public void setPrevious(final LinkedListNode previous) {
this.previous = previous;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Queue {

private final LinkedList<Element> elements = new LinkedList<>();

public void add(Element element) {
public void add(final Element element) {
this.elements.add(element);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@

public class Set {

private List<LinkedList<String>> table = new LinkedList<LinkedList<String>>();
private final List<LinkedList<String>> table = new LinkedList<LinkedList<String>>();

public Set() {
for (int i = 0; i < 26; i++) {
this.table.add(new LinkedList<String>());
}
}

public void add(String word) {
public void add(final String word) {
if (!contains(word)) {
int index = calculateTableIndex(word);
List<String> list = this.table.get(index);
final int index = calculateTableIndex(word);
final List<String> list = this.table.get(index);
list.add(word);
}
}

public void remove(String word) {
public void remove(final String word) {
if (contains(word)) {
int index = calculateTableIndex(word);
List<String> list = this.table.get(index);
final int index = calculateTableIndex(word);
final List<String> list = this.table.get(index);
list.remove(word);
}
}

private boolean contains(String word) {
int index = calculateTableIndex(word);
private boolean contains(final String word) {
final int index = calculateTableIndex(word);
return this.table.get(index).contains(word);
}

private int calculateTableIndex(String word) {
private int calculateTableIndex(final String word) {
return word.toLowerCase().charAt(0) % 26;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Stack {

private final List<Element> elements = new LinkedList<>();

public void push(Element element) {
public void push(final Element element) {
this.elements.add(element);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int size() {

private void guaranteesSpace() {
if (this.totalElements == this.elements.length) {
Element[] newElements = new Element[this.elements.length * 2];
final Element[] newElements = new Element[this.elements.length * 2];
for (int i = 0; i < this.elements.length; i++) {
newElements[i] = this.elements[i];
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/io/github/diegourban/algorithms/io/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class Reader {
private final byte[] buffer;
private int bufferPointer, bytesRead;

public Reader(InputStream input) {
public Reader(final InputStream input) {
din = new DataInputStream(input);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}

public String readLine() throws IOException {
byte[] buf = new byte[10000]; // line length
final byte[] buf = new byte[10000]; // line length
int cnt = 0;
int c = 0;

Expand All @@ -47,7 +47,7 @@ public int nextInt() throws IOException {
c = read();
}

boolean neg = (c == '-');
final boolean neg = (c == '-');
if (neg) {
c = read();
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public long nextLong() throws IOException {
c = read();
}

boolean neg = (c == '-');
final boolean neg = (c == '-');
if (neg) {
c = read();
}
Expand All @@ -106,7 +106,7 @@ public double nextDouble() throws IOException {
c = read();
}

boolean neg = (c == '-');
final boolean neg = (c == '-');
if (neg) {
c = read();
}
Expand All @@ -128,7 +128,7 @@ public double nextDouble() throws IOException {
return ret;
}

public void skip(int n) throws IOException {
public void skip(final int n) throws IOException {
for (int i = 0; i < n; i++) {
read();
}
Expand Down
Loading
Loading