55package net .minecraftforge .util .hash ;
66
77import net .minecraftforge .util .logging .Log ;
8+ import org .jetbrains .annotations .NotNullByDefault ;
9+ import org .jetbrains .annotations .Nullable ;
810
911import java .io .File ;
1012import java .io .IOException ;
1416import java .util .Collections ;
1517import java .util .HashMap ;
1618import java .util .Map ;
19+ import java .util .Objects ;
1720
1821import static net .minecraftforge .util .hash .HashUtils .sneak ;
1922
23+ @ NotNullByDefault
2024public class HashStore {
21- private final HashFunction HASH = HashFunction .SHA1 ;
25+ private static final HashFunction HASH = HashFunction .SHA1 ;
2226
2327 private final String root ;
2428 private final Map <String , String > oldHashes = new HashMap <>();
2529 private final Map <String , String > newHashes = new HashMap <>();
26- private File target ;
30+ private @ Nullable File target ;
2731
2832 public static HashStore fromFile (File path ) {
2933 File parent = path .getAbsoluteFile ().getParentFile ();
@@ -37,11 +41,15 @@ public static HashStore fromDir(File path) {
3741 }
3842
3943 public HashStore () {
40- this . root = "" ;
44+ this ( "" ) ;
4145 }
4246
4347 public HashStore (File root ) {
44- this .root = root .getAbsolutePath ();
48+ this (root .getAbsolutePath ());
49+ }
50+
51+ private HashStore (String root ) {
52+ this .root = root ;
4553 }
4654
4755 public boolean areSame (File ... files ) {
@@ -103,16 +111,20 @@ public boolean exists() {
103111 }
104112
105113 public HashStore add (String key , String data ) {
106- newHashes .put (key , HASH .hash (data ));
114+ if (!data .isEmpty ())
115+ newHashes .put (Objects .requireNonNull (key ), HASH .hash (data ));
107116 return this ;
108117 }
109118
110119 public HashStore add (String key , byte [] data ) {
111- newHashes .put (key , HASH .hash (data ));
120+ if (data .length > 0 )
121+ this .newHashes .put (Objects .requireNonNull (key ), HASH .hash (data ));
112122 return this ;
113123 }
114124
115- public HashStore add (String key , File file ) {
125+ public HashStore add (@ Nullable String key , File file ) {
126+ if (!file .exists ()) return this ;
127+
116128 try {
117129 if (key == null )
118130 key = getPath (file );
@@ -121,10 +133,10 @@ public HashStore add(String key, File file) {
121133 String prefix = getPath (file );
122134 for (File f : HashUtils .listFiles (file )) {
123135 String suffix = getPath (f ).substring (prefix .length ());
124- newHashes .put (key + " - " + suffix , HASH .hash (f ));
136+ this . newHashes .put (key + " - " + suffix , HASH .hash (f ));
125137 }
126138 } else {
127- newHashes .put (key , HASH .hash (file ));
139+ this . newHashes .put (key , HASH .hash (file ));
128140 }
129141 } catch (IOException e ) {
130142 throw new RuntimeException (e );
@@ -150,22 +162,27 @@ public HashStore add(File file) {
150162 }
151163
152164 public boolean isSame () {
153- return oldHashes .equals (newHashes );
165+ return this .oldHashes .equals (this .newHashes );
166+ }
167+
168+ /** Clears the new hashes, does not clear the old hashes read from {@link #load(File)}. */
169+ public void clear () {
170+ this .newHashes .clear ();
154171 }
155172
156173 public void save () {
157- if (target == null )
174+ if (this . target == null )
158175 throw new RuntimeException ("HashStore.save() called without load(File) so we dont know where to save it! Use load(File) or save(File)" );
159- save (target );
176+ save (this . target );
160177 }
161178
162179 public void save (File file ) {
163180 StringBuilder buf = new StringBuilder ();
164- ArrayList <String > keys = new ArrayList <String >( newHashes .keySet ());
181+ ArrayList <String > keys = new ArrayList <>( this . newHashes .keySet ());
165182 Collections .sort (keys );
166183
167184 for (String key : keys )
168- buf .append (key ).append ('=' ).append (newHashes .get (key )).append ('\n' );
185+ buf .append (key ).append ('=' ).append (this . newHashes .get (key )).append ('\n' );
169186
170187 try {
171188 Files .write (file .toPath (), buf .toString ().getBytes (StandardCharsets .UTF_8 ));
@@ -177,8 +194,8 @@ public void save(File file) {
177194 private String getPath (File file ) {
178195 String path = file .getAbsolutePath ();
179196
180- if (path .startsWith (root ))
181- path = path .substring (root .length ());
197+ if (path .startsWith (this . root ))
198+ path = path .substring (this . root .length ());
182199
183200 path = path .replace ('\\' , '/' );
184201
0 commit comments