1- /*Copyright (c) 2021 Diego Dominguez Gonzalez
1+ /*Copyright (c) 2022 Diego Dominguez Gonzalez
22 *
33 *Permission is hereby granted, free of charge, to any person obtaining a copy
44 *of this software and associated documentation files (the "Software"), to deal
2525
2626import java .lang .reflect .Constructor ;
2727import java .util .Collection ;
28+ import java .util .Collections ;
29+ import java .util .Comparator ;
2830import java .util .Iterator ;
31+ import java .util .LinkedList ;
32+ import java .util .List ;
2933import java .util .function .BinaryOperator ;
3034import java .util .function .Function ;
3135import java .util .function .Predicate ;
3236
37+ import static java .util .Objects .isNull ;
38+ import static java .util .Objects .nonNull ;
39+
3340/**
3441 * This class consists exclusively of static methods that operate on or return collections.
3542 *
@@ -73,9 +80,9 @@ public static <T> Option<T> flat(Collection<T> collection, BinaryOperator<T> map
7380 Iterator <T > iterator = collection .iterator ();
7481 if (!iterator .hasNext ()) return new Option <T >();
7582 T result = iterator .next ();
76- while (iterator .hasNext () && result != null ) {
83+ while (iterator .hasNext () && nonNull ( result ) ) {
7784 T aux = iterator .next ();
78- result =( aux == null )? null : mapper .apply (result , aux );
85+ result = ( isNull ( aux ) )? null : mapper .apply (result , aux );
7986 }
8087 return new Option <>(result );
8188 }
@@ -103,7 +110,126 @@ public static <T> Collection<T> filter(Collection<T> collection, Predicate<T> co
103110 * @param collection The collection to be filtered.
104111 */
105112 public static <T > Collection <T > filterNotNull (Collection <T > collection ) {
106- return filter (collection ,(A )-> A != null );
113+ return filter (collection ,(A )-> nonNull (A ) );
114+ }
115+
116+ /**
117+ * Returns a new List with the same items of the given Collection.
118+ *
119+ * @param collection The Collection used to generate the new List.
120+ */
121+ public static <T > List <T > toList (Collection <T > collection ) {
122+ Iterator <T > iterator = collection .iterator ();
123+ List <T > list = new LinkedList <>();
124+ while ( iterator .hasNext () ) {
125+ list .add (iterator .next ());
126+ }
127+ return list ;
128+ }
129+
130+ /**
131+ * Returns an Option wrapped the index of the given item inside the given collection. The Option's value will be present if the item is
132+ * actually in the collection, otherwise the optional will be empty.
133+ * @param collection The collection to search in.
134+ * @param item The item to look for.
135+ */
136+ public static <T > Option <Integer > findIndex (Collection <T > collection , T item ) {
137+ int index = toList (collection ).indexOf (item );
138+ if ( index >= 0 ) {
139+ return new Option <>(Integer .valueOf (index ));
140+ }
141+ return new Option <>();
142+ }
143+
144+ /**
145+ * Returns true it there is at least one item in the given collection witch matches the given condition.
146+ * @param collection The given collection.
147+ * @param condition The given collection.
148+ */
149+ public static <T > boolean anyMatch (Collection <T > collection , Predicate <T > condition ) {
150+ if (isEmpty (collection )) {
151+ return false ;
152+ }
153+ Collection <T > result = filter (collection , condition );
154+ return !isEmpty (result );
155+ }
156+
157+ /**
158+ * Returns true if the given collection is not null and is not empty.
159+ *
160+ * @param collection the given collection.
161+ */
162+ public static <T > boolean isEmpty (Collection <T > collection ) {
163+ if (isNull (collection ) || collection .isEmpty ()) {
164+ return true ;
165+ }
166+ return false ;
167+ }
168+
169+ /**
170+ * Return a new collection which the elements of the given collections.
171+ *
172+ * @param a First Collection given.
173+ * @param b Second Collection given.
174+ */
175+ public static <T > Collection <T > concat (Collection <T > a , Collection <T > b ) {
176+ a .addAll (b );
177+ return a ;
178+ }
179+
180+ /**
181+ * Return a new List which the elements of the given Lists.
182+ *
183+ * @param a First List given.
184+ * @param b Second List given.
185+ */
186+ public static <T > List <T > concat (List <T > a , List <T > b ) {
187+ a .addAll (b );
188+ return a ;
189+ }
190+
191+ /**
192+ * Return the first element of the given Collection.
193+ *
194+ * @param collection The given Collection.
195+ */
196+ public static <T > T first (Collection <T > collection ) {
197+ return toList (collection ).get (0 );
198+ }
199+
200+ /**
201+ * Return the last element of the given Collection.
202+ *
203+ * @param collection The given Collection.
204+ */
205+ public static <T > T last (Collection <T > collection ) {
206+ return toList (collection ).get (collection .size () - 1 );
207+ }
208+
209+
210+ /**
211+ * Return true if the collections have the same elements.
212+ *
213+ * @param a First Collection given.
214+ * @param b Second Collection given.
215+ * @param comparator The comparator needed to sort the elements.
216+ */
217+ public static <T > boolean compareCollections (Collection <T > a , Collection <T > b , Comparator <T > comparator ) {
218+ Collection <T > sortedA = sort (a , comparator );
219+ Collection <T > sortedB = sort (a , comparator );
220+ return sortedA .equals (sortedB );
221+ }
222+
223+ /**
224+ * Return a new sorted List from the given collection.
225+ *
226+ * @param collection The Collection given.
227+ * @param comparator The comparator needed to sort the elements.
228+ */
229+ public static <T > List <T > sort (Collection <T > collection , Comparator <T > comparator ) {
230+ List <T > list = toList (collection );
231+ Collections .sort (list ,comparator );
232+ return list ;
107233 }
108234
109235 //private methods
@@ -112,7 +238,7 @@ private static Collection instanceCollectionOf(Class<? extends Collection> tClas
112238 try {
113239 Constructor <? extends Collection > constructor = tClass .getConstructor ();
114240 return constructor .newInstance ();
115- } catch (Exception e ){
241+ } catch (Exception e ) {
116242 throw new RuntimeException (e );
117243 }
118244 }
0 commit comments