# packman1783/difference-calculator architecture

> Architecture diagram of the GitHub repository packman1783/difference-calculator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/packman1783/difference-calculator
- Repository: https://github.com/packman1783/difference-calculator

## Overview

##### 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)

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "CLI Layer"
        App["App"]:::cli
        OutputWriter["OutputWriter"]:::cli
    end

    subgraph "I/O Layer"
        FileReader["FileReader"]:::io
    end

    subgraph "Parsing Layer"
        ParserFactory["ParserFactory"]:::parsing
        Parser["Parser"]:::parsing
        JsonParser["JsonParser"]:::parsing
        YmlParser["YmlParser"]:::parsing
        FileType["FileType"]:::parsing
    end

    subgraph "Comparison Layer"
        Differ["Differ"]:::comp
        CompareUtil["CompareUtil"]:::comp
    end

    subgraph "Formatting Layer"
        Formatter["Formatter"]:::format
        Stylish["StylishFormatter"]:::format
        Plain["PlainFormatter"]:::format
        JsonFmt["JsonFormatter"]:::format
    end

    subgraph "Build & CI"
        Gradle["Gradle (Gradle Wrapper)"]:::ci
        Checkstyle["Checkstyle"]:::ci
        Actions["GitHub Actions"]:::ci
    end

    App -->|"read files"| FileReader
    FileReader -->|"getParser()"| ParserFactory
    ParserFactory -->|"uses FileType"| FileType
    ParserFactory -->|"create JsonParser"| JsonParser
    ParserFactory -->|"create YmlParser"| YmlParser
    JsonParser -->|"parse() trees"| Differ
    YmlParser -->|"parse() trees"| Differ
    Differ -->|"compareNodes()"| CompareUtil
    App -->|"format(diffTree)"| Formatter
    Formatter -->|"toStylish()"| Stylish
    Formatter -->|"toPlain()"| Plain
    Formatter -->|"toJson()"| JsonFmt
    Formatter -->|"write()"| OutputWriter

    classDef cli fill:#f0f8ff,stroke:#333,stroke-width:1px
    classDef io fill:#e0e0e0,stroke:#333,stroke-width:1px
    classDef parsing fill:#d0ffd0,stroke:#333,stroke-width:1px
    classDef comp fill:#d0e0ff,stroke:#333,stroke-width:1px
    classDef format fill:#ffe0b3,stroke:#333,stroke-width:1px
    classDef ci fill:#f5f5f5,stroke:#333,stroke-width:1px

    click App "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/App.java"
    click ParserFactory "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/factory/ParserFactory.java"
    click Parser "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/factory/Parser.java"
    click JsonParser "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/factory/JsonParser.java"
    click YmlParser "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/factory/YmlParser.java"
    click FileType "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/factory/FileType.java"
    click Differ "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/Differ.java"
    click CompareUtil "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/CompareUtil.java"
    click Formatter "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/formatters/Formatter.java"
    click Stylish "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/formatters/Stylish.java"
    click Plain "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/formatters/Plain.java"
    click JsonFmt "https://github.com/packman1783/difference-calculator/blob/main/app/src/main/java/hexlet/code/formatters/Json.java"
    click Gradle "https://github.com/packman1783/difference-calculator/blob/main/app/build.gradle"
    click Checkstyle "https://github.com/packman1783/difference-calculator/blob/main/app/config/checkstyle/checkstyle.xml"
    click Actions "https://github.com/packman1783/difference-calculator/tree/main/.github/workflows/"
```
