How packman1783/difference-calculator works
Project Type and Purpose
• This is a stand-alone CLI utility written in Java.
• It reads two input files (JSON or YAML), computes a structured difference, and writes the result in one of several output formats (stylish, plain, JSON).
High-Level Structure from File Tree
• Single Gradle module under app/ with standard Java layout.
• src/main/java/hexlet/code/ contains:
• App.java (entry point, CLI argument parsing)
• factory package (ParserFactory, FileType enum, Parser interface, JsonParser, YmlParser)
• Differ.java and CompareUtil.java (core diff engine)
• formatters package (Formatter interface and implementations: Stylish, Plain, Json)
• src/test covers unit tests for differ and parsers.
• Build and CI config: Gradle wrapper, Makefiles, Checkstyle, GitHub Actions workflows.
Key README Insights
• Usage examples show how App launches, selects parser based on extension, invokes differ, then formatter by --format flag.
• No external services or databases—pure file-to-stdout processing.
Components
a. CLI Layer
• App (CLI argument processor)
• OutputWriter (stdout)
b. I/O Layer
• FileReader (reads raw file content)
c. Parsing Layer (Factory Pattern)
• ParserFactory
• JsonParser
• YmlParser
d. Comparison Layer
• Differ (compares two parsed trees)
• CompareUtil (helper for node‐by‐node comparison)
e. Formatting Layer (Strategy Pattern)
• Formatter interface
• StylishFormatter
• PlainFormatter
• JsonFormatter
f. Build & CI (optional overlay)
• Gradle, Checkstyle, GitHub Actions
Interactions and Data Flow
1. User invokes: ./app [--format <fmt>] file1 file2
2. App parses CLI args → passes paths + format flag
3. For each file:
• FileReader loads content → ParserFactory selects JsonParser or YmlParser → produces a structured tree (e.g., Map or AST)
4. Differ consumes both trees → builds a DiffTree structure
5. Based on format flag, App selects appropriate Formatter → processes DiffTree → returns formatted String
6. OutputWriter writes the String to stdout
Architectural Patterns & Principles
• Factory Pattern for selecting the correct parser based on file type
• Strategy Pattern for interchangeable formatters
• Single Responsibility: each component (parsing, diffing, formatting, I/O) is isolated
• Dependency Injection can be illustrated (App injecting ParserFactory and Formatter)