CSE Roadmap

Core tier · about 4 hours · 6 parts

Part 3 of 6 · 1 min read

Classes

One base type for files and folders.

Class Job in one sentence
Node (abstract) Name, parent and a modified counter, with size() and isDir()
File Holds text. Its size is the text length
Directory Holds a sorted map from name to node. Its size is the sum of its children
FileSystem Owns the root, parses paths, and offers the operations
FsException A checked error with a clear message

This is the composite pattern. A caller can ask any node for its size and does not care whether it is one file or a whole tree. A directory answers by asking each child, and the children may be directories too. The recursion ends at files.

The children are in a TreeMap, so listings come out in name order and tests give the same result each time. Each node knows its parent. That makes a node's full path easy to rebuild, and makes the "move into itself" check a simple walk up.

Why a checked exception? A bad path is a normal event for a caller, such as a mistyped name, and the caller can respond. This follows the rule of Level 5, module 1: checked for expected failures that callers can handle.

Check yourself: 1. Which pattern lets size work on files and folders alike? 2. Why does each node keep its parent? 3. Why use a sorted map for children?