Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ch.claude_martin.foo;
- import java.util.*;
- public class SomeClass implements Comparable<SomeClass> {
- private static Comparator<SomeClass> COMPARATOR = Comparator.comparing(SomeClass::getCode).thenComparing(SomeClass::getName);
- private final String name, code;
- private final NavigableSet<SomeClass> children = new TreeSet<SomeClass>();
- private final SomeClass parent;
- // doesn't have to be public. only factory needs access.
- SomeClass(SomeClass parent, String name, String code) {
- this.parent = parent;
- this.name = name;
- this.code = code;
- }
- // Creates a root
- SomeClass(String name, String code) {
- this(null, name, code);
- }
- // doesn't have to be public. only factory needs access.
- void addChild(SomeClass child) {
- this.children.add(child);
- }
- public NavigableSet<SomeClass> getChildren() {
- return new TreeSet<>(this.children); // return defensive copy
- }
- public Optional<SomeClass> getParent() {
- return Optional.ofNullable(this.parent);
- }
- public String getName() {
- return name;
- }
- public String getCode() {
- return code;
- }
- @Override
- public int compareTo(SomeClass o) {
- return COMPARATOR.compare(this, o);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement