Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing Exception Types Being Thrown #397

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public boolean contains(T value) {

public boolean insert(T val) {
if (val == null) {
throw new IllegalArgumentException("Red-Black tree does not allow null values.");
throw new NullPointerException("Red-Black tree does not allow null values.");
}

Node x = root, y = NIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public boolean isEmpty() {

public boolean insert(T val, int priority) {
if (val == null) {
throw new IllegalArgumentException("TreapTree does not allow null values");
throw new NullPointerException("TreapTree does not allow null values");
}
if (!contains(root, val)) {
root = insert(this.root, val, priority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.williamfiset.algorithms.datastructures.utils.TreePrinter;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Scanner;

/**
Expand All @@ -30,13 +31,7 @@ public static class BinaryTree<T extends Comparable<T>> implements TreePrinter.P
private BinaryTree<T> leftChild, rightChild;

public BinaryTree(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
this.data = Objects.requireNonNull(data);
}

@Override
Expand Down Expand Up @@ -67,13 +62,7 @@ public T getData() {
}

public void setData(T data) {
if (data == null) {
try {
throw new Exception("Null data not allowed into tree");
} catch (Exception e) {
e.printStackTrace();
}
} else this.data = data;
this.data = Objects.requireNonNull(data);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public IntArray() {

// Initialize the array with a certain capacity
public IntArray(int capacity) {
if (capacity < 0) throw new IllegalArgumentException("Illegal Capacity: " + capacity);
if (capacity < 0) throw new NegativeArraySizeException("Illegal Capacity: " + capacity);
this.capacity = capacity;
arr = new int[capacity];
}

// Given an array make it a dynamic array!
public IntArray(int[] array) {
if (array == null) throw new IllegalArgumentException("Array cannot be null");
if (array == null) throw new NullPointerException("Array cannot be null");
arr = java.util.Arrays.copyOf(array, array.length);
capacity = len = array.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FenwickTreeRangeQueryPointUpdate(int sz) {
// does not get used, O(n) construction.
public FenwickTreeRangeQueryPointUpdate(long[] values) {

if (values == null) throw new IllegalArgumentException("Values array cannot be null!");
if (values == null) throw new NullPointerException("Values array cannot be null!");

N = values.length;
values[0] = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class FenwickTreeRangeUpdatePointQuery {
// does not get used, O(n) construction.
public FenwickTreeRangeUpdatePointQuery(long[] values) {

if (values == null) throw new IllegalArgumentException("Values array cannot be null!");
if (values == null) throw new NullPointerException("Values array cannot be null!");

N = values.length;
values[0] = 0L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void moveToRoot(FibonacciHeapNode<E> node) {

public boolean add(E e) {
if (e == null) {
throw new IllegalArgumentException(
throw new NullPointerException(
"Null elements not allowed in this FibonacciHeap implementation.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.williamfiset.algorithms.datastructures.hashtable;

import java.util.Objects;
import java.util.Random;

public class DoubleHashingTestObject implements SecondaryHash {
Expand Down Expand Up @@ -35,15 +36,13 @@ public DoubleHashingTestObject(int data) {
}

public DoubleHashingTestObject(int[] data) {
if (data == null) throw new IllegalArgumentException("Cannot be null");
vectorData = data;
vectorData = Objects.requireNonNull(data);
vectorHash();
computeHash();
}

public DoubleHashingTestObject(String data) {
if (data == null) throw new IllegalArgumentException("Cannot be null");
stringData = data;
stringData = Objects.requireNonNull(data);
stringHash();
computeHash();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ protected static final int gcd(int a, int b) {
// Place a key-value pair into the hash-table. If the value already
// exists inside the hash-table then the value is updated.
public V insert(K key, V val) {
if (key == null) throw new IllegalArgumentException("Null key");
if (key == null) throw new NullPointerException("Null key");
if (usedBuckets >= threshold) resizeTable();

setupProbing(key);
Expand Down Expand Up @@ -229,7 +229,7 @@ public V insert(K key, V val) {

// Returns true/false on whether a given key exists within the hash-table
public boolean hasKey(K key) {
if (key == null) throw new IllegalArgumentException("Null key");
if (key == null) throw new NullPointerException("Null key");

setupProbing(key);
final int offset = normalizeIndex(key.hashCode());
Expand Down Expand Up @@ -273,7 +273,7 @@ public boolean hasKey(K key) {
// NOTE: returns null if the value is null AND also returns
// null if the key does not exists.
public V get(K key) {
if (key == null) throw new IllegalArgumentException("Null key");
if (key == null) throw new NullPointerException("Null key");

setupProbing(key);
final int offset = normalizeIndex(key.hashCode());
Expand Down Expand Up @@ -319,7 +319,7 @@ public V get(K key) {
// NOTE: returns null if the value is null AND also returns
// null if the key does not exists.
public V remove(K key) {
if (key == null) throw new IllegalArgumentException("Null key");
if (key == null) throw new NullPointerException("Null key");

setupProbing(key);
final int offset = normalizeIndex(key.hashCode());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public V add(K key, V value) {

public V insert(K key, V value) {

if (key == null) throw new IllegalArgumentException("Null key");
if (key == null) throw new NullPointerException("Null key");
Entry<K, V> newEntry = new Entry<>(key, value);
int bucketIndex = normalizeIndex(newEntry.hash);
return bucketInsertEntry(bucketIndex, newEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ private class KDNode<E extends Comparable<E>> {
private KDNode<E> right;

public KDNode(E[] coords) {
if (coords == null) throw new IllegalArgumentException("Error: Null coordinate set passed");
if (coords == null) throw new NullPointerException("Error: Null coordinate set passed");
if (coords.length != k)
throw new IllegalArgumentException(
"Error: Expected " + k + "dimensions, but given " + coords.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ public void addFirst(T elem) {
}

// Add an element at a specified index
public void addAt(int index, T data) throws Exception {
public void addAt(int index, T data) {
if (index < 0 || index > size) {
throw new Exception("Illegal Index");
throw new IndexOutOfBoundsException(index);
}
if (index == 0) {
addFirst(data);
Expand All @@ -105,20 +105,20 @@ public void addAt(int index, T data) throws Exception {

// Check the value of the first node if it exists, O(1)
public T peekFirst() {
if (isEmpty()) throw new RuntimeException("Empty list");
if (isEmpty()) throw new IllegalStateException("Empty list");
return head.data;
}

// Check the value of the last node if it exists, O(1)
public T peekLast() {
if (isEmpty()) throw new RuntimeException("Empty list");
if (isEmpty()) throw new IllegalStateException("Empty list");
return tail.data;
}

// Remove the first value at the head of the linked list, O(1)
public T removeFirst() {
// Can't remove data from an empty list
if (isEmpty()) throw new RuntimeException("Empty list");
if (isEmpty()) throw new IllegalStateException("Empty list");

// Extract the data at the head and move
// the head pointer forwards one node
Expand All @@ -139,7 +139,7 @@ public T removeFirst() {
// Remove the last value at the tail of the linked list, O(1)
public T removeLast() {
// Can't remove data from an empty list
if (isEmpty()) throw new RuntimeException("Empty list");
if (isEmpty()) throw new IllegalStateException("Empty list");

// Extract the data at the tail and move
// the tail pointer backwards one node
Expand Down Expand Up @@ -185,7 +185,7 @@ private T remove(Node<T> node) {
public T removeAt(int index) {
// Make sure the index provided is valid
if (index < 0 || index >= size) {
throw new IllegalArgumentException();
throw new IndexOutOfBoundsException(index);
}

int i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class BinaryHeap<T extends Comparable<T>> {

Expand Down Expand Up @@ -90,9 +91,7 @@ public boolean contains(T elem) {
// element must not be null, O(log(n))
public void add(T elem) {

if (elem == null) throw new IllegalArgumentException();

heap.add(elem);
heap.add(Objects.requireNonNull(elem));

int indexOfLastElem = size() - 1;
swim(indexOfLastElem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

Expand Down Expand Up @@ -106,9 +107,7 @@ public boolean contains(T elem) {
// element must not be null, O(log(n))
public void add(T elem) {

if (elem == null) throw new IllegalArgumentException();

heap.add(elem);
heap.add(Objects.requireNonNull(elem));
int indexOfLastElem = size() - 1;
mapAdd(elem, indexOfLastElem);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package com.williamfiset.algorithms.datastructures.priorityqueue;

import java.util.Objects;

@SuppressWarnings("unchecked")
public class MinDHeap<T extends Comparable<T>> {

Expand Down Expand Up @@ -61,8 +63,7 @@ public T poll() {

// Adds a none null element to the priority queue
public void add(T elem) {
if (elem == null) throw new IllegalArgumentException("No null elements please :)");
heap[sz] = elem;
heap[sz] = Objects.requireNonNull(elem);
swim(sz);
sz++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;

public class MinIndexedDHeap<T extends Comparable<T>> {

Expand Down Expand Up @@ -104,7 +105,7 @@ public T pollMinValue() {

public void insert(int ki, T value) {
if (contains(ki)) throw new IllegalArgumentException("index already exists; received: " + ki);
valueNotNullOrThrow(value);
Objects.requireNonNull(value);
pm[ki] = sz;
im[sz] = ki;
values[ki] = value;
Expand Down Expand Up @@ -218,20 +219,15 @@ private void isNotEmptyOrThrow() {

private void keyExistsAndValueNotNullOrThrow(int ki, Object value) {
keyExistsOrThrow(ki);
valueNotNullOrThrow(value);
Objects.requireNonNull(value);
}

private void keyExistsOrThrow(int ki) {
if (!contains(ki)) throw new NoSuchElementException("Index does not exist; received: " + ki);
}

private void valueNotNullOrThrow(Object value) {
if (value == null) throw new IllegalArgumentException("value cannot be null");
}

private void keyInBoundsOrThrow(int ki) {
if (ki < 0 || ki >= N)
throw new IllegalArgumentException("Key index out of bounds; received: " + ki);
if (ki < 0 || ki >= N) throw new IndexOutOfBoundsException(ki);
}

/* Test functions */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ class Node {

// Construct a quad tree for a particular region.
public Node(Rect region) {
if (region == null) throw new IllegalArgumentException("Illegal argument");
this.region = region;
this.region = Objects.requireNonNull(region);
X = new long[NUM_POINTS];
Y = new long[NUM_POINTS];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.williamfiset.algorithms.datastructures.queue;

import java.util.NoSuchElementException;

/**
* Besides the Generics, the loss of property of size is another difference between ArrayQueue and
* IntQueue. The size of ArrayQueue is calculated by the formula, as are empty status and full
Expand Down Expand Up @@ -27,7 +29,7 @@ public ArrayQueue(int capacity) {
@Override
public void offer(T elem) {
if (isFull()) {
throw new RuntimeException("Queue is full");
throw new IllegalStateException("Queue is full");
}
data[rear++] = elem;
rear = adjustIndex(rear, data.length);
Expand All @@ -37,7 +39,7 @@ public void offer(T elem) {
@SuppressWarnings("unchecked")
public T poll() {
if (isEmpty()) {
throw new RuntimeException("Queue is empty");
throw new NoSuchElementException("Queue is empty");
}
front = adjustIndex(front, data.length);
return (T) data[front++];
Expand All @@ -47,7 +49,7 @@ public T poll() {
@SuppressWarnings("unchecked")
public T peek() {
if (isEmpty()) {
throw new RuntimeException("Queue is empty");
throw new NoSuchElementException("Queue is empty");
}
front = adjustIndex(front, data.length);
return (T) data[front];
Expand Down
Loading