BetterLists 1.1 clean code with sonarlint

This commit is contained in:
Klemek
2018-03-22 20:26:08 +01:00
parent b4bd402486
commit 10ea328b05
8 changed files with 44 additions and 42 deletions
+24 -3
View File
@@ -4,7 +4,7 @@ An extension of the java.util.List interface which include some of the C# LINQ u
List classes are extended as well. (ArrayList -> BetterArrayList) List classes are extended as well. (ArrayList -> BetterArrayList)
Current version v1.0 Current version v1.1
Before BetterLists : Before BetterLists :
```Java ```Java
@@ -30,8 +30,29 @@ NOTE : Please note that, unlike C# LINQ, these functions are not optimized at lo
## Download ## Download
* [betterlists-1.0.jar](../../raw/master/download/betterlists-1.0.jar) * [betterlists-1.1.jar](../../raw/master/download/betterlists-1.0.jar)
* [betterlists-1.0-sources.jar](../../raw/master/download/betterlists-1.0-sources.jar)
## Maven
You can use this project as a maven dependency with this :
```XML
<repositories>
...
<repository>
<id>klemek.betterlists</id>
<url>https://github.com/klemek/betterlists/raw/mvn-repo</url>
</repository>
</repositories>
...
<dependencies>
...
<dependency>
<groupId>klemek</groupId>
<artifactId>betterlists</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
```
## All code examples ## All code examples
### List ### List
Binary file not shown.
Binary file not shown.
@@ -2,7 +2,6 @@ package fr.klemek.betterlists;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List;
/** /**
* An extension of the java.util.ArrayList class which include some of the C# * An extension of the java.util.ArrayList class which include some of the C#
@@ -24,7 +23,7 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
* - the collection whose elements are to be placed into this list * - the collection whose elements are to be placed into this list
*/ */
public static <T> BetterArrayList<T> fromList(Collection<T> c) { public static <T> BetterArrayList<T> fromList(Collection<T> c) {
return new BetterArrayList<T>(c); return new BetterArrayList<>(c);
} }
/** /**
@@ -78,6 +77,6 @@ public class BetterArrayList<T> extends ArrayList<T> implements BetterList<T> {
*/ */
@Override @Override
public BetterArrayList<T> subList(int fromIndex, int toIndex) { public BetterArrayList<T> subList(int fromIndex, int toIndex) {
return (BetterArrayList<T>) ((List<T>) this).subList(fromIndex, toIndex); return (BetterArrayList<T>) this.subList(fromIndex, toIndex);
} }
} }
@@ -2,7 +2,6 @@ package fr.klemek.betterlists;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
/** /**
* An extension of the java.util.LinkedList class which include some of the C# * An extension of the java.util.LinkedList class which include some of the C#
@@ -24,7 +23,7 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
* - the collection whose elements are to be placed into this list * - the collection whose elements are to be placed into this list
*/ */
public static <T> BetterLinkedList<T> fromList(Collection<T> c) { public static <T> BetterLinkedList<T> fromList(Collection<T> c) {
return new BetterLinkedList<T>(c); return new BetterLinkedList<>(c);
} }
/** /**
@@ -68,7 +67,7 @@ public class BetterLinkedList<T> extends LinkedList<T> implements BetterList<T>
*/ */
@Override @Override
public BetterLinkedList<T> subList(int fromIndex, int toIndex) { public BetterLinkedList<T> subList(int fromIndex, int toIndex) {
return (BetterLinkedList<T>) ((List<T>) this).subList(fromIndex, toIndex); return (BetterLinkedList<T>) this.subList(fromIndex, toIndex);
} }
} }
+13 -28
View File
@@ -1,7 +1,6 @@
package fr.klemek.betterlists; package fr.klemek.betterlists;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.function.Function; import java.util.function.Function;
@@ -82,7 +81,7 @@ public interface BetterList<T> extends List<T> {
* in the other. * in the other.
*/ */
public default BetterList<T> exclusion(List<T> other) { public default BetterList<T> exclusion(List<T> other) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
for (T element : this) for (T element : this)
if (!other.contains(element)) if (!other.contains(element))
out.add(element); out.add(element);
@@ -274,16 +273,9 @@ public interface BetterList<T> extends List<T> {
* @return a List whose elements are sorted according to a key. * @return a List whose elements are sorted according to a key.
*/ */
public default <E extends Comparable<E>> BetterList<T> orderBy(Function<T, E> selector){ public default <E extends Comparable<E>> BetterList<T> orderBy(Function<T, E> selector){
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
out.addAll(this); out.addAll(this);
Collections.sort(out, new Comparator<T>() { Collections.sort(out, (o1,o2) -> selector.apply(o1).compareTo(selector.apply(o2)));
@Override
public int compare(T o1, T o2) {
return selector.apply(o1).compareTo(selector.apply(o2));
}
});
return out; return out;
} }
@@ -294,16 +286,9 @@ public interface BetterList<T> extends List<T> {
* @return a List whose elements are sorted according to a key. * @return a List whose elements are sorted according to a key.
*/ */
public default <E extends Comparable<E>> BetterList<T> orderByDescending(Function<T, E> selector){ public default <E extends Comparable<E>> BetterList<T> orderByDescending(Function<T, E> selector){
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
out.addAll(this); out.addAll(this);
Collections.sort(out, new Comparator<T>() { Collections.sort(out, (o1,o2) -> selector.apply(o2).compareTo(selector.apply(o1)));
@Override
public int compare(T o1, T o2) {
return selector.apply(o2).compareTo(selector.apply(o1));
}
});
return out; return out;
} }
@@ -314,7 +299,7 @@ public interface BetterList<T> extends List<T> {
* reverse order. * reverse order.
*/ */
public default BetterList<T> reverse() { public default BetterList<T> reverse() {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>(this.size());
for (T element : this) for (T element : this)
out.add(0, element); out.add(0, element);
return out; return out;
@@ -331,7 +316,7 @@ public interface BetterList<T> extends List<T> {
* function on each element of the sequence. * function on each element of the sequence.
*/ */
public default <E> BetterList<E> select(Function<T, E> selector) { public default <E> BetterList<E> select(Function<T, E> selector) {
BetterList<E> out = new BetterArrayList<E>(); BetterList<E> out = new BetterArrayList<>();
for (T element : this) for (T element : this)
out.add(selector.apply(element)); out.add(selector.apply(element));
return out; return out;
@@ -348,7 +333,7 @@ public interface BetterList<T> extends List<T> {
* index in the sequence. * index in the sequence.
*/ */
public default BetterList<T> skip(int count) { public default BetterList<T> skip(int count) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
int n = 0; int n = 0;
for (T element : this) { for (T element : this) {
if (n >= count) if (n >= count)
@@ -369,7 +354,7 @@ public interface BetterList<T> extends List<T> {
* specified by predicate. * specified by predicate.
*/ */
public default BetterList<T> skipWhile(Function<T, Boolean> predicate) { public default BetterList<T> skipWhile(Function<T, Boolean> predicate) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
boolean match = true; boolean match = true;
for (T element : this) for (T element : this)
if (!match || !predicate.apply(element)) { if (!match || !predicate.apply(element)) {
@@ -405,7 +390,7 @@ public interface BetterList<T> extends List<T> {
* of the input sequence. * of the input sequence.
*/ */
public default BetterList<T> take(int count) { public default BetterList<T> take(int count) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>(count);
int n = 0; int n = 0;
for (T element : this) { for (T element : this) {
if (n < count) if (n < count)
@@ -426,7 +411,7 @@ public interface BetterList<T> extends List<T> {
* the element at which the test no longer passes. * the element at which the test no longer passes.
*/ */
public default BetterList<T> takeWhile(Function<T, Boolean> predicate) { public default BetterList<T> takeWhile(Function<T, Boolean> predicate) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
for (T element : this) for (T element : this)
if (predicate.apply(element)) if (predicate.apply(element))
out.add(element); out.add(element);
@@ -445,7 +430,7 @@ public interface BetterList<T> extends List<T> {
* duplicates. * duplicates.
*/ */
public default BetterList<T> union(List<T> other) { public default BetterList<T> union(List<T> other) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
for (T element : this) for (T element : this)
if (other.contains(element)) if (other.contains(element))
out.add(element); out.add(element);
@@ -461,7 +446,7 @@ public interface BetterList<T> extends List<T> {
* condition. * condition.
*/ */
public default BetterList<T> where(Function<T, Boolean> predicate) { public default BetterList<T> where(Function<T, Boolean> predicate) {
BetterList<T> out = new BetterArrayList<T>(); BetterList<T> out = new BetterArrayList<>();
for (T element : this) for (T element : this)
if (predicate.apply(element)) if (predicate.apply(element))
out.add(element); out.add(element);
+1 -2
View File
@@ -1,6 +1,5 @@
package fr.klemek.betterlists; package fr.klemek.betterlists;
import java.util.List;
import java.util.Stack; import java.util.Stack;
/** /**
@@ -45,7 +44,7 @@ public class BetterStack<T> extends Stack<T> implements BetterList<T> {
*/ */
@Override @Override
public BetterStack<T> subList(int fromIndex, int toIndex) { public BetterStack<T> subList(int fromIndex, int toIndex) {
return (BetterStack<T>) ((List<T>) this).subList(fromIndex, toIndex); return (BetterStack<T>) this.subList(fromIndex, toIndex);
} }
} }
+2 -3
View File
@@ -1,7 +1,6 @@
package fr.klemek.betterlists; package fr.klemek.betterlists;
import java.util.Collection; import java.util.Collection;
import java.util.List;
import java.util.Vector; import java.util.Vector;
/** /**
@@ -24,7 +23,7 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
* - the collection whose elements are to be placed into this vector * - the collection whose elements are to be placed into this vector
*/ */
public static <T> BetterVector<T> fromList(Collection<T> c) { public static <T> BetterVector<T> fromList(Collection<T> c) {
return new BetterVector<T>(c); return new BetterVector<>(c);
} }
/** /**
@@ -94,7 +93,7 @@ public class BetterVector<T> extends Vector<T> implements BetterList<T> {
*/ */
@Override @Override
public BetterVector<T> subList(int fromIndex, int toIndex) { public BetterVector<T> subList(int fromIndex, int toIndex) {
return (BetterVector<T>) ((List<T>) this).subList(fromIndex, toIndex); return (BetterVector<T>) this.subList(fromIndex, toIndex);
} }
} }