Skip to content

Commit

Permalink
Support enums in cache helper (#3901) [ci fast]
Browse files Browse the repository at this point in the history

Signed-off-by: Ben Sherman <[email protected]>
Signed-off-by: Paolo Di Tommaso <[email protected]>
Co-authored-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
bentsherman and pditommaso authored Apr 29, 2023
1 parent eeb4b5c commit e4b977a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions modules/nf-commons/src/main/nextflow/util/CacheHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ public static Hasher hasher( Hasher hasher, Object value, HashMode mode ) {
if( value instanceof Short )
return hasher.putShort((Short) value);

if( value instanceof Integer)
if( value instanceof Integer)
return hasher.putInt((Integer) value);

if( value instanceof Long )
if( value instanceof Long )
return hasher.putLong((Long) value);

if( value instanceof Float )
Expand Down Expand Up @@ -217,6 +217,10 @@ public static Hasher hasher( Hasher hasher, Object value, HashMode mode ) {
return ((CacheFunnel) value).funnel(hasher,mode);
}

if( value instanceof Enum ) {
return hasher.putUnencodedChars( value.getClass().getName() + "." + value );
}

Bolts.debug1(log, FIRST_ONLY, "[WARN] Unknown hashing type: "+value.getClass());
return hasher.putInt( value.hashCode() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import test.TestHelper
*/
class CacheHelperTest extends Specification {

enum TestEnum { TEST_A, TEST_B, TEST_C }

def 'test hashCode' () {

setup:
Expand All @@ -44,6 +46,7 @@ class CacheHelperTest extends Specification {
def anObjectArray = Hashing.murmur3_128().newHasher().putInt(1).putInt(2).putInt(3).hash()
def aMap = Hashing.murmur3_128().newHasher().putInt(1).putUnencodedChars('String1').putBoolean(true).hash()
def aList = Hashing.murmur3_128().newHasher().putUnencodedChars('A').putUnencodedChars('B').putUnencodedChars('C').hash()
def anEnum = Hashing.murmur3_128().newHasher().putUnencodedChars('nextflow.util.CacheHelperTest$TestEnum.TEST_A').hash()

def file = Files.createTempFile('test', null)
file.deleteOnExit()
Expand All @@ -66,6 +69,7 @@ class CacheHelperTest extends Specification {
CacheHelper.hasher([1,2,3] as Object[]).hash() == anObjectArray
CacheHelper.hasher( [f1: 1, f2: 'String1', f3: true] ) .hash() == aMap
CacheHelper.hasher( ['A','B','C'] ).hash() == aList
CacheHelper.hasher(TestEnum.TEST_A).hash() == anEnum
CacheHelper.hasher(file).hash() == aFile

CacheHelper.hasher(['abc',123]).hash() == CacheHelper.hasher(['abc',123]).hash()
Expand Down

0 comments on commit e4b977a

Please sign in to comment.