diff --git a/bundles/sirix-core/logs/sirix.log b/bundles/sirix-core/logs/sirix.log new file mode 100644 index 000000000..e69de29bb diff --git a/bundles/sirix-core/src/main/java/org/sirix/utils/IntToObjectMap.java b/bundles/sirix-core/src/main/java/org/sirix/utils/IntToObjectMap.java index 9e7df12cc..6a67e133a 100644 --- a/bundles/sirix-core/src/main/java/org/sirix/utils/IntToObjectMap.java +++ b/bundles/sirix-core/src/main/java/org/sirix/utils/IntToObjectMap.java @@ -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 implements Iterable { +/** + * 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; @@ -254,7 +267,7 @@ public IntToObjectMap(int capacity) { * @param e * element which is being mapped using the given key */ - private void prvt_put(int key, T e) { + private void prvtPut(int key, T e) { // Hash entry to which the new pair would be inserted int hashIndex = calcBaseHashIndex(key); @@ -300,8 +313,8 @@ public void clear() { // And setting all the next[i] to point at // i+1. - for (int i = 1; i < this.capacity; ) { - next[i] = ++i; + for (int i = 1; i < this.capacity; ++i) { + next[i] = i + 1; } // 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 iterator = iterator(); iterator.hasNext(); ) { - T object = iterator.next(); + for (T object : this) { 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; } @@ -418,14 +430,14 @@ public T get(int key) { */ @SuppressWarnings("unchecked") protected void grow() { - IntToObjectMap that = new IntToObjectMap(this.capacity * 2); + IntToObjectMap that = new IntToObjectMap<>(this.capacity * 2); // Iterates fast over the collection. Any valid pair is put into the new // map without checking for duplicates or if there's enough space for // it. for (IndexIterator iterator = new IndexIterator(); iterator.hasNext(); ) { int index = iterator.next(); - that.prvt_put(this.keys[index], (T) this.values[index]); + that.prvtPut(this.keys[index], (T) this.values[index]); } // Copy that's data into this. @@ -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,13 +503,13 @@ 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(); } // Now that everything is set, the pair can be just put inside with no // worries. - prvt_put(key, e); + prvtPut(key, e); return null; } @@ -547,8 +559,8 @@ public Object[] toArray() { Object[] array = new Object[size]; // Iterates over the values, adding them to the array. - for (Iterator iterator = iterator(); iterator.hasNext(); ) { - array[++j] = iterator.next(); + for (T t : this) { + array[++j] = t; } return array; } @@ -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 that = (IntToObjectMap) 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))) { return false; } }