
net.sf.okapi.common.ListUtil Maven / Gradle / Ivy
/*===========================================================================
Copyright (C) 2009-2017 by the Okapi Framework contributors
-----------------------------------------------------------------------------
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
===========================================================================*/
package net.sf.okapi.common;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Helper methods to manipulate lists.
*/
public class ListUtil {
/**
* Splits up a string of comma-separated substrings into a string list of those substrings.
* @param st string of comma-separated substrings.
* @return a list of substrings.
*/
public static List stringAsList(String st) {
return listTrimValues(stringAsList(st, ","));
}
// /**
// * Converts an array of string representing locales into a list of locales.
// * @param array the array of strings to convert.
// * @return a list of locales for the given strings.
// */
// public static List stringArrayAsLanguageList (String[] array) {
// List list = new ArrayList();
// for ( int i=0; i stringAsLanguageList (String input) {
if ( input == null ) return null;
List res = new ArrayList<>();
List list = new ArrayList<>();
stringAsList(list, input, ",");
for ( String lang : list ) {
lang = lang.trim();
if ( !Util.isEmpty(lang) ) {
res.add(LocaleId.fromString(lang));
}
}
return res;
}
// /**
// * Converts a list of languages into an array of strings.
// * @param list List of languages.
// * @return an array of strings for the given languages.
// */
// public static String[] languageListAsStringArray (List list) {
// String[] res = new String[list.size()];
// for ( int i=0; i list) {
// if ( list == null ) return "";
// StringBuilder tmp = new StringBuilder();
// for ( int i=0; i 0 ) {
// tmp.append(",");
// }
// tmp.append(list.get(i).toString());
// }
// return tmp.toString();
// }
/**
* Splits up a string of comma-separated substrings into a string list of those substrings.
* @param list a list to put the substrings.
* @param st string of comma-separated substrings.
*/
public static void stringAsList(List list, String st) {
stringAsList(list, st, ",");
listTrimValues(list);
}
/**
* Splits up a string of delimited substrings into a string list of those substrings.
* @param st string of delimited substrings.
* @param delimiter a string delimiting substrings in the string.
* @return a list of substrings.
*/
public static List stringAsList(String st, String delimiter) {
ArrayList res = new ArrayList<>();
stringAsList(res, st, delimiter);
return res;
}
/**
* Splits up a string of delimited substrings into a string list of those substrings.
* @param st string of delimited substrings.
* @param delimiter a character delimiting substrings in the string.
* @return a list of substrings.
*/
public static List stringAsList(String st, char delimiter) {
ArrayList res = new ArrayList<>();
stringAsList(res, st, Character.toString(delimiter));
return res;
}
/**
* Splits up a string of delimited substrings into a string list of those substrings.
* @param list a list to put the substrings.
* @param st string of delimited substrings.
* @param delimiter a string delimiting substrings in the string.
*/
public static void stringAsList(List list, String st, String delimiter) {
if (Util.isEmpty(st)) return;
if (list == null) return;
list.clear();
if (Util.isEmpty(delimiter)) {
list.add(st);
return;
}
int start = 0;
int len = delimiter.length();
while (true) {
int index = st.substring(start).indexOf(delimiter);
if (index == -1) break;
list.add(st.substring(start, start + index));
start += index + len;
}
if (start <= st.length())
list.add(st.substring(start));
}
/**
* Splits up a string of comma-separated substrings into an array of those substrings.
* @param st string of comma-separated substrings.
* @return the generated array of strings.
*/
public static String[] stringAsArray(String st) {
List list = stringAsList(st);
if (Util.isEmpty(list))
return new String[] {};
return list.toArray(new String[] {});
}
/**
* Splits up a string of comma-separated substrings into an array of those substrings.
* @param st string of comma-separated substrings.
* @param delimiter a string delimiting substrings in the string.
* @return the generated array of strings.
*/
public static String[] stringAsArray(String st, String delimiter) {
List list = stringAsList(st, delimiter);
if (Util.isEmpty(list))
return new String[] {};
return list.toArray(new String[] {});
}
/**
* Merges specified elements of a given string array into a single string. The merged elements are joined with a given joiner.
* @param array the given array of strings.
* @param start index of the start element to be merged.
* @param end index of the end element (inclusive) to be merged.
* @param joiner string to join elements in the resulting string.
* @return the string of merged elements.
*/
public static String merge(String[] array, int start, int end, String joiner) {
//return merge(Arrays.asList(array), start, end, joiner);
if (!Util.checkIndex(start, array) && !Util.checkIndex(end, array))
return "";
if (start < 0 && Util.checkIndex(end, array))
start = 0;
if (Util.checkIndex(start, array) && end >= array.length)
end = array.length - 1;
if (start >= end) return "";
StringBuilder tmp = new StringBuilder(array[start]);
for (int i = start + 1; i < end + 1; i++) {
tmp.append(joiner);
tmp.append(array[i]);
}
return tmp.toString();
}
/**
* Merges specified items of a given string list into a single string. The merged items are joined with a given joiner.
* @param list the given list of strings.
* @param start index of the start item to be merged.
* @param end index of the end item (inclusive) to be merged.
* @param joiner string to join items in the resulting string.
* @return the string of merged items.
*/
public static String merge(List list, int start, int end, String joiner) {
return merge(stringListAsArray(list), start, end, joiner);
}
/**
* Converts a string of comma-separated numbers into a list of integers.
* @param st string of comma-separated numbers.
* @return a list of integers.
*/
public static List stringAsIntList (String st) {
return stringAsIntList(st, ",");
}
/**
* Converts a string of comma-separated numbers into a list of integers and sorts the list ascendantly.
* @param st string of comma-separated numbers
* @param delimiter a string delimiting numbers in the string
* @return a list of integers
*/
public static List stringAsIntList(String st, String delimiter) {
return stringAsIntList(st, delimiter, false);
}
/**
* Converts a string of comma-separated numbers into a list of integers.
* @param st string of comma-separated numbers
* @param delimiter a string delimiting numbers in the string
* @param sortList if the numbers in the resulting list should be sorted (ascendantly)
* @return a list of integers
*/
public static List stringAsIntList(String st, String delimiter, boolean sortList) {
List res = new ArrayList<>(); // Always create the list event if input string is empty
if (Util.isEmpty(st)) return res;
String[] parts = st.split(delimiter);
for (String part : parts) {
if (Util.isEmpty(part.trim()))
res.add(0);
else
res.add(Integer.valueOf(part.trim()));
}
if (sortList) Collections.sort(res);
return res;
}
/**
* Remove empty trailing elements of the given list.
* Possible empty elements in the head and middle of the list remain if located before a non-empty element.
* @param list the list to be trimmed.
*/
public static void listTrimTrail(List list) {
if (list == null) return;
for (int i = list.size() -1; i >= 0; i--)
if (Util.isEmpty(list.get(i)))
list.remove(i);
else
break;
}
/**
* Creates a list, containing all trimmed values of a given list of strings. Empty elements remain on the
* list, non-empty ones are trimmed from both sides. The given list is not changed.
* @param list the given list of strings.
* @return the list with trimmed elements.
*/
public static List listTrimValues(List list) {
if ( list == null ) return null;
List res = new ArrayList<>();
for (String st : list)
if (Util.isEmpty(st))
res.add(st);
else
res.add(st.trim());
return res;
}
/**
* Converts a list of strings into an array of those strings.
* @param list List of strings.
* @return an array of strings.
*/
public static String[] stringListAsArray(List list) {
if (Util.isEmpty(list))
return new String[] {};
return list.toArray(new String[] {});
}
/**
* Converts a list of class references into an array of those class references.
* @param list List of class references.
* @return An array of class references.
*/
public static Class>[] classListAsArray(List> list) {
if (Util.isEmpty(list))
return null;
Class>[] res = (Class>[]) Array.newInstance(Class.class, list.size());
for (int i = 0; i < list.size(); i++)
res[i] = list.get(i);
return res;
}
/**
* Converts a list of objects into an array of those objects.
* @param list List of objects.
* @return an array of objects.
*/
public static Object[] objectListAsArray(List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy