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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package Torello.JavaDoc.SyntaxHiLite;

import Torello.Java.FileNode;
import Torello.Java.FileRW;
import Torello.Java.FileTransfer;
import Torello.Java.StorageWriter;

import java.io.File;

import java.util.TreeSet;


// These are just some simple methods that allow for reading and writing a Java TreeSet that 
// contains all of the Hash-Codes which have been saved to this Cache.  When a file is written to 
// the Cache, a simple Hash-Code is generated using the String-Contents of that file.  Currently,
// Java's Standard String.hashCode() is used to produce the Hash-Code.
// 
// This means summing the value of every character in the String 

class TreeSetMethods
{
    private static final String HASH_SAVE_TREE = "HILITED_STRINGS_HASH_CODE.ts";

    static String checkCSD(String cacheSaveDirectory)
    {
        cacheSaveDirectory = cacheSaveDirectory.trim();

        if (! cacheSaveDirectory.endsWith(File.separator))
            cacheSaveDirectory = cacheSaveDirectory + File.separator;

        File f  = new File(cacheSaveDirectory);

        if (! f.exists()) throw new CacheError(
            "The specified cache-directory does not exist on the file-system: " +
            "[" + cacheSaveDirectory + "]"
        );

        return cacheSaveDirectory;
    }

    @SuppressWarnings("unchecked") // the last line of this method
    static <NUMBER extends Number> TreeSet<NUMBER> checkTS
        (final String cacheSaveDirectory, final Class<NUMBER> c)
    {
        String  fName   = cacheSaveDirectory + HASH_SAVE_TREE;
        File    f       = new File(fName);

        if (! f.exists()) throw new CacheError(
            "The current-cache directory does not contain a primary-cache file: " +
            "[" + fName + "]"
        );

        final TreeSet<?> ts;

        try
            { ts = FileRW.readObjectFromFile(fName, TreeSet.class, true); }

        catch (ClassNotFoundException e)
        {
            throw new CacheError(
                "There primary cache file loaded, but does not contain the correct " +
                "data-structure.  It appears to be of the wrong type.\n" +
                '[' + fName + "]\n" +
                "Please see cause throwable.getCause() for more details.",
                e
            );
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error attempting to read the following primary-cache file.  " + 
                "It may be corrupted: [" + fName + "]\n" +
                "Please see cause throwable.getCause() for more details.",
                e
            );
        }

        // @SuppressWarnings("unchecked")
        return (TreeSet<NUMBER>) ts;
    }


    // Just saves a brand-new (empty) Hash-Code List (a java.util.TreeSet) to disk, using
    // Standard Java Object Serialization.

    private static void writeNewTS(String cacheSaveDirectory)
    {
        // The Generic Type is "not reified", and therefore not written to disk
        final TreeSet<Number> hashCodes = new TreeSet<>();

        try
            { FileRW.writeObjectToFile(hashCodes, cacheSaveDirectory + HASH_SAVE_TREE, true); }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error writing the Cache Hash-Code File to disk.  " +
                "[" + cacheSaveDirectory + HASH_SAVE_TREE + "].  " ,
                e
            );
        }
    }


    // This will save the hash-code {@code TreeSet<Integer>} to disk.
    // It uses standard Java Object-Serialization

    static void persistMasterHashToDisk
        (final TreeSet<?> hashCodes, final String cacheSaveDirectory)
    {
        try
        {
            FileRW.writeObjectToFile
                (hashCodes, cacheSaveDirectory + HASH_SAVE_TREE, true);
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error writing the Master Hash-Code table to disk. " +
                "File [" + cacheSaveDirectory + HASH_SAVE_TREE + "] was not saved. " +
                "The cache-file will have to be refreshed at some point.  New Files " +
                "Cache-Hash not saved.",
                e
            );
        }
    }


    // This will initialize a cache in the file-system directory identified by parameter
    // 'cacheSaveDirectory'.  This method will traverse the File-System Directory-Tree and clobber
    // the contents of all sub-directories, and the files that they contain.
    // 
    // If the directory specified does not exist, a CacheError is thrown.
    // At the end a new Hash-Code => File-Location 

    static String initializeOrClear(String cacheSaveDirectory, StorageWriter sw)
    {
        cacheSaveDirectory = checkCSD(cacheSaveDirectory);

        final String tempStrForStupidLambdaFinal = cacheSaveDirectory;

        try
        {
            File f = new File(cacheSaveDirectory);

            if (f.isDirectory())
                FileTransfer.deleteFilesRecursive(
                    FileNode.createRoot(cacheSaveDirectory).loadTree(), null,
                    (FileNode fn) -> fn.getFullPathName().equals(tempStrForStupidLambdaFinal),
                    sw
                );

            f.mkdirs();
        }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error emptying/clearing the directory " +
                "[" + cacheSaveDirectory + "] of it's contents, please see cause " +
                "throwable.getCause() for details.",
                e
            );
        }

        try
            { writeNewTS(cacheSaveDirectory); }

        catch (Exception e)
        {
            throw new CacheError(
                "There was an error saving/creating the new cache-file " +
                "[" + cacheSaveDirectory + "], please see cause chain throwable.getCause(), " +
                "for more details.",
                e
            );
        }

        // return new HiLiteCache(cacheSaveDirectory);
        return cacheSaveDirectory;
    }


}