All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mindoo.domino.jna.utils.ListUtil Maven / Gradle / Ivy

There is a newer version: 0.9.53
Show newest version
package com.mindoo.domino.jna.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class ListUtil {

	/**
	 * Returns the element at list position list or null,
	 * if the list is not big enough.
	 * 
	 * @param  list element type
	 * @param list list
	 * @param index element position
	 * @return element or null
	 */
	public static  T getNth(List list, int index) {
		if (list==null)
			return null;
		else if (list.size()>index) {
			T element=list.get(index);
			return element;
		}
		else {
			return null;
		}
	}
	
	/**
	 * Method to return a sublist from a list based on the start index start and
	 * the number of entries to return count. In constrast to {@link List#subList(int, int)},
	 * this implementation is forgiving in case start or start+count is higher than
	 * the actual number of list entries.
	 * 
	 * @param  list type
	 * @param list list
	 * @param start start index
	 * @param count number of entries to return
	 * @return sublist, backed by the original list; see {@link List#subList(int, int)} for details
	 */
	public static  List subListChecked(List list, int start, int count) {
		if (start > list.size())
			return Collections.emptyList();
		else {
			long startLong = start;
			long countLong = count;
			//make sure we do not exceed Integer.MAX_VALUE
			long sum = Math.min(Integer.MAX_VALUE, startLong+countLong);
			
			return list.subList(start, Math.min(list.size(), (int) sum));
		}
	}

	/**
	 * Checks if a list contains a value ignoring the case
	 * 
	 * @param list list of strings
	 * @param value value to search for
	 * @return true if list contains value
	 */
	public static boolean containsIgnoreCase(List list, String value) {
		if (list==null)
			return false;
		
		for (String currStr : list) {
			if (currStr.equalsIgnoreCase(value))
				return true;
		}
		return false;
	}
	
	/**
	 * Removes empty strings from the list and trims each list entry
	 * 
	 * @param list list
	 * @return trimmed list
	 */
	public static List fullTrim(List list) {
		if (list==null)
			return list;
		
		boolean isRequired = false;
		for (String currStr : list) {
			if ("".equals(currStr) || currStr.startsWith(" ") || currStr.endsWith(" ")) {
				isRequired = true;
				break;
			}
		}
		
		if (!isRequired)
			return list;
		else {
			List trimmedList = new ArrayList(list.size());
			for (String currStr : list) {
				currStr = currStr.trim();
				if (currStr.length()>0) {
					trimmedList.add(currStr);
				}
			}
			return trimmedList;
		}
	}
	
	/**
	 * Removes empty strings from the list
	 * 
	 * @param list list
	 * @return trimmed list
	 */
	public static List trim(List list) {
		if (list==null)
			return list;
		
		boolean isRequired = false;
		for (String currStr : list) {
			if ("".equals(currStr)) {
				isRequired = true;
				break;
			}
		}
		
		if (!isRequired)
			return list;
		else {
			List trimmedList = new ArrayList(list.size()-1);
			for (String currStr : list) {
				if (currStr.length()>0) {
					trimmedList.add(currStr);
				}
			}
			return trimmedList;
		}
	}
	
	/**
	 * Converts a list with strings to lowercase format
	 * 
	 * @param values list with strings
	 * @param loc locale to use for conversion
	 * @return list in lowercase
	 */
	public static List toLowerCase(List values, Locale loc) {
		if (values==null) {
			return null;
		}
		List listInLowercase = new ArrayList<>(values.size());
		for (String currValue : values) {
			listInLowercase.add(currValue.toLowerCase(loc));
		}
		return listInLowercase;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy