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

Optional is recommended to avoid explicit null checks #420

Open
wants to merge 1 commit 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 @@ -327,6 +327,7 @@ public T next() {

Node node = stack.pop();

// Optional is recommended to avoid explicit null checks
if (node.right != null) {
stack.push(node.right);
trav = node.right;
Expand Down Expand Up @@ -354,6 +355,7 @@ public boolean validateBSTInvarient(Node node) {
if (node == null) return true;
T val = node.value;
boolean isValid = true;
// Optional is recommended to avoid explicit null checks
if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0;
if (node.right != null) isValid = isValid && node.right.value.compareTo(val) > 0;
return isValid && validateBSTInvarient(node.left) && validateBSTInvarient(node.right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public T next() {

Node node = stack.pop();

// Optional is recommended to avoid explicit null checks
if (node.right != null) {
stack.push(node.right);
trav = node.right;
Expand All @@ -357,6 +358,7 @@ boolean validateBSTInvarient(Node node) {
if (node == null) return true;
T val = node.value;
boolean isValid = true;
// Optional is recommended to avoid explicit null checks
if (node.left != null) isValid = isValid && node.left.value.compareTo(val) < 0;
if (node.right != null) isValid = isValid && node.right.value.compareTo(val) > 0;
return isValid && validateBSTInvarient(node.left) && validateBSTInvarient(node.right);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ public boolean hasNext() {
public T next() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
Node node = stack.pop();
// Optional is recommended to avoid explicit null checks
if (node.right != null) stack.push(node.right);
if (node.left != null) stack.push(node.left);
return node.data;
Expand Down Expand Up @@ -279,6 +280,8 @@ public T next() {
Node node = stack.pop();

// Try moving down right once

// Optional is recommended to avoid explicit null checks
if (node.right != null) {
stack.push(node.right);
trav = node.right;
Expand All @@ -301,6 +304,7 @@ private java.util.Iterator<T> postOrderTraversal() {
final java.util.Stack<Node> stack2 = new java.util.Stack<>();
stack1.push(root);
while (!stack1.isEmpty()) {
// Optional is recommended to avoid explicit null checks
Node node = stack1.pop();
if (node != null) {
stack2.push(node);
Expand Down Expand Up @@ -346,6 +350,7 @@ public boolean hasNext() {
public T next() {
if (expectedNodeCount != nodeCount) throw new java.util.ConcurrentModificationException();
Node node = queue.poll();
// Optional is recommended to avoid explicit null checks
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
return node.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public AnagramSet(int[] mods) {
bloomFilter = new BloomFilter(mods);

// // Assuming all mods are primes each mod value will have a modular inverse
// It is recommended to use for-each loops instead of traditional for loops.
for (int i = 0; i < N_HASHES; i++) {
java.math.BigInteger mod = java.math.BigInteger.valueOf(MODS[i]);
for (int j = 0; j < PRIMES.length; j++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void add(long[] hashes) {

// Checks if a particular key is found within the bloom filter
public boolean contains(long[] hashes) {
// It is recommended to use for-each loops instead of traditional for loops.
for (int i = 0; i < hashes.length; i++) {
int block = (int) (hashes[i] >> DIV64_SHIFT);
long MASK = 1L << (hashes[i] & MOD64_MASK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void intHash() {
}

private void vectorHash() {
// It is recommended to use for-each loops instead of traditional for loops.
for (int i = 0; i < vectorData.length; i++) hash2 += randomVector[i] * vectorData[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static void preOrder(List<Integer> lst, TestTreeNode node) {
if (node == null) return;

lst.add(node.data);
// Optional is recommended to avoid explicit null checks
if (node.left != null) preOrder(lst, node.left);
if (node.right != null) preOrder(lst, node.right);
}
Expand All @@ -51,6 +52,7 @@ static void inOrder(List<Integer> lst, TestTreeNode node) {

if (node == null) return;

// Optional is recommended to avoid explicit null checks
if (node.left != null) inOrder(lst, node.left);
lst.add(node.data);
if (node.right != null) inOrder(lst, node.right);
Expand All @@ -60,6 +62,7 @@ static void postOrder(List<Integer> lst, TestTreeNode node) {

if (node == null) return;

// Optional is recommended to avoid explicit null checks
if (node.left != null) postOrder(lst, node.left);
if (node.right != null) postOrder(lst, node.right);
lst.add(node.data);
Expand All @@ -72,6 +75,7 @@ static void levelOrder(List<Integer> lst, TestTreeNode node) {

while (!q.isEmpty()) {

// Optional is recommended to avoid explicit null checks
node = q.poll();
lst.add(node.data);
if (node.left != null) q.offer(node.left);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ private void intHash() {
}

private void vectorHash() {
// It is recommended to use for-each loops instead of traditional for loops.
for (int i = 0; i < vectorData.length; i++) hash2 += randomVector[i] * vectorData[i];
}

Expand Down