1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package Torello.JavaDoc.SyntaxHiLite;

import Torello.Java.UnreachableError;
import java.io.File;

/** A 64-Bit Java-{@code Integer} implementation of a File-System Cache. */
public class HLC64 extends AbstractHashCodeHLC<Long>
{
    /**
     * Initializes a 63-Bit (java.lang.Long) Cache in the specified directory.
     * <!-- Content below this line was block copied from class 'AbstractHashCodeHLC' -->
     * 
     * This will load the hashCodes table to memory from the file-system directory identified
     * by {@code String}-Parameter {@code 'cacheSaveDirectory'}.  An exception shall be thrown
     * if this file is not found, or the specified directory doesn't exist.
     *
     * @param cacheSaveDirectory This constructor presumes that this cache has been used and
     * visited before.  This directory name should point to your local-cache of the
     * {@code HiLite.ME} Server Code hilite past-operations.
     *
     * @throws CacheError This error will throw if the cache has not been instantiated, or
     * is corrupted.  If the specified directory does not exist, then this {@code Error} shall
     * also throw.  The chain-cause {@code Throwable} should be visible, and is included as the 
     * {@code Throwable.getCause()}.
     */
    public HLC64(String cacheSaveDirectory)
    { super(cacheSaveDirectory, Long.class); }

    public Long computeCacheKey(
            final String    codeTypeParam,
            final boolean   includeLineNumbers,
            final byte      styleNum,
            final String    sourceCodeAsString
        )
    {
        final long FNV_PRIME = 0x100000001b3L;
        long hash = 0xcbf29ce484222325L;
    
        // Mix in codeTypeParam
        for (int i = 0; i < codeTypeParam.length(); i++) {
            hash ^= codeTypeParam.charAt(i);
            hash *= FNV_PRIME;
        }
    
        // Mix in includeLineNumbers
        hash ^= (includeLineNumbers ? 1 : 0);
        hash *= FNV_PRIME;
    
        // Mix in styleNum
        hash ^= styleNum;
        hash *= FNV_PRIME;
    
        // Mix in sourceCodeAsString
        for (int i = 0; i < sourceCodeAsString.length(); i++) {
            hash ^= sourceCodeAsString.charAt(i);
            hash *= FNV_PRIME;
        }
    
        return hash;
    }


    // Implementation of abstract-parent method.
    // All it does is convert a Hash-Code into a Directory name.
    // NOTE: This method is package private

    String getSubDirName(Long hashCode)
    {
        final long absVal = (hashCode.longValue() < 0)
            ? -hashCode.longValue()
            : hashCode.longValue();

        return
            this.cacheSaveDirectory + 
            zeroPad10e4(absVal % AbstractHashCodeHLC.NUM_DIRS) + File.separator;
    }

    private static String zeroPad10e4(final long l)
    {
        if (l < 10)     return "000"    + l;
        if (l < 100)    return "00"     + l;
        if (l < 1000)   return "0"      + l;
        if (l < 10000)  return ""       + l;

        throw new UnreachableError();
    }
}