From 913812c2d7a560979aaf82e0ff7ceba0390baf96 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Mon, 9 May 2016 11:52:22 +0200 Subject: Refactor Lists utilities --- src/ch/epfl/xblast/Lists.java | 102 +++++++++++++++++++++--------------------- 1 file changed, 51 insertions(+), 51 deletions(-) (limited to 'src') diff --git a/src/ch/epfl/xblast/Lists.java b/src/ch/epfl/xblast/Lists.java index 073e0cc..3794c1e 100644 --- a/src/ch/epfl/xblast/Lists.java +++ b/src/ch/epfl/xblast/Lists.java @@ -36,33 +36,27 @@ public final class Lists { } /** - * Returns a symmetric version of the list, without repeating the last element of the input list. - * For instance, mirrored([kay]) will return [kayak]. + * Returns a copy of the given list with element e prepended. * - * @param l the input list + * @param l the list + * @param e the element to prepend * @param the type of the list's elements - * @return the mirrored list - * @throws IllegalArgumentException if the given list is empty + * @return a copy of the list with the element prepended */ - public static List mirrored(List l) { - if (l == null || l.size() == 0) throw new IllegalArgumentException(); - - return Stream - .concat(l.stream(), Lists.reversed(l).stream().skip(1)) - .collect(Collectors.toList()); + public static List prepended(List l, T e) { + return Lists.inserted(l, 0, e); } /** - * Returns a reversed copy of the given list, leaving the original one unmodified. + * Returns a copy of the given list with element e appended. * - * @param l the list to reverse + * @param l the list + * @param e the element to append * @param the type of the list's elements - * @return a reversed copy of the list + * @return a copy of the list with the element appended */ - private static List reversed(List l) { - List r = new ArrayList<>(l); - Collections.reverse(r); - return r; + public static List appended(List l, T e) { + return Lists.inserted(l, l.size(), e); } /** @@ -74,46 +68,24 @@ public final class Lists { * @return a copy of the list with the element inserted at start and end */ public static List surrounded(List l, T e) { - return Lists.inserted(Lists.inserted(l, 0, e), l.size() + 1, e); - } - - /** - * Returns a copy of the given list with element e inserted at index i. - * - * @param l the list - * @param i the insertion index - * @param e the element to insert - * @param the type of the list's elements - * @return a copy of the list with the element inserted - */ - public static List inserted(List l, int i, T e) { - List r = new LinkedList<>(l); - r.add(i, e); - return r; + return Lists.appended(Lists.prepended(l, e), e); } /** - * Returns a copy of the given list with element e prepended. + * Returns a symmetric version of the list, without repeating the last element of the input list. + * For instance, mirrored([kay]) will return [kayak]. * - * @param l the list - * @param e the element to prepend + * @param l the input list * @param the type of the list's elements - * @return a copy of the list with the element prepended + * @return the mirrored list + * @throws IllegalArgumentException if the given list is empty */ - public static List prepended(List l, T e) { - return Lists.inserted(l, 0, e); - } + public static List mirrored(List l) { + if (l == null || l.size() == 0) throw new IllegalArgumentException(); - /** - * Returns a copy of the given list with element e appended. - * - * @param l the list - * @param e the element to append - * @param the type of the list's elements - * @return a copy of the list with the element appended - */ - public static List appended(List l, T e) { - return Lists.inserted(l, l.size(), e); + return Stream + .concat(l.stream(), Lists.reversed(l).stream().skip(1)) + .collect(Collectors.toList()); } /** @@ -167,4 +139,32 @@ public final class Lists { .collect(Collectors.toList())); } + /** + * Returns a reversed copy of the given list, leaving the original one unmodified. + * + * @param l the list to reverse + * @param the type of the list's elements + * @return a reversed copy of the list + */ + private static List reversed(List l) { + List r = new ArrayList<>(l); + Collections.reverse(r); + return r; + } + + /** + * Returns a copy of the given list with element e inserted at index i. + * + * @param l the list + * @param i the insertion index + * @param e the element to insert + * @param the type of the list's elements + * @return a copy of the list with the element inserted + */ + private static List inserted(List l, int i, T e) { + List r = new LinkedList<>(l); + r.add(i, e); + return r; + } + } -- cgit v1.2.3