Utilise list instead of collection for ListBuilder
This commit is contained in:
parent
c9fceaac0d
commit
49e0be1b37
1 changed files with 14 additions and 14 deletions
|
|
@ -1,30 +1,30 @@
|
||||||
package net.frankheijden.serverutils.utils;
|
package net.frankheijden.serverutils.utils;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.List;
|
||||||
|
|
||||||
public class ListBuilder<T> {
|
public class ListBuilder<T> {
|
||||||
|
|
||||||
private final Collection<T> collection;
|
private final List<T> list;
|
||||||
private ListFormat<T> formatter;
|
private ListFormat<T> formatter;
|
||||||
private String seperator;
|
private String seperator;
|
||||||
private String lastSeperator;
|
private String lastSeperator;
|
||||||
|
|
||||||
private ListBuilder(Collection<T> collection) {
|
private ListBuilder(List<T> list) {
|
||||||
this.collection = collection;
|
this.list = list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> ListBuilder<T> create(Collection<T> collection) {
|
public static <T> ListBuilder<T> create(List<T> list) {
|
||||||
return new ListBuilder<>(collection);
|
return new ListBuilder<>(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a pre-defined ListBuilder with type String.
|
* Creates a pre-defined ListBuilder with type String.
|
||||||
* @param collection The collection to be used.
|
* @param list The collection to be used.
|
||||||
* @return The ListBuilder.
|
* @return The ListBuilder.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static ListBuilder<String> createStrings(Collection<? extends String> collection) {
|
public static ListBuilder<String> createStrings(List<? extends String> list) {
|
||||||
ListBuilder<String> builder = create((Collection<String>) collection);
|
ListBuilder<String> builder = create((List<String>) list);
|
||||||
builder.format(ListFormat.stringFormat);
|
builder.format(ListFormat.stringFormat);
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|
@ -46,17 +46,17 @@ public class ListBuilder<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (collection.size() == 1) {
|
if (list.size() == 1) {
|
||||||
return formatter.format(collection.iterator().next());
|
return formatter.format(list.iterator().next());
|
||||||
} else {
|
} else {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
int i = 1;
|
int i = 1;
|
||||||
for (T t : collection) {
|
for (T t : list) {
|
||||||
sb.append(formatter.format(t));
|
sb.append(formatter.format(t));
|
||||||
if (i == collection.size() - 1) {
|
if (i == list.size() - 1) {
|
||||||
sb.append(lastSeperator);
|
sb.append(lastSeperator);
|
||||||
} else if (i != collection.size()) {
|
} else if (i != list.size()) {
|
||||||
sb.append(seperator);
|
sb.append(seperator);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue