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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package Torello.JavaDoc.SyntaxHiLite;

import java.io.File;

import Torello.Java.FileRW;

class CheckInOut 
{
    static <NUMBER extends Number> String get(
            final String                        sourceCodeAsString,
            final NUMBER                        hashCode,
            final AbstractHashCodeHLC<NUMBER>   THIS
        )
    {
        if (! THIS.hashCodes.contains(hashCode)) return null;

        final String root = THIS.getSubDirName(hashCode) + "H" + hashCode.toString();


        // Does a try-catch & re-throw as CacheError, with appropriate Error-Message
        // If the file is not found - PRECISELY BECAUSE THIS WAS A CACHE-HIT => ...
        //   ==> This would be a corrupted Cache.  The method below throws 'CacheError' if there 
        //       are any problems reading this file.

        final String saved = CheckInOut.readFile(root + "-SOURCE.sdat");


        // If the loaded Source-Code (from disk) - after a character for character comparison - is
        // **EXACTLY EQUAL** to the Input / Requested Hi-Lite String, then the Hi-Lited Source-File
        // is loaded from disk, and returned.
        // 
        // CASE 1:
        // If there is a Cache-Hit, followed by a successful "Equals Comparison", but then the 
        // HTML-File fails to load from disk - then (again) this must be a corrupted Cache and the
        // method below will throw a Cache-Error if the file fails to load.
        // 
        // CASE 2;
        // if the "Equals Comparison" fails - the the HTML-File is an outdated File.  This happens 
        // if-and-when the Textual-Changes slipped by without modifying the String.hashCode() 
        // computation.  This is why the "Equals Comparison is mandatory".  If the "equals" fails,
        // then return null because the file will have to be re-hilited.

        return saved.equals(sourceCodeAsString)
            ? CheckInOut.readFile(root + "-HILITE.sdat")
            : null;
    }

    static <NUMBER extends Number> void checkIn(
            final String                        sourceCodeAsString,
            final String                        hilitedCodeAsString,
            final NUMBER                        hashCode,
            final AbstractHashCodeHLC<NUMBER>   THIS
        )
    {
        final String root = THIS.getSubDirName(hashCode);

        try
        {
                final File f = new File(root);
                if (! f.exists()) f.mkdirs();
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an exception when accessing or creating the cache directory:\n" +
                "    [" + root + "...sdat].\n" +
                "See cause exception Throwable.getCause() for details.",
                e
            );
        }

        final String fileName = root + "H" + hashCode.toString();

        try
        {
            FileRW.writeObjectToFile(sourceCodeAsString,    fileName + "-SOURCE.sdat", true);
            FileRW.writeObjectToFile(hilitedCodeAsString,   fileName + "-HILITE.sdat", true);
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an exception when writing to the cache directory:\n" +
                "[" + root + "...sdat].\n" +
                "Attempting to write Files:\n" + 
                "    [" + fileName + "-SOURCE.sdat].\n" +
                "    [" + fileName + "-HILITE.sdat].\n" +
                "See cause exception Throwable.getCause() for details.",
                e
            );
        }

        THIS.hashCodes.add(hashCode);
        // DEBUGING System.out.println(" CHECKEDIN ");
    }

    private static String readFile(final String fileName)
    {
        try
            { return FileRW.readObjectFromFileNOCNFE(fileName, String.class, true); }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error reading a file from the cache-directory: " +
                "    [" + fileName + "]\n" +
                "Please see cause throwable.getCause() for more details",
                e
            );
        }
    }
}