Skip to content

Commit

Permalink
Improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
khmarbaise committed Jul 22, 2023
1 parent b75bb18 commit ab3da75
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>equalsverifier</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>com.github.marschall</groupId>
<artifactId>memoryfilesystem</artifactId>
Expand All @@ -49,6 +54,11 @@
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.marschall</groupId>
<artifactId>memoryfilesystem</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ public int hashCode() {

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.byteArray().length; i++) {
sb.append(String.format("%02x", this.byteArray()[i]));
}
return "ByteArrayWrapper{" +
"byteArray=" + Arrays.toString(byteArray) +
"byteArray=" + sb +
'}';
}

Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/soebes/module/calculator/ModuleCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.soebes.module.calculator.FileSelector.selectAllFiles;
import static com.soebes.module.calculator.FileSelector.toChecksumForFile;

public final class ModuleCalculator {

private final Logger LOGGER = LoggerFactory.getLogger( getClass() );

public ModuleCalculator() {
}

public boolean hashChanged(Path path, Path hashFile) {
LOGGER.debug("Starting: {} hashFile: {}", path, hashFile);
try {
ChecksumForFileResult hashFromFile = ChecksumForFileResult.NON_EXISTENT;
if (Files.exists(hashFile) && Files.isRegularFile(hashFile) && Files.isReadable(hashFile)) {
Expand All @@ -26,7 +31,9 @@ public boolean hashChanged(Path path, Path hashFile) {
Files.createDirectories(hashFile.getParent());
Files.write(hashFile, calculatedHash.getDigest().byteArray());

return !calculatedHash.equals(hashFromFile);
boolean changed = !calculatedHash.equals(hashFromFile);
LOGGER.debug("changed: {} hashFromFile: {} calculatedHash: {}", changed, hashFromFile.getDigest(), calculatedHash.getDigest());
return changed;

} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -38,10 +45,13 @@ public boolean hashChanged(Path path, Path hashFile) {
* var defaultExcludes = List.of("target", ".git", ".github", ".idea");
*/
public ChecksumForFileResult calculateHashForDirectoryTree(Path path) throws IOException {
LOGGER.debug("Starting: {}", path);
List<Path> paths = selectAllFiles(path);
return paths
.parallelStream()
.peek(f -> LOGGER.debug("File:{}", f))
.map(toChecksumForFile)
.peek(s -> LOGGER.debug("{}", s))
.reduce(ChecksumForFileResult.NULL, ChecksumForFileResult::accept);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,20 @@ class ByteArrayWrapperTest {
void verify_hashcode_and_equals() {
EqualsVerifier.forClass(ByteArrayWrapper.class).suppress(Warning.NULL_FIELDS).verify();
}

static final ByteArrayWrapper NULL = new ByteArrayWrapper(new byte[]{
0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
});

@Test
void name() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < NULL.byteArray().length; i++) {
sb.append(String.format("%02x", NULL.byteArray()[i]));
}
System.out.println("sb = " + sb);
}
}

0 comments on commit ab3da75

Please sign in to comment.