Skip to content

Commit

Permalink
v0.5.5 Bugfix for 0 length collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Yates committed Nov 25, 2013
1 parent aeedd7a commit d2ce00d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Obviously requires at least Groovy 2.0.5 (so that the extension system exists)

Usage:

@Grab( 'com.bloidonia:groovy-common-extensions:0.5.4' )
@Grab( 'com.bloidonia:groovy-common-extensions:0.5.5' )

and the following methods will be available to you:

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'signing'
sourceCompatibility=1.6
targetCompatibility=1.6

version = '0.5.4'
version = '0.5.5'

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ class CollectionExtensionMethods {
}

@groovy.transform.Immutable( knownImmutables=[ 'median' ])
public static class AverageStats<V extends Number> {
Double mean
V median
Double variance
Double stdDev
public static class AverageStats {
BigDecimal mean
BigDecimal median
BigDecimal variance
BigDecimal stdDev

String toString() {
"AverageStats( mean:$mean, median:$median, variance:$variance, stdDev:$stdDev )"
Expand All @@ -300,15 +300,18 @@ class CollectionExtensionMethods {
* @param collection The {@code Collection} of {@code Number} elements to generate stats for
* @return A populated {@code AverageStats} object
*/
static <V extends Number> AverageStats<V> average( Collection<V> collection ) {
static <V extends Number> AverageStats average( Collection<V> collection ) {
int size = collection.size()
V sum = collection.sum()
int zsize = size - 1
V median = size % 2 == 1 ?
collection.sort( false )[ ( zsize / 2 ).toInteger() ] :
collection.sort( false )[ [ Math.floor( zsize / 2 ), Math.ceil( zsize / 2.0 ) ]*.toInteger() ].sum() / 2
double mean = sum / size
double variance = 0
if( size == 0 ) {
return new AverageStats( mean: 0, median: 0, variance: 0, stdDev: 0 )
}
V sum = collection.sum()
int zsize = size - 1
BigDecimal median = size % 2 == 1 ?
collection.sort( false )[ ( zsize / 2 ).toInteger() ] :
collection.sort( false )[ [ Math.floor( zsize / 2 ), Math.ceil( zsize / 2.0 ) ]*.toInteger() ].sum() / 2
BigDecimal mean = sum / size
BigDecimal variance = 0

for( V num : collection ) {
variance += ( mean - num ) * ( mean - num )
Expand Down
14 changes: 12 additions & 2 deletions src/test/groovy/tests/CollectionTests.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class CollectionTests extends Specification {
def avg = [ 332G, 42G, 100G ].average()
expect:
avg.mean == 158
avg.median == 100G
avg.median == 100
String.format( '%.7g', avg.variance ) == '15698.67'
String.format( '%.6g', avg.stdDev ) == '125.294'
}
Expand All @@ -154,8 +154,18 @@ class CollectionTests extends Specification {
def avg = [ 332.0G, 998.0G, 42.0G, 100.0G ].average()
expect:
avg.mean == 368
avg.median == 216.0G
avg.median == 216
String.format( '%.7g', avg.variance ) == '144074.0'
String.format( '%.6g', avg.stdDev ) == '379.571'
}

def 'check zero length averages'() {
given:
def avg = [].average()
expect:
avg.mean == 0
avg.median == 0
avg.variance == 0
avg.stdDev == 0
}
}

0 comments on commit d2ce00d

Please sign in to comment.