You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by ro...@apache.org on 2010/02/13 21:54:31 UTC
svn commit: r909912 [3/10] - in
/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste: common/
eval/ hadoop/ hadoop/cooccurence/ hadoop/item/ hadoop/pseudo/
hadoop/slopeone/ impl/common/ impl/common/jdbc/ impl/eval/ impl/model/
impl/model/...
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastByIDMap.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastByIDMap.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastByIDMap.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastByIDMap.java Sat Feb 13 20:54:05 2010
@@ -17,8 +17,6 @@
package org.apache.mahout.cf.taste.impl.common;
-import org.apache.mahout.common.RandomUtils;
-
import java.io.Serializable;
import java.util.AbstractSet;
import java.util.Arrays;
@@ -28,19 +26,21 @@
import java.util.NoSuchElementException;
import java.util.Set;
+import org.apache.mahout.common.RandomUtils;
+
/**
* @see FastMap
* @see FastIDSet
*/
public final class FastByIDMap<V> implements Serializable, Cloneable {
-
+
public static final int NO_MAX_SIZE = Integer.MAX_VALUE;
private static final double ALLOWED_LOAD_FACTOR = 1.5;
-
+
/** Dummy object used to represent a key that has been removed. */
private static final long REMOVED = Long.MAX_VALUE;
private static final long NULL = Long.MIN_VALUE;
-
+
private long[] keys;
private V[] values;
private int numEntries;
@@ -48,44 +48,48 @@
private int maxSize;
private BitSet recentlyAccessed;
private final boolean countingAccesses;
-
+
/** Creates a new {@link FastByIDMap} with default capacity. */
public FastByIDMap() {
- this(2, NO_MAX_SIZE);
+ this(2, FastByIDMap.NO_MAX_SIZE);
}
-
+
public FastByIDMap(int size) {
- this(size, NO_MAX_SIZE);
+ this(size, FastByIDMap.NO_MAX_SIZE);
}
-
+
/**
- * Creates a new {@link FastByIDMap} whose capacity can accommodate the given number of entries without rehash.</p>
- *
- * @param size desired capacity
- * @param maxSize max capacity
- * @throws IllegalArgumentException if size is less than 0, maxSize is less than 1,
- * or at least half of {@link RandomUtils#MAX_INT_SMALLER_TWIN_PRIME}
+ * Creates a new {@link FastByIDMap} whose capacity can accommodate the given number of entries without
+ * rehash.</p>
+ *
+ * @param size
+ * desired capacity
+ * @param maxSize
+ * max capacity
+ * @throws IllegalArgumentException
+ * if size is less than 0, maxSize is less than 1, or at least half of
+ * {@link RandomUtils#MAX_INT_SMALLER_TWIN_PRIME}
*/
public FastByIDMap(int size, int maxSize) {
if (size < 0) {
throw new IllegalArgumentException("size must be at least 0");
}
- int max = (int) (RandomUtils.MAX_INT_SMALLER_TWIN_PRIME / ALLOWED_LOAD_FACTOR);
+ int max = (int) (RandomUtils.MAX_INT_SMALLER_TWIN_PRIME / FastByIDMap.ALLOWED_LOAD_FACTOR);
if (size >= max) {
throw new IllegalArgumentException("size must be less than " + max);
}
if (maxSize < 1) {
throw new IllegalArgumentException("maxSize must be at least 1");
}
- int hashSize = RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * size));
+ int hashSize = RandomUtils.nextTwinPrime((int) (FastByIDMap.ALLOWED_LOAD_FACTOR * size));
keys = new long[hashSize];
- Arrays.fill(keys, NULL);
+ Arrays.fill(keys, FastByIDMap.NULL);
values = (V[]) new Object[hashSize];
this.maxSize = maxSize;
this.countingAccesses = maxSize != Integer.MAX_VALUE;
this.recentlyAccessed = countingAccesses ? new BitSet(hashSize) : null;
}
-
+
/**
* @see #findForAdd(long)
*/
@@ -96,7 +100,7 @@
int jump = 1 + theHashCode % (hashSize - 2);
int index = theHashCode % hashSize;
long currentKey = keys[index];
- while (currentKey != NULL && key != currentKey) {
+ while ((currentKey != FastByIDMap.NULL) && (key != currentKey)) {
if (index < jump) {
index += hashSize - jump;
} else {
@@ -106,7 +110,7 @@
}
return index;
}
-
+
/**
* @see #find(long)
*/
@@ -117,7 +121,8 @@
int jump = 1 + theHashCode % (hashSize - 2);
int index = theHashCode % hashSize;
long currentKey = keys[index];
- while (currentKey != NULL && currentKey != REMOVED && key != currentKey) { // Different here
+ while ((currentKey != FastByIDMap.NULL) && (currentKey != FastByIDMap.REMOVED) && (key != currentKey)) { // Different
+ // here
if (index < jump) {
index += hashSize - jump;
} else {
@@ -127,9 +132,9 @@
}
return index;
}
-
+
public V get(long key) {
- if (key == NULL) {
+ if (key == FastByIDMap.NULL) {
return null;
}
int index = find(key);
@@ -138,42 +143,42 @@
}
return values[index];
}
-
+
public int size() {
return numEntries;
}
-
+
public boolean isEmpty() {
return numEntries == 0;
}
-
+
public boolean containsKey(long key) {
- return key != NULL && key != REMOVED && keys[find(key)] != NULL;
+ return (key != FastByIDMap.NULL) && (key != FastByIDMap.REMOVED) && (keys[find(key)] != FastByIDMap.NULL);
}
-
+
public boolean containsValue(Object value) {
if (value == null) {
return false;
}
for (V theValue : values) {
- if (theValue != null && value.equals(theValue)) {
+ if ((theValue != null) && value.equals(theValue)) {
return true;
}
}
return false;
}
-
+
public V put(long key, V value) {
- if (key == NULL || key == REMOVED) {
+ if ((key == FastByIDMap.NULL) || (key == FastByIDMap.REMOVED)) {
throw new IllegalArgumentException();
}
if (value == null) {
throw new NullPointerException();
}
// If less than half the slots are open, let's clear it up
- if (numSlotsUsed * ALLOWED_LOAD_FACTOR >= keys.length) {
+ if (numSlotsUsed * FastByIDMap.ALLOWED_LOAD_FACTOR >= keys.length) {
// If over half the slots used are actual entries, let's grow
- if (numEntries * ALLOWED_LOAD_FACTOR >= numSlotsUsed) {
+ if (numEntries * FastByIDMap.ALLOWED_LOAD_FACTOR >= numSlotsUsed) {
growAndRehash();
} else {
// Otherwise just rehash to clear REMOVED entries and don't grow
@@ -189,20 +194,20 @@
return oldValue;
} else {
// If size is limited,
- if (countingAccesses && numEntries >= maxSize) {
+ if (countingAccesses && (numEntries >= maxSize)) {
// and we're too large, clear some old-ish entry
clearStaleEntry(index);
}
keys[index] = key;
values[index] = value;
numEntries++;
- if (keyIndex == NULL) {
+ if (keyIndex == FastByIDMap.NULL) {
numSlotsUsed++;
}
return null;
}
}
-
+
private void clearStaleEntry(int index) {
while (true) {
long currentKey;
@@ -213,7 +218,7 @@
index--;
}
currentKey = keys[index];
- } while (currentKey == NULL || currentKey == REMOVED);
+ } while ((currentKey == FastByIDMap.NULL) || (currentKey == FastByIDMap.REMOVED));
if (recentlyAccessed.get(index)) {
recentlyAccessed.clear(index);
} else {
@@ -221,20 +226,20 @@
}
}
// Delete the entry
- keys[index] = REMOVED;
+ keys[index] = FastByIDMap.REMOVED;
numEntries--;
values[index] = null;
}
-
+
public V remove(long key) {
- if (key == NULL || key == REMOVED) {
+ if ((key == FastByIDMap.NULL) || (key == FastByIDMap.REMOVED)) {
return null;
}
int index = find(key);
- if (keys[index] == NULL) {
+ if (keys[index] == FastByIDMap.NULL) {
return null;
} else {
- keys[index] = REMOVED;
+ keys[index] = FastByIDMap.REMOVED;
numEntries--;
V oldValue = values[index];
values[index] = null;
@@ -243,36 +248,36 @@
}
// Could un-set recentlyAccessed's bit but doesn't matter
}
-
+
public void clear() {
numEntries = 0;
numSlotsUsed = 0;
- Arrays.fill(keys, NULL);
+ Arrays.fill(keys, FastByIDMap.NULL);
Arrays.fill(values, null);
if (countingAccesses) {
recentlyAccessed.clear();
}
}
-
+
public LongPrimitiveIterator keySetIterator() {
return new KeyIterator();
}
-
- public Set<Map.Entry<Long, V>> entrySet() {
+
+ public Set<Map.Entry<Long,V>> entrySet() {
return new EntrySet();
}
-
+
public void rehash() {
- rehash(RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * numEntries)));
+ rehash(RandomUtils.nextTwinPrime((int) (FastByIDMap.ALLOWED_LOAD_FACTOR * numEntries)));
}
-
+
private void growAndRehash() {
- if (keys.length * ALLOWED_LOAD_FACTOR >= RandomUtils.MAX_INT_SMALLER_TWIN_PRIME) {
+ if (keys.length * FastByIDMap.ALLOWED_LOAD_FACTOR >= RandomUtils.MAX_INT_SMALLER_TWIN_PRIME) {
throw new IllegalStateException("Can't grow any more");
}
- rehash(RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * keys.length)));
+ rehash(RandomUtils.nextTwinPrime((int) (FastByIDMap.ALLOWED_LOAD_FACTOR * keys.length)));
}
-
+
private void rehash(int newHashSize) {
long[] oldKeys = keys;
V[] oldValues = values;
@@ -282,17 +287,17 @@
recentlyAccessed = new BitSet(newHashSize);
}
keys = new long[newHashSize];
- Arrays.fill(keys, NULL);
+ Arrays.fill(keys, FastByIDMap.NULL);
values = (V[]) new Object[newHashSize];
int length = oldKeys.length;
for (int i = 0; i < length; i++) {
long key = oldKeys[i];
- if (key != NULL && key != REMOVED) {
+ if ((key != FastByIDMap.NULL) && (key != FastByIDMap.REMOVED)) {
put(key, oldValues[i]);
}
}
}
-
+
void iteratorRemove(int lastNext) {
if (lastNext >= values.length) {
throw new NoSuchElementException();
@@ -301,10 +306,10 @@
throw new IllegalStateException();
}
values[lastNext] = null;
- keys[lastNext] = REMOVED;
+ keys[lastNext] = FastByIDMap.REMOVED;
numEntries--;
}
-
+
@Override
public FastByIDMap<V> clone() {
FastByIDMap<V> clone;
@@ -318,7 +323,7 @@
clone.recentlyAccessed = countingAccesses ? new BitSet(keys.length) : null;
return clone;
}
-
+
@Override
public String toString() {
if (isEmpty()) {
@@ -328,25 +333,25 @@
result.append('{');
for (int i = 0; i < keys.length; i++) {
long key = keys[i];
- if (key != NULL && key != REMOVED) {
+ if ((key != FastByIDMap.NULL) && (key != FastByIDMap.REMOVED)) {
result.append(key).append('=').append(values[i]).append(',');
}
}
result.setCharAt(result.length() - 1, '}');
return result.toString();
}
-
+
private final class KeyIterator extends AbstractLongPrimitiveIterator {
-
+
private int position;
private int lastNext = -1;
-
+
@Override
public boolean hasNext() {
goToNext();
return position < keys.length;
}
-
+
@Override
public long nextLong() {
goToNext();
@@ -356,7 +361,7 @@
}
return keys[position++];
}
-
+
@Override
public long peek() {
goToNext();
@@ -365,96 +370,96 @@
}
return keys[position];
}
-
+
private void goToNext() {
int length = values.length;
- while (position < length && values[position] == null) {
+ while ((position < length) && (values[position] == null)) {
position++;
}
}
-
+
@Override
public void remove() {
iteratorRemove(lastNext);
}
-
+
@Override
public void skip(int n) {
position += n;
}
-
+
}
-
- private final class EntrySet extends AbstractSet<Map.Entry<Long, V>> {
-
+
+ private final class EntrySet extends AbstractSet<Map.Entry<Long,V>> {
+
@Override
public int size() {
return FastByIDMap.this.size();
}
-
+
@Override
public boolean isEmpty() {
return FastByIDMap.this.isEmpty();
}
-
+
@Override
public boolean contains(Object o) {
return containsKey((Long) o);
}
-
+
@Override
- public Iterator<Map.Entry<Long, V>> iterator() {
+ public Iterator<Map.Entry<Long,V>> iterator() {
return new EntryIterator();
}
-
+
@Override
- public boolean add(Map.Entry<Long, V> t) {
+ public boolean add(Map.Entry<Long,V> t) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
-
+
@Override
- public boolean addAll(Collection<? extends Map.Entry<Long, V>> ts) {
+ public boolean addAll(Collection<? extends Map.Entry<Long,V>> ts) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean retainAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean removeAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void clear() {
FastByIDMap.this.clear();
}
-
- private final class MapEntry implements Map.Entry<Long, V> {
-
+
+ private final class MapEntry implements Map.Entry<Long,V> {
+
private final int index;
-
+
private MapEntry(int index) {
this.index = index;
}
-
+
@Override
public Long getKey() {
return keys[index];
}
-
+
@Override
public V getValue() {
return values[index];
}
-
+
@Override
public V setValue(V value) {
if (value == null) {
@@ -465,20 +470,20 @@
return oldValue;
}
}
-
- private final class EntryIterator implements Iterator<Map.Entry<Long, V>> {
-
+
+ private final class EntryIterator implements Iterator<Map.Entry<Long,V>> {
+
private int position;
private int lastNext = -1;
-
+
@Override
public boolean hasNext() {
goToNext();
return position < keys.length;
}
-
+
@Override
- public Map.Entry<Long, V> next() {
+ public Map.Entry<Long,V> next() {
goToNext();
lastNext = position;
if (position >= keys.length) {
@@ -486,20 +491,20 @@
}
return new MapEntry(position++);
}
-
+
private void goToNext() {
int length = values.length;
- while (position < length && values[position] == null) {
+ while ((position < length) && (values[position] == null)) {
position++;
}
}
-
+
@Override
public void remove() {
iteratorRemove(lastNext);
}
}
-
+
}
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastIDSet.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastIDSet.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastIDSet.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastIDSet.java Sat Feb 13 20:54:05 2010
@@ -17,46 +17,46 @@
package org.apache.mahout.cf.taste.impl.common;
-import org.apache.mahout.common.RandomUtils;
-
import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
+import org.apache.mahout.common.RandomUtils;
+
/**
* @see FastByIDMap
*/
public final class FastIDSet implements Serializable, Cloneable {
-
+
private static final double ALLOWED_LOAD_FACTOR = 1.5;
-
+
/** Dummy object used to represent a key that has been removed. */
private static final long REMOVED = Long.MAX_VALUE;
private static final long NULL = Long.MIN_VALUE;
-
+
private long[] keys;
private int numEntries;
private int numSlotsUsed;
-
+
/** Creates a new {@link FastIDSet} with default capacity. */
public FastIDSet() {
this(2);
}
-
+
public FastIDSet(int size) {
if (size < 0) {
throw new IllegalArgumentException("size must be at least 0");
}
- int max = (int) (RandomUtils.MAX_INT_SMALLER_TWIN_PRIME / ALLOWED_LOAD_FACTOR);
+ int max = (int) (RandomUtils.MAX_INT_SMALLER_TWIN_PRIME / FastIDSet.ALLOWED_LOAD_FACTOR);
if (size >= max) {
throw new IllegalArgumentException("size must be less than " + max);
}
- int hashSize = RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * size));
+ int hashSize = RandomUtils.nextTwinPrime((int) (FastIDSet.ALLOWED_LOAD_FACTOR * size));
keys = new long[hashSize];
- Arrays.fill(keys, NULL);
+ Arrays.fill(keys, FastIDSet.NULL);
}
-
+
/**
* @see #findForAdd(long)
*/
@@ -67,7 +67,7 @@
int jump = 1 + theHashCode % (hashSize - 2);
int index = theHashCode % hashSize;
long currentKey = keys[index];
- while (currentKey != NULL && key != currentKey) { // note: true when currentKey == REMOVED
+ while ((currentKey != FastIDSet.NULL) && (key != currentKey)) { // note: true when currentKey == REMOVED
if (index < jump) {
index += hashSize - jump;
} else {
@@ -77,7 +77,7 @@
}
return index;
}
-
+
/**
* @see #find(long)
*/
@@ -88,7 +88,8 @@
int jump = 1 + theHashCode % (hashSize - 2);
int index = theHashCode % hashSize;
long currentKey = keys[index];
- while (currentKey != NULL && currentKey != REMOVED && key != currentKey) { // Different here
+ while ((currentKey != FastIDSet.NULL) && (currentKey != FastIDSet.REMOVED) && (key != currentKey)) { // Different
+ // here
if (index < jump) {
index += hashSize - jump;
} else {
@@ -98,27 +99,27 @@
}
return index;
}
-
+
public int size() {
return numEntries;
}
-
+
public boolean isEmpty() {
return numEntries == 0;
}
-
+
public boolean contains(long key) {
- return key != NULL && key != REMOVED && keys[find(key)] != NULL;
+ return (key != FastIDSet.NULL) && (key != FastIDSet.REMOVED) && (keys[find(key)] != FastIDSet.NULL);
}
-
+
public boolean add(long key) {
- if (key == NULL || key == REMOVED) {
+ if ((key == FastIDSet.NULL) || (key == FastIDSet.REMOVED)) {
throw new IllegalArgumentException();
}
// If less than half the slots are open, let's clear it up
- if (numSlotsUsed * ALLOWED_LOAD_FACTOR >= keys.length) {
+ if (numSlotsUsed * FastIDSet.ALLOWED_LOAD_FACTOR >= keys.length) {
// If over half the slots used are actual entries, let's grow
- if (numEntries * ALLOWED_LOAD_FACTOR >= numSlotsUsed) {
+ if (numEntries * FastIDSet.ALLOWED_LOAD_FACTOR >= numSlotsUsed) {
growAndRehash();
} else {
// Otherwise just rehash to clear REMOVED entries and don't grow
@@ -131,43 +132,43 @@
if (keyIndex != key) {
keys[index] = key;
numEntries++;
- if (keyIndex == NULL) {
+ if (keyIndex == FastIDSet.NULL) {
numSlotsUsed++;
}
return true;
}
return false;
}
-
+
public LongPrimitiveIterator iterator() {
return new KeyIterator();
}
-
+
public long[] toArray() {
long[] result = new long[numEntries];
for (int i = 0, position = 0; i < result.length; i++) {
- while (keys[position] == NULL || keys[position] == REMOVED) {
+ while ((keys[position] == FastIDSet.NULL) || (keys[position] == FastIDSet.REMOVED)) {
position++;
}
result[i] = keys[position++];
}
return result;
}
-
+
public boolean remove(long key) {
- if (key == NULL || key == REMOVED) {
+ if ((key == FastIDSet.NULL) || (key == FastIDSet.REMOVED)) {
return false;
}
int index = find(key);
- if (keys[index] == NULL) {
+ if (keys[index] == FastIDSet.NULL) {
return false;
} else {
- keys[index] = REMOVED;
+ keys[index] = FastIDSet.REMOVED;
numEntries--;
return true;
}
}
-
+
public boolean addAll(long[] c) {
boolean changed = false;
for (long k : c) {
@@ -177,17 +178,17 @@
}
return changed;
}
-
+
public boolean addAll(FastIDSet c) {
boolean changed = false;
for (long k : c.keys) {
- if (k != NULL && k != REMOVED && add(k)) {
+ if ((k != FastIDSet.NULL) && (k != FastIDSet.REMOVED) && add(k)) {
changed = true;
}
}
return changed;
}
-
+
public boolean removeAll(long[] c) {
boolean changed = false;
for (long o : c) {
@@ -197,78 +198,79 @@
}
return changed;
}
-
+
public boolean removeAll(FastIDSet c) {
boolean changed = false;
for (long k : c.keys) {
- if (k != NULL && k != REMOVED && remove(k)) {
+ if ((k != FastIDSet.NULL) && (k != FastIDSet.REMOVED) && remove(k)) {
changed = true;
}
}
return changed;
}
-
+
public boolean retainAll(FastIDSet c) {
boolean changed = false;
for (int i = 0; i < keys.length; i++) {
long k = keys[i];
- if (k != NULL && k != REMOVED && !c.contains(k)) {
- keys[i] = REMOVED;
+ if ((k != FastIDSet.NULL) && (k != FastIDSet.REMOVED) && !c.contains(k)) {
+ keys[i] = FastIDSet.REMOVED;
numEntries--;
changed = true;
}
}
return changed;
}
-
+
public void clear() {
numEntries = 0;
numSlotsUsed = 0;
- Arrays.fill(keys, NULL);
+ Arrays.fill(keys, FastIDSet.NULL);
}
-
+
private void growAndRehash() {
- if (keys.length * ALLOWED_LOAD_FACTOR >= RandomUtils.MAX_INT_SMALLER_TWIN_PRIME) {
+ if (keys.length * FastIDSet.ALLOWED_LOAD_FACTOR >= RandomUtils.MAX_INT_SMALLER_TWIN_PRIME) {
throw new IllegalStateException("Can't grow any more");
}
- rehash(RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * keys.length)));
+ rehash(RandomUtils.nextTwinPrime((int) (FastIDSet.ALLOWED_LOAD_FACTOR * keys.length)));
}
-
+
public void rehash() {
- rehash(RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * numEntries)));
+ rehash(RandomUtils.nextTwinPrime((int) (FastIDSet.ALLOWED_LOAD_FACTOR * numEntries)));
}
-
+
private void rehash(int newHashSize) {
long[] oldKeys = keys;
numEntries = 0;
numSlotsUsed = 0;
keys = new long[newHashSize];
- Arrays.fill(keys, NULL);
+ Arrays.fill(keys, FastIDSet.NULL);
int length = oldKeys.length;
for (int i = 0; i < length; i++) {
long key = oldKeys[i];
- if (key != NULL && key != REMOVED) {
+ if ((key != FastIDSet.NULL) && (key != FastIDSet.REMOVED)) {
add(key);
}
}
}
-
+
/**
* Convenience method to quickly compute just the size of the intersection with another {@link FastIDSet}.
- *
- * @param other {@link FastIDSet} to intersect with
+ *
+ * @param other
+ * {@link FastIDSet} to intersect with
* @return number of elements in intersection
*/
public int intersectionSize(FastIDSet other) {
int count = 0;
for (long key : other.keys) {
- if (key != NULL && key != REMOVED && keys[find(key)] != NULL) {
+ if ((key != FastIDSet.NULL) && (key != FastIDSet.REMOVED) && (keys[find(key)] != FastIDSet.NULL)) {
count++;
}
}
return count;
}
-
+
@Override
public FastIDSet clone() {
FastIDSet clone;
@@ -280,7 +282,7 @@
clone.keys = keys.clone();
return clone;
}
-
+
@Override
public String toString() {
if (isEmpty()) {
@@ -289,25 +291,25 @@
StringBuilder result = new StringBuilder();
result.append('[');
for (long key : keys) {
- if (key != NULL && key != REMOVED) {
+ if ((key != FastIDSet.NULL) && (key != FastIDSet.REMOVED)) {
result.append(key).append(',');
}
}
result.setCharAt(result.length() - 1, ']');
return result.toString();
}
-
+
private final class KeyIterator extends AbstractLongPrimitiveIterator {
-
+
private int position;
private int lastNext = -1;
-
+
@Override
public boolean hasNext() {
goToNext();
return position < keys.length;
}
-
+
@Override
public long nextLong() {
goToNext();
@@ -317,7 +319,7 @@
}
return keys[position++];
}
-
+
@Override
public long peek() {
goToNext();
@@ -326,14 +328,15 @@
}
return keys[position];
}
-
+
private void goToNext() {
int length = keys.length;
- while (position < length && (keys[position] == NULL || keys[position] == REMOVED)) {
+ while ((position < length)
+ && ((keys[position] == FastIDSet.NULL) || (keys[position] == FastIDSet.REMOVED))) {
position++;
}
}
-
+
@Override
public void remove() {
if (lastNext >= keys.length) {
@@ -342,19 +345,19 @@
if (lastNext < 0) {
throw new IllegalStateException();
}
- keys[lastNext] = REMOVED;
+ keys[lastNext] = FastIDSet.REMOVED;
numEntries--;
}
-
+
public Iterator<Long> iterator() {
return new KeyIterator();
}
-
+
@Override
public void skip(int n) {
position += n;
}
-
+
}
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastMap.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastMap.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastMap.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FastMap.java Sat Feb 13 20:54:05 2010
@@ -17,8 +17,6 @@
package org.apache.mahout.cf.taste.impl.common;
-import org.apache.mahout.common.RandomUtils;
-
import java.io.Serializable;
import java.util.AbstractCollection;
import java.util.AbstractSet;
@@ -29,26 +27,36 @@
import java.util.NoSuchElementException;
import java.util.Set;
+import org.apache.mahout.common.RandomUtils;
+
/**
- * <p>This is an optimized {@link Map} implementation, based on algorithms described in Knuth's "Art of Computer
- * Programming", Vol. 3, p. 529.</p>
- *
- * <p>It should be faster than {@link java.util.HashMap} in some cases, but not all. Its main feature is a "max size"
- * and the ability to transparently, efficiently and semi-intelligently evict old entries when max size is
- * exceeded.</p>
- *
- * <p>This class is not a bit thread-safe.</p>
- *
- * <p>This implementation does not allow <code>null</code> as a key or value.</p>
+ * <p>
+ * This is an optimized {@link Map} implementation, based on algorithms described in Knuth's "Art of Computer
+ * Programming", Vol. 3, p. 529.
+ * </p>
+ *
+ * <p>
+ * It should be faster than {@link java.util.HashMap} in some cases, but not all. Its main feature is a
+ * "max size" and the ability to transparently, efficiently and semi-intelligently evict old entries when max
+ * size is exceeded.
+ * </p>
+ *
+ * <p>
+ * This class is not a bit thread-safe.
+ * </p>
+ *
+ * <p>
+ * This implementation does not allow <code>null</code> as a key or value.
+ * </p>
*/
-public final class FastMap<K, V> implements Map<K, V>, Serializable, Cloneable {
-
+public final class FastMap<K,V> implements Map<K,V>, Serializable, Cloneable {
+
public static final int NO_MAX_SIZE = Integer.MAX_VALUE;
private static final double ALLOWED_LOAD_FACTOR = 1.5;
-
+
/** Dummy object used to represent a key that has been removed. */
private static final Object REMOVED = new Object();
-
+
private K[] keys;
private V[] values;
private int numEntries;
@@ -56,28 +64,32 @@
private int maxSize;
private BitSet recentlyAccessed;
private final boolean countingAccesses;
-
+
/** Creates a new {@link FastMap} with default capacity. */
public FastMap() {
this(2, NO_MAX_SIZE);
}
-
+
public FastMap(int size) {
this(size, NO_MAX_SIZE);
}
-
+
public FastMap(Map<K,V> other) {
this(other.size());
putAll(other);
}
-
+
/**
- * Creates a new {@link FastMap} whose capacity can accommodate the given number of entries without rehash.</p>
- *
- * @param size desired capacity
- * @param maxSize max capacity
- * @throws IllegalArgumentException if size is less than 0, maxSize is less than 1,
- * or at least half of {@link RandomUtils#MAX_INT_SMALLER_TWIN_PRIME}
+ * Creates a new {@link FastMap} whose capacity can accommodate the given number of entries without
+ * rehash.</p>
+ *
+ * @param size
+ * desired capacity
+ * @param maxSize
+ * max capacity
+ * @throws IllegalArgumentException
+ * if size is less than 0, maxSize is less than 1, or at least half of
+ * {@link RandomUtils#MAX_INT_SMALLER_TWIN_PRIME}
*/
public FastMap(int size, int maxSize) {
if (size < 0) {
@@ -97,7 +109,7 @@
this.countingAccesses = maxSize != Integer.MAX_VALUE;
this.recentlyAccessed = countingAccesses ? new BitSet(hashSize) : null;
}
-
+
private int find(Object key) {
int theHashCode = key.hashCode() & 0x7FFFFFFF; // make sure it's positive
K[] keys = this.keys;
@@ -105,7 +117,7 @@
int jump = 1 + theHashCode % (hashSize - 2);
int index = theHashCode % hashSize;
K currentKey = keys[index];
- while (currentKey != null && (currentKey == REMOVED || !key.equals(currentKey))) {
+ while ((currentKey != null) && ((currentKey == REMOVED) || !key.equals(currentKey))) {
if (index < jump) {
index += hashSize - jump;
} else {
@@ -115,7 +127,7 @@
}
return index;
}
-
+
@Override
public V get(Object key) {
if (key == null) {
@@ -127,39 +139,42 @@
}
return values[index];
}
-
+
@Override
public int size() {
return numEntries;
}
-
+
@Override
public boolean isEmpty() {
return numEntries == 0;
}
-
+
@Override
public boolean containsKey(Object key) {
- return key != null && keys[find(key)] != null;
+ return (key != null) && (keys[find(key)] != null);
}
-
+
@Override
public boolean containsValue(Object value) {
if (value == null) {
return false;
}
for (V theValue : values) {
- if (theValue != null && value.equals(theValue)) {
+ if ((theValue != null) && value.equals(theValue)) {
return true;
}
}
return false;
}
-
- /** @throws NullPointerException if key or value is null */
+
+ /**
+ * @throws NullPointerException
+ * if key or value is null
+ */
@Override
public V put(K key, V value) {
- if (key == null || value == null) {
+ if ((key == null) || (value == null)) {
throw new NullPointerException();
}
// If less than half the slots are open, let's clear it up
@@ -176,7 +191,7 @@
int index = find(key);
if (keys[index] == null) {
// If size is limited,
- if (countingAccesses && numEntries >= maxSize) {
+ if (countingAccesses && (numEntries >= maxSize)) {
// and we're too large, clear some old-ish entry
clearStaleEntry(index);
}
@@ -191,7 +206,7 @@
return oldValue;
}
}
-
+
private void clearStaleEntry(int index) {
while (true) {
K currentKey;
@@ -202,7 +217,7 @@
index--;
}
currentKey = keys[index];
- } while (currentKey == null || currentKey == REMOVED);
+ } while ((currentKey == null) || (currentKey == REMOVED));
if (recentlyAccessed.get(index)) {
recentlyAccessed.clear(index);
} else {
@@ -210,18 +225,18 @@
}
}
// Delete the entry
- ((Object[]) keys)[index] = REMOVED;
+ ((Object[])keys)[index] = REMOVED;
numEntries--;
values[index] = null;
}
-
+
@Override
- public void putAll(Map<? extends K, ? extends V> map) {
- for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
+ public void putAll(Map<? extends K,? extends V> map) {
+ for (Entry<? extends K,? extends V> entry : map.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
-
+
@Override
public V remove(Object key) {
if (key == null) {
@@ -231,7 +246,7 @@
if (keys[index] == null) {
return null;
} else {
- ((Object[]) keys)[index] = REMOVED;
+ ((Object[])keys)[index] = REMOVED;
numEntries--;
V oldValue = values[index];
values[index] = null;
@@ -240,7 +255,7 @@
}
// Could un-set recentlyAccessed's bit but doesn't matter
}
-
+
@Override
public void clear() {
numEntries = 0;
@@ -251,33 +266,33 @@
recentlyAccessed.clear();
}
}
-
+
@Override
public Set<K> keySet() {
return new KeySet();
}
-
+
@Override
public Collection<V> values() {
return new ValueCollection();
}
-
+
@Override
- public Set<Entry<K, V>> entrySet() {
+ public Set<Entry<K,V>> entrySet() {
return new EntrySet();
}
-
+
public void rehash() {
rehash(RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * numEntries)));
}
-
+
private void growAndRehash() {
if (keys.length * ALLOWED_LOAD_FACTOR >= RandomUtils.MAX_INT_SMALLER_TWIN_PRIME) {
throw new IllegalStateException("Can't grow any more");
}
rehash(RandomUtils.nextTwinPrime((int) (ALLOWED_LOAD_FACTOR * keys.length)));
}
-
+
private void rehash(int newHashSize) {
K[] oldKeys = keys;
V[] oldValues = values;
@@ -291,12 +306,12 @@
int length = oldKeys.length;
for (int i = 0; i < length; i++) {
K key = oldKeys[i];
- if (key != null && key != REMOVED) {
+ if ((key != null) && (key != REMOVED)) {
put(key, oldValues[i]);
}
}
}
-
+
void iteratorRemove(int lastNext) {
if (lastNext >= values.length) {
throw new NoSuchElementException();
@@ -305,15 +320,15 @@
throw new IllegalStateException();
}
values[lastNext] = null;
- ((Object[]) keys)[lastNext] = REMOVED;
+ ((Object[])keys)[lastNext] = REMOVED;
numEntries--;
}
-
+
@Override
- public FastMap<K, V> clone() {
- FastMap<K, V> clone;
+ public FastMap<K,V> clone() {
+ FastMap<K,V> clone;
try {
- clone = (FastMap<K, V>) super.clone();
+ clone = (FastMap<K,V>) super.clone();
} catch (CloneNotSupportedException cnse) {
throw new AssertionError();
}
@@ -322,7 +337,7 @@
clone.recentlyAccessed = countingAccesses ? new BitSet(keys.length) : null;
return clone;
}
-
+
@Override
public String toString() {
if (isEmpty()) {
@@ -332,84 +347,84 @@
result.append('{');
for (int i = 0; i < keys.length; i++) {
K key = keys[i];
- if (key != null && key != REMOVED) {
+ if ((key != null) && (key != REMOVED)) {
result.append(key).append('=').append(values[i]).append(',');
}
}
result.setCharAt(result.length() - 1, '}');
return result.toString();
}
-
- private final class EntrySet extends AbstractSet<Entry<K, V>> {
-
+
+ private final class EntrySet extends AbstractSet<Entry<K,V>> {
+
@Override
public int size() {
return FastMap.this.size();
}
-
+
@Override
public boolean isEmpty() {
return FastMap.this.isEmpty();
}
-
+
@Override
public boolean contains(Object o) {
return containsKey(o);
}
-
+
@Override
- public Iterator<Entry<K, V>> iterator() {
+ public Iterator<Entry<K,V>> iterator() {
return new EntryIterator();
}
-
+
@Override
- public boolean add(Entry<K, V> t) {
+ public boolean add(Entry<K,V> t) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
-
+
@Override
- public boolean addAll(Collection<? extends Entry<K, V>> ts) {
+ public boolean addAll(Collection<? extends Entry<K,V>> ts) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean retainAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean removeAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void clear() {
FastMap.this.clear();
}
-
- private final class MapEntry implements Entry<K, V> {
-
+
+ private final class MapEntry implements Entry<K,V> {
+
private final int index;
-
+
private MapEntry(int index) {
this.index = index;
}
-
+
@Override
public K getKey() {
return keys[index];
}
-
+
@Override
public V getValue() {
return values[index];
}
-
+
@Override
public V setValue(V value) {
if (value == null) {
@@ -420,20 +435,20 @@
return oldValue;
}
}
-
- private final class EntryIterator implements Iterator<Entry<K, V>> {
-
+
+ private final class EntryIterator implements Iterator<Entry<K,V>> {
+
private int position;
private int lastNext = -1;
-
+
@Override
public boolean hasNext() {
goToNext();
return position < keys.length;
}
-
+
@Override
- public Entry<K, V> next() {
+ public Entry<K,V> next() {
goToNext();
lastNext = position;
if (position >= keys.length) {
@@ -441,85 +456,85 @@
}
return new MapEntry(position++);
}
-
+
private void goToNext() {
int length = values.length;
- while (position < length && values[position] == null) {
+ while ((position < length) && (values[position] == null)) {
position++;
}
}
-
+
@Override
public void remove() {
iteratorRemove(lastNext);
}
}
-
+
}
-
+
private final class KeySet extends AbstractSet<K> {
-
+
@Override
public int size() {
return FastMap.this.size();
}
-
+
@Override
public boolean isEmpty() {
return FastMap.this.isEmpty();
}
-
+
@Override
public boolean contains(Object o) {
return containsKey(o);
}
-
+
@Override
public Iterator<K> iterator() {
return new KeyIterator();
}
-
+
@Override
public boolean add(K t) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean addAll(Collection<? extends K> ts) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean retainAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean removeAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void clear() {
FastMap.this.clear();
}
-
+
private final class KeyIterator implements Iterator<K> {
-
+
private int position;
private int lastNext = -1;
-
+
@Override
public boolean hasNext() {
goToNext();
return position < keys.length;
}
-
+
@Override
public K next() {
goToNext();
@@ -529,85 +544,85 @@
}
return keys[position++];
}
-
+
private void goToNext() {
int length = values.length;
- while (position < length && values[position] == null) {
+ while ((position < length) && (values[position] == null)) {
position++;
}
}
-
+
@Override
public void remove() {
iteratorRemove(lastNext);
}
}
-
+
}
-
+
private final class ValueCollection extends AbstractCollection<V> {
-
+
@Override
public int size() {
return FastMap.this.size();
}
-
+
@Override
public boolean isEmpty() {
return FastMap.this.isEmpty();
}
-
+
@Override
public boolean contains(Object o) {
return containsValue(o);
}
-
+
@Override
public Iterator<V> iterator() {
return new ValueIterator();
}
-
+
@Override
public boolean add(V v) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean addAll(Collection<? extends V> vs) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean removeAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public boolean retainAll(Collection<?> objects) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void clear() {
FastMap.this.clear();
}
-
+
private final class ValueIterator implements Iterator<V> {
-
+
private int position;
private int lastNext = -1;
-
+
@Override
public boolean hasNext() {
goToNext();
return position < values.length;
}
-
+
@Override
public V next() {
goToNext();
@@ -617,20 +632,20 @@
}
return values[position++];
}
-
+
private void goToNext() {
int length = values.length;
- while (position < length && values[position] == null) {
+ while ((position < length) && (values[position] == null)) {
position++;
}
}
-
+
@Override
public void remove() {
iteratorRemove(lastNext);
}
-
+
}
-
+
}
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverage.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverage.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverage.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverage.java Sat Feb 13 20:54:05 2010
@@ -20,33 +20,41 @@
import java.io.Serializable;
/**
- * <p>A simple class that can keep track of a running avearage of a series of numbers. One can add to or remove from the
- * series, as well as update a datum in the series. The class does not actually keep track of the series of values, just
- * its running average, so it doesn't even matter if you remove/change a value that wasn't added.</p>
+ * <p>
+ * A simple class that can keep track of a running avearage of a series of numbers. One can add to or remove
+ * from the series, as well as update a datum in the series. The class does not actually keep track of the
+ * series of values, just its running average, so it doesn't even matter if you remove/change a value that
+ * wasn't added.
+ * </p>
*/
public class FullRunningAverage implements RunningAverage, Serializable {
-
+
private int count;
private double average;
-
+
public FullRunningAverage() {
count = 0;
average = Double.NaN;
}
-
- /** @param datum new item to add to the running average */
+
+ /**
+ * @param datum
+ * new item to add to the running average
+ */
@Override
public synchronized void addDatum(double datum) {
if (++count == 1) {
average = datum;
} else {
- average = average * ((double) (count - 1) / (double) count) + datum / (double) count;
+ average = average * (count - 1) / count + datum / count;
}
}
-
+
/**
- * @param datum item to remove to the running average
- * @throws IllegalStateException if count is 0
+ * @param datum
+ * item to remove to the running average
+ * @throws IllegalStateException
+ * if count is 0
*/
@Override
public synchronized void removeDatum(double datum) {
@@ -56,35 +64,37 @@
if (--count == 0) {
average = Double.NaN;
} else {
- average = average * ((double) (count + 1) / (double) count) - datum / (double) count;
+ average = average * (count + 1) / count - datum / count;
}
}
-
+
/**
- * @param delta amount by which to change a datum in the running average
- * @throws IllegalStateException if count is 0
+ * @param delta
+ * amount by which to change a datum in the running average
+ * @throws IllegalStateException
+ * if count is 0
*/
@Override
public synchronized void changeDatum(double delta) {
if (count == 0) {
throw new IllegalStateException();
}
- average += delta / (double) count;
+ average += delta / count;
}
-
+
@Override
public synchronized int getCount() {
return count;
}
-
+
@Override
public synchronized double getAverage() {
return average;
}
-
+
@Override
public synchronized String toString() {
return String.valueOf(average);
}
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverageAndStdDev.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverageAndStdDev.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverageAndStdDev.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/FullRunningAverageAndStdDev.java Sat Feb 13 20:54:05 2010
@@ -17,35 +17,39 @@
package org.apache.mahout.cf.taste.impl.common;
-/** <p>Extends {@link FullRunningAverage} to add a running standard deviation computation.</p> */
+/**
+ * <p>
+ * Extends {@link FullRunningAverage} to add a running standard deviation computation.
+ * </p>
+ */
public final class FullRunningAverageAndStdDev extends FullRunningAverage implements RunningAverageAndStdDev {
-
+
private double stdDev;
private double sumX2;
-
+
public FullRunningAverageAndStdDev() {
stdDev = Double.NaN;
}
-
+
@Override
public synchronized double getStandardDeviation() {
return stdDev;
}
-
+
@Override
public synchronized void addDatum(double datum) {
super.addDatum(datum);
sumX2 += datum * datum;
recomputeStdDev();
}
-
+
@Override
public synchronized void removeDatum(double datum) {
super.removeDatum(datum);
sumX2 -= datum * datum;
recomputeStdDev();
}
-
+
/**
* @throws UnsupportedOperationException
*/
@@ -53,20 +57,20 @@
public void changeDatum(double delta) {
throw new UnsupportedOperationException();
}
-
+
private synchronized void recomputeStdDev() {
int count = getCount();
if (count > 1) {
double average = getAverage();
- stdDev = Math.sqrt((sumX2 - average * average * (double) count) / (double) (count - 1));
+ stdDev = Math.sqrt((sumX2 - average * average * count) / (count - 1));
} else {
stdDev = Double.NaN;
}
}
-
+
@Override
public synchronized String toString() {
return String.valueOf(String.valueOf(getAverage()) + ',' + stdDev);
}
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverage.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverage.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverage.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverage.java Sat Feb 13 20:54:05 2010
@@ -18,36 +18,36 @@
package org.apache.mahout.cf.taste.impl.common;
public final class InvertedRunningAverage implements RunningAverage {
-
+
private final RunningAverage delegate;
-
+
public InvertedRunningAverage(RunningAverage delegate) {
this.delegate = delegate;
}
-
+
@Override
public void addDatum(double datum) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void removeDatum(double datum) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void changeDatum(double delta) {
throw new UnsupportedOperationException();
}
-
+
@Override
public int getCount() {
return delegate.getCount();
}
-
+
@Override
public double getAverage() {
return -delegate.getAverage();
}
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverageAndStdDev.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverageAndStdDev.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverageAndStdDev.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/InvertedRunningAverageAndStdDev.java Sat Feb 13 20:54:05 2010
@@ -18,41 +18,41 @@
package org.apache.mahout.cf.taste.impl.common;
public final class InvertedRunningAverageAndStdDev implements RunningAverageAndStdDev {
-
+
private final RunningAverageAndStdDev delegate;
-
+
public InvertedRunningAverageAndStdDev(RunningAverageAndStdDev delegate) {
this.delegate = delegate;
}
-
+
@Override
public void addDatum(double datum) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void removeDatum(double datum) {
throw new UnsupportedOperationException();
}
-
+
@Override
public void changeDatum(double delta) {
throw new UnsupportedOperationException();
}
-
+
@Override
public int getCount() {
return delegate.getCount();
}
-
+
@Override
public double getAverage() {
return -delegate.getAverage();
}
-
+
@Override
public double getStandardDeviation() {
return delegate.getStandardDeviation();
}
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveArrayIterator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveArrayIterator.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveArrayIterator.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveArrayIterator.java Sat Feb 13 20:54:05 2010
@@ -20,19 +20,21 @@
import java.util.NoSuchElementException;
/**
- * While long[] is an Iterable, it is not an Iterable<Long>. This adapter class
- * addresses that.
+ * While long[] is an Iterable, it is not an Iterable<Long>. This adapter class addresses that.
*/
public final class LongPrimitiveArrayIterator implements LongPrimitiveIterator {
-
+
private final long[] array;
private int position;
private final int max;
-
+
/**
- * <p>Creates an {@link LongPrimitiveArrayIterator} over an entire array.</p>
- *
- * @param array array to iterate over
+ * <p>
+ * Creates an {@link LongPrimitiveArrayIterator} over an entire array.
+ * </p>
+ *
+ * @param array
+ * array to iterate over
*/
public LongPrimitiveArrayIterator(long[] array) {
if (array == null) {
@@ -42,17 +44,17 @@
this.position = 0;
this.max = array.length;
}
-
+
@Override
public boolean hasNext() {
return position < max;
}
-
+
@Override
public Long next() {
return nextLong();
}
-
+
@Override
public long nextLong() {
if (position >= array.length) {
@@ -60,7 +62,7 @@
}
return array[position++];
}
-
+
@Override
public long peek() {
if (position >= array.length) {
@@ -68,7 +70,7 @@
}
return array[position];
}
-
+
/**
* @throws UnsupportedOperationException
*/
@@ -76,17 +78,17 @@
public void remove() {
throw new UnsupportedOperationException();
}
-
+
@Override
public void skip(int n) {
if (n > 0) {
position += n;
}
}
-
+
@Override
public String toString() {
return "LongPrimitiveArrayIterator";
}
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveIterator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveIterator.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveIterator.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/LongPrimitiveIterator.java Sat Feb 13 20:54:05 2010
@@ -20,22 +20,22 @@
import java.util.Iterator;
/**
- * Adds notion of iterating over <code>long</code> primitives in the style of an
- * {@link Iterator} -- as opposed to iterating over {@link Long}. Implementations of
- * this interface however also implement {@link Iterator} and {@link Iterable} over
- * {@link Long} for convenience.
+ * Adds notion of iterating over <code>long</code> primitives in the style of an {@link Iterator} -- as
+ * opposed to iterating over {@link Long}. Implementations of this interface however also implement
+ * {@link Iterator} and {@link Iterable} over {@link Long} for convenience.
*/
public interface LongPrimitiveIterator extends SkippingIterator<Long> {
-
+
/**
* @return next <code>long</code> in iteration
- * @throws java.util.NoSuchElementException if no more elements exist in the iteration
+ * @throws java.util.NoSuchElementException
+ * if no more elements exist in the iteration
*/
long nextLong();
-
+
/**
* @return next <code>long</code> in iteration without advancing iteration
*/
long peek();
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RefreshHelper.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RefreshHelper.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RefreshHelper.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RefreshHelper.java Sat Feb 13 20:54:05 2010
@@ -17,10 +17,6 @@
package org.apache.mahout.cf.taste.impl.common;
-import org.apache.mahout.cf.taste.common.Refreshable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -28,57 +24,65 @@
import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReentrantLock;
+import org.apache.mahout.cf.taste.common.Refreshable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
- * A helper class for implementing {@link Refreshable}. This object is typically included in an implementation {@link
- * Refreshable} to implement {@link Refreshable#refresh(Collection)}. It execute the class's own supplied update logic,
- * after updating all the object's dependencies. This also ensures that dependencies are not updated multiple times.
+ * A helper class for implementing {@link Refreshable}. This object is typically included in an implementation
+ * {@link Refreshable} to implement {@link Refreshable#refresh(Collection)}. It execute the class's own
+ * supplied update logic, after updating all the object's dependencies. This also ensures that dependencies
+ * are not updated multiple times.
*/
public final class RefreshHelper implements Refreshable {
-
+
private static final Logger log = LoggerFactory.getLogger(RefreshHelper.class);
-
+
private final List<Refreshable> dependencies;
private final ReentrantLock refreshLock;
private final Callable<?> refreshRunnable;
-
- /** @param refreshRunnable encapsulates the containing object's own refresh logic */
+
+ /**
+ * @param refreshRunnable
+ * encapsulates the containing object's own refresh logic
+ */
public RefreshHelper(Callable<?> refreshRunnable) {
this.dependencies = new ArrayList<Refreshable>(3);
this.refreshLock = new ReentrantLock();
this.refreshRunnable = refreshRunnable;
}
-
+
/** Add a dependency to be refreshed first when the encapsulating object does. */
public void addDependency(Refreshable refreshable) {
if (refreshable != null) {
dependencies.add(refreshable);
}
}
-
+
public void removeDependency(Refreshable refreshable) {
if (refreshable != null) {
dependencies.remove(refreshable);
}
}
-
+
/**
- * Typically this is called in {@link Refreshable#refresh(java.util.Collection)} and is the entire body of that
- * method.
+ * Typically this is called in {@link Refreshable#refresh(java.util.Collection)} and is the entire body of
+ * that method.
*/
@Override
public void refresh(Collection<Refreshable> alreadyRefreshed) {
if (!refreshLock.isLocked()) {
refreshLock.lock();
try {
- alreadyRefreshed = buildRefreshed(alreadyRefreshed);
+ alreadyRefreshed = RefreshHelper.buildRefreshed(alreadyRefreshed);
for (Refreshable dependency : dependencies) {
- maybeRefresh(alreadyRefreshed, dependency);
+ RefreshHelper.maybeRefresh(alreadyRefreshed, dependency);
}
if (refreshRunnable != null) {
try {
refreshRunnable.call();
} catch (Exception e) {
- log.warn("Unexpected exception while refreshing", e);
+ RefreshHelper.log.warn("Unexpected exception while refreshing", e);
}
}
} finally {
@@ -86,30 +90,34 @@
}
}
}
-
+
/**
* Creates a new and empty {@link Collection} if the method parameter is <code>null</code>.
- *
- * @param currentAlreadyRefreshed {@link Refreshable}s to refresh later on
- * @return an empty {@link Collection} if the method param was <code>null</code> or the unmodified method param.
+ *
+ * @param currentAlreadyRefreshed
+ * {@link Refreshable}s to refresh later on
+ * @return an empty {@link Collection} if the method param was <code>null</code> or the unmodified method
+ * param.
*/
public static Collection<Refreshable> buildRefreshed(Collection<Refreshable> currentAlreadyRefreshed) {
return currentAlreadyRefreshed == null ? new HashSet<Refreshable>(3) : currentAlreadyRefreshed;
}
-
+
/**
- * Adds the specified {@link Refreshable} to the given collection of {@link Refreshable}s if it is not already there
- * and immediately refreshes it.
- *
- * @param alreadyRefreshed the collection of {@link Refreshable}s
- * @param refreshable the {@link Refreshable} to potentially add and refresh
+ * Adds the specified {@link Refreshable} to the given collection of {@link Refreshable}s if it is not
+ * already there and immediately refreshes it.
+ *
+ * @param alreadyRefreshed
+ * the collection of {@link Refreshable}s
+ * @param refreshable
+ * the {@link Refreshable} to potentially add and refresh
*/
public static void maybeRefresh(Collection<Refreshable> alreadyRefreshed, Refreshable refreshable) {
if (!alreadyRefreshed.contains(refreshable)) {
alreadyRefreshed.add(refreshable);
- log.info("Added refreshable: {}", refreshable);
+ RefreshHelper.log.info("Added refreshable: {}", refreshable);
refreshable.refresh(alreadyRefreshed);
- log.info("Refreshed: {}", alreadyRefreshed);
+ RefreshHelper.log.info("Refreshed: {}", alreadyRefreshed);
}
}
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/Retriever.java Sat Feb 13 20:54:05 2010
@@ -19,14 +19,20 @@
import org.apache.mahout.cf.taste.common.TasteException;
-/** <p>Implementations can retrieve a value for a given key.</p> */
-public interface Retriever<K, V> {
-
+/**
+ * <p>
+ * Implementations can retrieve a value for a given key.
+ * </p>
+ */
+public interface Retriever<K,V> {
+
/**
- * @param key key for which a value should be retrieved
+ * @param key
+ * key for which a value should be retrieved
* @return value for key
- * @throws TasteException if an error occurs while retrieving the value
+ * @throws TasteException
+ * if an error occurs while retrieving the value
*/
V get(K key) throws TasteException;
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverage.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverage.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverage.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverage.java Sat Feb 13 20:54:05 2010
@@ -18,34 +18,45 @@
package org.apache.mahout.cf.taste.impl.common;
/**
- * <p>Interface for classes that can keep track of a running average of a series of numbers. One can add to or remove
- * from the series, as well as update a datum in the series. The class does not actually keep track of the series of
- * values, just its running average, so it doesn't even matter if you remove/change a value that wasn't added.</p>
+ * <p>
+ * Interface for classes that can keep track of a running average of a series of numbers. One can add to or
+ * remove from the series, as well as update a datum in the series. The class does not actually keep track of
+ * the series of values, just its running average, so it doesn't even matter if you remove/change a value that
+ * wasn't added.
+ * </p>
*/
public interface RunningAverage {
-
+
/**
- * @param datum new item to add to the running average
- * @throws IllegalArgumentException if datum is {@link Double#NaN}
+ * @param datum
+ * new item to add to the running average
+ * @throws IllegalArgumentException
+ * if datum is {@link Double#NaN}
*/
void addDatum(double datum);
-
+
/**
- * @param datum item to remove to the running average
- * @throws IllegalArgumentException if datum is {@link Double#NaN}
- * @throws IllegalStateException if count is 0
+ * @param datum
+ * item to remove to the running average
+ * @throws IllegalArgumentException
+ * if datum is {@link Double#NaN}
+ * @throws IllegalStateException
+ * if count is 0
*/
void removeDatum(double datum);
-
+
/**
- * @param delta amount by which to change a datum in the running average
- * @throws IllegalArgumentException if delta is {@link Double#NaN}
- * @throws IllegalStateException if count is 0
+ * @param delta
+ * amount by which to change a datum in the running average
+ * @throws IllegalArgumentException
+ * if delta is {@link Double#NaN}
+ * @throws IllegalStateException
+ * if count is 0
*/
void changeDatum(double delta);
-
+
int getCount();
-
+
double getAverage();
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverageAndStdDev.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverageAndStdDev.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverageAndStdDev.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/RunningAverageAndStdDev.java Sat Feb 13 20:54:05 2010
@@ -17,10 +17,14 @@
package org.apache.mahout.cf.taste.impl.common;
-/** <p>Extends {@link RunningAverage} by adding standard deviation too.</p> */
+/**
+ * <p>
+ * Extends {@link RunningAverage} by adding standard deviation too.
+ * </p>
+ */
public interface RunningAverageAndStdDev extends RunningAverage {
-
+
/** @return standard deviation of data */
double getStandardDeviation();
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SamplingLongPrimitiveIterator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SamplingLongPrimitiveIterator.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SamplingLongPrimitiveIterator.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SamplingLongPrimitiveIterator.java Sat Feb 13 20:54:05 2010
@@ -17,37 +17,37 @@
package org.apache.mahout.cf.taste.impl.common;
-import org.apache.mahout.common.RandomUtils;
-
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;
+import org.apache.mahout.common.RandomUtils;
+
/**
- * Wraps an {@link Iterator} and returns only some subset of the elements that it would, as determined by a sampling
- * rate parameter.
+ * Wraps an {@link Iterator} and returns only some subset of the elements that it would, as determined by a
+ * sampling rate parameter.
*/
public final class SamplingLongPrimitiveIterator extends AbstractLongPrimitiveIterator {
-
+
private static final Random r = RandomUtils.getRandom();
-
+
private final LongPrimitiveIterator delegate;
private final double samplingRate;
private long next;
private boolean hasNext;
-
+
public SamplingLongPrimitiveIterator(LongPrimitiveIterator delegate, double samplingRate) {
this.delegate = delegate;
this.samplingRate = samplingRate;
this.hasNext = true;
doNext();
}
-
+
@Override
public boolean hasNext() {
return hasNext;
}
-
+
@Override
public long nextLong() {
if (hasNext) {
@@ -57,7 +57,7 @@
}
throw new NoSuchElementException();
}
-
+
@Override
public long peek() {
if (hasNext) {
@@ -65,10 +65,10 @@
}
throw new NoSuchElementException();
}
-
+
private void doNext() {
int toSkip = 0;
- while (r.nextDouble() >= samplingRate) {
+ while (SamplingLongPrimitiveIterator.r.nextDouble() >= samplingRate) {
toSkip++;
}
// Really, would be nicer to select value from geometric distribution, for small values of samplingRate
@@ -80,12 +80,12 @@
next = delegate.next();
found = true;
}
-
+
if (!found) {
hasNext = false;
}
}
-
+
/**
* @throws UnsupportedOperationException
*/
@@ -93,15 +93,14 @@
public void remove() {
throw new UnsupportedOperationException();
}
-
+
@Override
public void skip(int n) {
delegate.skip((int) (n / samplingRate)); // Kind of an approximation, but this is expected skip
}
-
-
+
public static LongPrimitiveIterator maybeWrapIterator(LongPrimitiveIterator delegate, double samplingRate) {
return samplingRate >= 1.0 ? delegate : new SamplingLongPrimitiveIterator(delegate, samplingRate);
}
-
+
}
\ No newline at end of file
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SkippingIterator.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SkippingIterator.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SkippingIterator.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/SkippingIterator.java Sat Feb 13 20:54:05 2010
@@ -19,14 +19,17 @@
import java.util.Iterator;
-/** Adds ability to skip ahead in an iterator, perhaps more efficiently than by calling {@link #next()} repeatedly. */
+/**
+ * Adds ability to skip ahead in an iterator, perhaps more efficiently than by calling {@link #next()}
+ * repeatedly.
+ */
public interface SkippingIterator<V> extends Iterator<V> {
-
+
/**
- * Skip the next n elements supplied by this {@link Iterator}. If there are less than n elements remaining, this skips
- * all remaining elements in the {@link Iterator}. This method has the same effect as calling {@link #next()} n times,
- * except that it will never throw {@link java.util.NoSuchElementException}.
+ * Skip the next n elements supplied by this {@link Iterator}. If there are less than n elements remaining,
+ * this skips all remaining elements in the {@link Iterator}. This method has the same effect as calling
+ * {@link #next()} n times, except that it will never throw {@link java.util.NoSuchElementException}.
*/
void skip(int n);
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/WeightedRunningAverage.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/WeightedRunningAverage.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/WeightedRunningAverage.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/WeightedRunningAverage.java Sat Feb 13 20:54:05 2010
@@ -20,35 +20,35 @@
import java.io.Serializable;
public final class WeightedRunningAverage implements RunningAverage, Serializable {
-
+
private double totalWeight;
private double average;
-
+
public WeightedRunningAverage() {
totalWeight = 0.0;
average = Double.NaN;
}
-
+
@Override
public synchronized void addDatum(double datum) {
addDatum(datum, 1.0);
}
-
+
public synchronized void addDatum(double datum, double weight) {
double oldTotalWeight = totalWeight;
totalWeight += weight;
if (oldTotalWeight <= 0.0) {
average = datum * weight;
} else {
- average = average * (oldTotalWeight / totalWeight) + datum / totalWeight;
+ average = average * oldTotalWeight / totalWeight + datum / totalWeight;
}
}
-
+
@Override
public synchronized void removeDatum(double datum) {
removeDatum(datum, 1.0);
}
-
+
public synchronized void removeDatum(double datum, double weight) {
double oldTotalWeight = totalWeight;
totalWeight -= weight;
@@ -56,40 +56,40 @@
average = Double.NaN;
totalWeight = 0.0;
} else {
- average = average * (oldTotalWeight / totalWeight) - datum / totalWeight;
+ average = average * oldTotalWeight / totalWeight - datum / totalWeight;
}
}
-
+
@Override
public synchronized void changeDatum(double delta) {
changeDatum(delta, 1.0);
}
-
+
public synchronized void changeDatum(double delta, double weight) {
if (weight > totalWeight) {
throw new IllegalArgumentException();
}
- average += (delta * weight) / totalWeight;
+ average += delta * weight / totalWeight;
}
-
- public synchronized double getTotalWeight() {
+
+ public synchronized double getTotalWeight() {
return totalWeight;
}
-
+
/** @return {@link #getTotalWeight()} */
@Override
- public synchronized int getCount() {
+ public synchronized int getCount() {
return (int) totalWeight;
}
-
+
@Override
public synchronized double getAverage() {
return average;
}
-
+
@Override
public synchronized String toString() {
return String.valueOf(average);
}
-
+
}
Modified: lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/jdbc/AbstractJDBCComponent.java
URL: http://svn.apache.org/viewvc/lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/jdbc/AbstractJDBCComponent.java?rev=909912&r1=909911&r2=909912&view=diff
==============================================================================
--- lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/jdbc/AbstractJDBCComponent.java (original)
+++ lucene/mahout/trunk/core/src/main/java/org/apache/mahout/cf/taste/impl/common/jdbc/AbstractJDBCComponent.java Sat Feb 13 20:54:05 2010
@@ -17,51 +17,55 @@
package org.apache.mahout.cf.taste.impl.common.jdbc;
-import org.apache.mahout.cf.taste.common.TasteException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import java.sql.ResultSet;
+import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
-import java.sql.ResultSet;
-import java.sql.SQLException;
+
+import org.apache.mahout.cf.taste.common.TasteException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* A helper class with common elements for several JDBC-related components.
*/
public abstract class AbstractJDBCComponent {
-
+
private static final Logger log = LoggerFactory.getLogger(AbstractJDBCComponent.class);
-
+
protected static final String DEFAULT_DATASOURCE_NAME = "jdbc/taste";
private static final int DEFAULT_FETCH_SIZE = 1000; // A max, "big" number of rows to buffer at once
-
+
protected static void checkNotNullAndLog(String argName, Object value) {
- if (value == null || value.toString().length() == 0) {
+ if ((value == null) || (value.toString().length() == 0)) {
throw new IllegalArgumentException(argName + " is null or empty");
}
- log.debug("{}: {}", argName, value);
+ AbstractJDBCComponent.log.debug("{}: {}", argName, value);
}
-
+
protected static void checkNotNullAndLog(String argName, Object[] values) {
- if (values == null || values.length == 0) {
+ if ((values == null) || (values.length == 0)) {
throw new IllegalArgumentException(argName + " is null or zero-length");
}
for (Object value : values) {
- checkNotNullAndLog(argName, value);
+ AbstractJDBCComponent.checkNotNullAndLog(argName, value);
}
}
-
-
+
/**
- * <p>Looks up a {@link DataSource} by name from JNDI. "java:comp/env/" is prepended to the argument before looking up
- * the name in JNDI.</p>
- *
- * @param dataSourceName JNDI name where a {@link DataSource} is bound (e.g. "jdbc/taste")
+ * <p>
+ * Looks up a {@link DataSource} by name from JNDI. "java:comp/env/" is prepended to the argument before
+ * looking up the name in JNDI.
+ * </p>
+ *
+ * @param dataSourceName
+ * JNDI name where a {@link DataSource} is bound (e.g. "jdbc/taste")
* @return {@link DataSource} under that JNDI name
- * @throws TasteException if a JNDI error occurs
+ * @throws TasteException
+ * if a JNDI error occurs
*/
protected static DataSource lookupDataSource(String dataSourceName) throws TasteException {
Context context = null;
@@ -75,18 +79,18 @@
try {
context.close();
} catch (NamingException ne) {
- log.warn("Error while closing Context; continuing...", ne);
+ AbstractJDBCComponent.log.warn("Error while closing Context; continuing...", ne);
}
}
}
}
-
+
protected int getFetchSize() {
- return DEFAULT_FETCH_SIZE;
+ return AbstractJDBCComponent.DEFAULT_FETCH_SIZE;
}
-
+
protected void advanceResultSet(ResultSet resultSet, int n) throws SQLException {
resultSet.relative(n);
}
-
+
}