001package Torello.JavaDoc.SyntaxHiLite;
002
003import Torello.Java.UnreachableError;
004import java.io.File;
005
006public class HLC64 extends AbstractHashCodeHLC<Long>
007{
008    public HLC64(String cacheSaveDirectory)
009    { super(cacheSaveDirectory, Long.class); }
010
011    public Long computeCacheKey(
012            final String    codeTypeParam,
013            final boolean   includeLineNumbers,
014            final byte      styleNum,
015            final String    sourceCodeAsString
016        )
017    {
018        final long FNV_PRIME = 0x100000001b3L;
019        long hash = 0xcbf29ce484222325L;
020    
021        // Mix in codeTypeParam
022        for (int i = 0; i < codeTypeParam.length(); i++) {
023            hash ^= codeTypeParam.charAt(i);
024            hash *= FNV_PRIME;
025        }
026    
027        // Mix in includeLineNumbers
028        hash ^= (includeLineNumbers ? 1 : 0);
029        hash *= FNV_PRIME;
030    
031        // Mix in styleNum
032        hash ^= styleNum;
033        hash *= FNV_PRIME;
034    
035        // Mix in sourceCodeAsString
036        for (int i = 0; i < sourceCodeAsString.length(); i++) {
037            hash ^= sourceCodeAsString.charAt(i);
038            hash *= FNV_PRIME;
039        }
040    
041        return hash;
042    }
043
044    String getSubDirName(Long hashCode)
045    {
046        final long absVal = (hashCode.longValue() < 0)
047            ? -hashCode.longValue()
048            : hashCode.longValue();
049
050        return
051            this.cacheSaveDirectory + 
052            zeroPad10e4(absVal % AbstractHashCodeHLC.NUM_DIRS) + File.separator;
053    }
054
055    private static String zeroPad10e4(final long l)
056    {
057        if (l < 10)     return "000"    + l;
058        if (l < 100)    return "00"     + l;
059        if (l < 1000)   return "0"      + l;
060        if (l < 10000)  return ""       + l;
061
062        throw new UnreachableError();
063    }
064}