Skip to content

Commit

Permalink
Move some of the static methods of Dataset to FlatDataCollection and …
Browse files Browse the repository at this point in the history
…TypeConversions classes.
  • Loading branch information
datumbox committed Apr 2, 2015
1 parent 8fe351e commit ae92e16
Show file tree
Hide file tree
Showing 51 changed files with 473 additions and 411 deletions.
Expand Up @@ -16,6 +16,7 @@
*/
package com.datumbox.common.dataobjects;

import com.datumbox.common.utilities.TypeConversions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -53,17 +54,17 @@ private void addRemoveValues(AssociativeArray array, int sign) {
//sign should be -1 or 1
for(Map.Entry<Object, Object> entry : array.entrySet()) {
Object column = entry.getKey();
Double previousValue = Dataset.toDouble(internalData.get(column));
Double previousValue = TypeConversions.toDouble(internalData.get(column));
if(previousValue==null) {
previousValue=0.0;
}
internalData.put(column, previousValue+ sign*Dataset.toDouble(entry.getValue()));
internalData.put(column, previousValue+ sign*TypeConversions.toDouble(entry.getValue()));
}
}

public final void multiplyValues(double multiplier) {
for(Map.Entry<Object, Object> entry : internalData.entrySet()) {
Double previousValue = Dataset.toDouble(entry.getValue());
Double previousValue = TypeConversions.toDouble(entry.getValue());
if(previousValue==null) {
continue;
}
Expand All @@ -80,7 +81,7 @@ public final Object get(Object key) {
}

public final Double getDouble(Object key) {
return Dataset.toDouble(internalData.get(key));
return TypeConversions.toDouble(internalData.get(key));
}

public final Object put(Object key, Object value) {
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.datumbox.common.dataobjects;

import com.datumbox.common.utilities.TypeConversions;
import java.util.Collection;
import java.util.Iterator;

Expand Down Expand Up @@ -64,7 +65,7 @@ public boolean hasNext() {

@Override
public Double next() {
return Dataset.toDouble(objectIterator.next());
return TypeConversions.toDouble(objectIterator.next());
}

@Override
Expand Down
89 changes: 5 additions & 84 deletions src/main/java/com/datumbox/common/dataobjects/Dataset.java
Expand Up @@ -18,8 +18,8 @@

import com.datumbox.common.utilities.MapFunctions;
import com.datumbox.common.utilities.RandomValue;
import com.datumbox.common.utilities.TypeConversions;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -93,97 +93,18 @@ public Record get(Integer id) {
return recordList.get(id);
}


/**
* Converts safely any Number to Double.
* @param o
* @return
*/
public static Double toDouble(Object o) {
if(o==null) {
return null;
}
if(o instanceof Boolean) {
return ((Boolean)o)?1.0:0.0;
}
return ((Number)o).doubleValue();
}

/**
* Converts safely any Number to Integer.
* @param o
* @return
*/
public static Integer toInteger(Object o) {
if(o==null) {
return null;
}
if(o instanceof Boolean) {
return ((Boolean)o)?1:0;
}
return ((Number)o).intValue();
}

/**
* Converts to Double[] safely the original FlatDataCollection by using the
* iteratorDouble.
*
* @param flatDataCollection
* @return
*/
public static Double[] copyCollection2DoubleArray(FlatDataCollection flatDataCollection) {
int n = flatDataCollection.size();
Double[] doubleArray = new Double[n];
int i=0;

Iterator<Double> it = flatDataCollection.iteratorDouble();
while(it.hasNext()) {
doubleArray[i++] = it.next();
}

return doubleArray;
}

/**
* Converts to Object[] the original FlatDataCollection. The method is used to
* generate a deep copy of the flatDataCollection and it is called in order to
* avoid modifying the original array.
*
* @param <T>
* @param c
* @param flatDataCollection
* @return
* @throws IllegalArgumentException
*/
@SuppressWarnings("unchecked")
public static <T> T[] copyCollection2Array(Class<T> c, FlatDataCollection flatDataCollection) throws IllegalArgumentException {
int n = flatDataCollection.size();
if(n==0) {
throw new IllegalArgumentException();
}

T[] copy = (T[]) Array.newInstance(c, n);

int i=0;
for (Object value : flatDataCollection) {
copy[i++]=c.cast(value);
}

return copy;
}

/**
* Replaces the actual values of the flatDataCollection with their ranks and
* returns in the tieCounter the keys that occur more than once and the
* number of occurrences. The tieCounter does not store the list and ranks
* of the actual ties as in the PHP implementation because we never use them.
* of the actual ties because we never use them.
*
* @param flatDataCollection
* @return
*/
public static AssociativeArray getRanksFromValues(FlatDataList flatDataCollection) {

AssociativeArray tiesCounter = new AssociativeArray(new LinkedHashMap<>()); //ConcurrentSkipListMap
AssociativeArray tiesCounter = new AssociativeArray(new LinkedHashMap<>());
Map<Object, Double> key2AvgRank = new LinkedHashMap<>();

_buildRankArrays(flatDataCollection.internalData, tiesCounter, key2AvgRank); //tiesCounter and key2AvgRank are modified
Expand All @@ -200,7 +121,7 @@ public static AssociativeArray getRanksFromValues(FlatDataList flatDataCollectio
* Replaces the actual values of the associativeArray with their ranks and
* returns in the tieCounter the keys that occur more than once and the
* number of occurrences. The tieCounter does not store the list and ranks
* of the actual ties as in the PHP implementation because we never use them.
* of the actual ties because we never use them.
*
* @param associativeArray
* @return
Expand Down Expand Up @@ -249,7 +170,7 @@ private static void _buildRankArrays(Collection<Object> dataCollection, Associat
while(it.hasNext()) {
Map.Entry<Object, Object> entry = it.next();
Object key = entry.getKey();
double count = Dataset.toDouble(entry.getValue());
double count = TypeConversions.toDouble(entry.getValue());
if(count<=1.0) {
//keep as ties only keys that occur more than once.
//tiesCounter.remove(key);
Expand Down
Expand Up @@ -17,6 +17,7 @@
package com.datumbox.common.dataobjects;

import com.google.common.collect.HashMultiset;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand All @@ -27,6 +28,46 @@
* @author bbriniotis
*/
public final class FlatDataCollection extends DataStructureCollection<Collection<Object>> implements Iterable<Object> {

/**
* Converts to Object[] the original FlatDataCollection. The method is used to
* generate a deep copy of the flatDataCollection and it is called in order to
* avoid modifying the original array.
*
* @param <T>
* @param c
* @return
* @throws IllegalArgumentException
*/
public <T> T[] copyCollection2Array(Class<T> c) throws IllegalArgumentException {
int n = internalData.size();
if (n == 0) {
throw new IllegalArgumentException();
}
T[] copy = (T[]) Array.newInstance(c, n);
int i = 0;
for (Object value : internalData) {
copy[i++] = c.cast(value);
}
return copy;
}

/**
* Converts to Double[] safely the original FlatDataCollection by using the
* iteratorDouble.
*
* @return
*/
public Double[] copyCollection2DoubleArray() {
int n = internalData.size();
Double[] doubleArray = new Double[n];
int i = 0;
Iterator<Double> it = this.iteratorDouble();
while (it.hasNext()) {
doubleArray[i++] = it.next();
}
return doubleArray;
}

public FlatDataCollection() throws IllegalArgumentException {
throw new IllegalArgumentException();
Expand Down
Expand Up @@ -16,6 +16,7 @@
*/
package com.datumbox.common.dataobjects;

import com.datumbox.common.utilities.TypeConversions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
Expand Down Expand Up @@ -48,7 +49,7 @@ public final Object get(int index) {
}

public final Double getDouble(int index) {
return Dataset.toDouble(internalData.get(index));
return TypeConversions.toDouble(internalData.get(index));
}

public final boolean add(Object e) {
Expand Down

0 comments on commit ae92e16

Please sign in to comment.