-
-
Notifications
You must be signed in to change notification settings - Fork 253
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
Simple refactor, typo fixes, added logger #639
Changes from 5 commits
6798bff
1587ff9
d9363d4
a3a548d
bf1f0fc
8205de6
5d8b16c
fe820ae
af62111
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package org.sirix.utils; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
|
@@ -37,6 +41,11 @@ | |
* @lucene.experimental | ||
*/ | ||
public class IntToObjectMap<T> implements Iterable<T> { | ||
/** | ||
* Logger for this class. | ||
*/ | ||
private static final Logger LOGGER = LoggerFactory.getLogger(IntToObjectMap.class); | ||
|
||
|
||
/** | ||
* Implements an IntIterator which iterates over all the allocated indexes. | ||
|
@@ -137,9 +146,13 @@ public boolean hasNext() { | |
|
||
@SuppressWarnings("unchecked") | ||
public T next() { | ||
if (!iterator.hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return (T) values[iterator.next()]; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
iterator.remove(); | ||
} | ||
|
@@ -151,7 +164,7 @@ public void remove() { | |
private static int defaultCapacity = 16; | ||
|
||
/** | ||
* Holds the base hash entries. if the capacity is 2^N, than the base hash | ||
* Holds the base hash entries. if the capacity is 2^N, thn the base hash | ||
* holds 2^(N+1). It can hold | ||
*/ | ||
int[] baseHash; | ||
|
@@ -301,7 +314,7 @@ public void clear() { | |
// And setting all the <code>next[i]</code> to point at | ||
// <code>i+1</code>. | ||
for (int i = 1; i < this.capacity; ) { | ||
next[i] = ++i; | ||
next[i] = i + 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong, as it doesn't increment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What am I not seeing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh the increment isn't even present in the method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gonna keep an eye out for silly mistakes |
||
} | ||
|
||
// Surly, the last one should point to the 'Ground'. | ||
|
@@ -329,8 +342,7 @@ public boolean containsKey(int key) { | |
* false otherwise. | ||
*/ | ||
public boolean containsValue(Object o) { | ||
for (Iterator<T> iterator = iterator(); iterator.hasNext(); ) { | ||
T object = iterator.next(); | ||
for (T object : this) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can explain why you used enhanced for-loop in this situation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No other reason than just making it more compact There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Though I can see it being an issue for some people if I'm being honest There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's great :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you |
||
if (object.equals(o)) { | ||
return true; | ||
} | ||
|
@@ -353,7 +365,7 @@ protected int find(int key) { | |
|
||
// while the index does not point to the 'Ground' | ||
while (localIndex != 0) { | ||
// returns the index found in case of of a matching key. | ||
// returns the index found in case of a matching key. | ||
if (keys[localIndex] == key) { | ||
return localIndex; | ||
} | ||
|
@@ -384,7 +396,7 @@ private int findForRemove(int key, int baseHashIndex) { | |
|
||
// while the index does not point to the 'Ground' | ||
while (index != 0) { | ||
// returns the index found in case of of a matching key. | ||
// returns the index found in case of a matching key. | ||
if (keys[index] == key) { | ||
return index; | ||
} | ||
|
@@ -465,7 +477,7 @@ public IntIterator keyIterator() { | |
@SuppressWarnings("unused") | ||
private void printBaseHash() { | ||
for (int i = 0; i < this.baseHash.length; i++) { | ||
System.out.println(i + ".\t" + baseHash[i]); | ||
LOGGER.info("{}.\t{}", i, baseHash[i]); | ||
} | ||
} | ||
|
||
|
@@ -491,7 +503,7 @@ public T put(int key, T e) { | |
|
||
// Is there enough room for a new pair? | ||
if (size == capacity) { | ||
// No? Than grow up! | ||
// No? Then grow up! | ||
grow(); | ||
} | ||
|
||
|
@@ -580,7 +592,7 @@ public T[] toArray(T[] a) { | |
|
||
@Override | ||
public String toString() { | ||
StringBuffer sb = new StringBuffer(); | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append('{'); | ||
IntIterator keyIterator = keyIterator(); | ||
while (keyIterator.hasNext()) { | ||
|
@@ -605,6 +617,9 @@ public int hashCode() { | |
@SuppressWarnings("unchecked") | ||
@Override | ||
public boolean equals(Object o) { | ||
if (!(o instanceof IntToObjectMap)) { | ||
return false; | ||
} | ||
IntToObjectMap<T> that = (IntToObjectMap<T>) o; | ||
if (that.size() != this.size()) { | ||
return false; | ||
|
@@ -619,7 +634,7 @@ public boolean equals(Object o) { | |
|
||
T v1 = this.get(key); | ||
T v2 = that.get(key); | ||
if ((v1 == null && v2 != null) || (v1 != null && v2 == null) || (!v1.equals(v2))) { | ||
if ((v1 == null && v2 != null) || (v1 != null && v2 == null) || (v1!= null && !v1.equals(v2))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we also may be able to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or simply There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll keep that in mind |
||
return false; | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sill minor typo: I guess it's "then" ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two times the charm :')