From b2fe2f45579f47f7871b6de33c6c4eb5b7dbd5e1 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sun, 6 Jan 2019 12:33:19 +0000 Subject: [PATCH] Clean up (#64) * [readme] use valid haskell example * [tree] Immediately return this expression instead of assigning it to the temporary variable "treeList". * [tree] Use normal defensive copying for array fields --- README.org | 9 +++++---- src/main/java/net/kemitix/mon/tree/GeneralisedTree.java | 4 ++-- src/main/java/net/kemitix/mon/tree/MutableTree.java | 7 +------ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/README.org b/README.org index f5756b5..94e336d 100644 --- a/README.org +++ b/README.org @@ -34,11 +34,12 @@ if you try and use them together. e.g.: #+BEGIN_SRC haskell - type PhoneNumber = String - type Name = String - type PhoneBook = [(Name,PhoneNumber)] + newtype PhoneNumber = PhoneNumber String + newtype Name = Name String + newtype PhoneBookEntry = PhoneBookEntry (Name, PhoneNumber) + newtype PhoneBook = PhoneBook [PhoneBookEntry] #+END_SRC - + In Java we don't have the ability to have that true alias, so TypeAlias is more of a type-wrapper. It's as close as I could get to a Haskell type alias in Java. diff --git a/src/main/java/net/kemitix/mon/tree/GeneralisedTree.java b/src/main/java/net/kemitix/mon/tree/GeneralisedTree.java index 5c4ef78..d0cb9cb 100644 --- a/src/main/java/net/kemitix/mon/tree/GeneralisedTree.java +++ b/src/main/java/net/kemitix/mon/tree/GeneralisedTree.java @@ -50,7 +50,7 @@ class GeneralisedTree implements Tree, TreeMapper { */ GeneralisedTree(final T item, final Collection> subTrees) { this.item = item; - this.subTrees = Collections.unmodifiableList(new ArrayList<>(subTrees)); + this.subTrees = new ArrayList<>(subTrees); } /** @@ -77,6 +77,6 @@ class GeneralisedTree implements Tree, TreeMapper { */ @Override public List> subTrees() { - return subTrees; + return new ArrayList<>(subTrees); } } diff --git a/src/main/java/net/kemitix/mon/tree/MutableTree.java b/src/main/java/net/kemitix/mon/tree/MutableTree.java index 4674e14..e4be977 100644 --- a/src/main/java/net/kemitix/mon/tree/MutableTree.java +++ b/src/main/java/net/kemitix/mon/tree/MutableTree.java @@ -29,7 +29,6 @@ import java.util.Collection; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; -import java.util.stream.Stream; /** * A mutable {@link Tree}. @@ -126,12 +125,8 @@ class MutableTree implements Tree, TreeMapper { } @Override - @SuppressWarnings("unchecked") public List> subTrees() { - final Stream> mutableTreeStream = mySubTrees.stream(); - final Stream> treeStream = mutableTreeStream.map(Tree.class::cast); - final List> treeList = treeStream.collect(Collectors.toList()); - return treeList; + return new ArrayList<>(mySubTrees); } /**