public LoopData(@Nullable Object... objects) { if (Collections.isEmpty(objects)) { this.length = (long) 0; } else { this.length = (long) objects.length; } }
public static boolean notEmpty(@Nullable Collection<?> c) { return !isEmpty(c); }
@Nonnull public static <T> List<T> asList(T... ts) { final List<T> result = new ArrayList<T>(); if (!Collections.isEmpty(ts)) { for (T t : ts) { result.add(t); } } return result; }
/** * Method returns last list element. * * @param list list of elements * @param <T> type of element in list * @return null if list is null, or if list is empty, last element otherwise */ public static <T> T getLastListElement(List<? extends T> list) { T result = null; if (!isEmpty(list)) { result = list.get(list.size() - 1); } return result; }
public LoopData(@Nullable Collection collection) { if (Collections.isEmpty(collection)) { this.length = (long) 0; } else { this.length = (long) collection.size(); } }
/** * Method returns any first collection element. * * @param collection collection of elements * @param <T> type of element in collection * @return null if collection is null, or if collections is empty, any first element otherwise */ @Nullable public static <T> T getFirstCollectionElement(Collection<? extends T> collection) { T result = null; if (!isEmpty(collection)) { for (T t : collection) { result = t; break; } } return result; }
/** * Method returns first list element. * * @param list list of elements * @param <T> type of element in list * @return null if list is null, or if list is empty, first element otherwise */ @Nullable public static <T> T getFirstListElement(List<? extends T> list) { T result = null; if (!isEmpty(list)) { result = list.get(0); } return result; }
@Nonnull public static String fromStackTrace(@Nullable StackTraceElement... stackTraceElements) { final StringBuilder sb = new StringBuilder(); if (!Collections.isEmpty(stackTraceElements)) { for (StackTraceElement stackTraceElement : stackTraceElements) { sb.append(" at "); sb.append(stackTraceElement.toString()); sb.append(System.getProperty("line.separator")); } } return sb.toString(); }
public static <T> T find(@Nullable Collection<T> list, @Nonnull JPredicate<T> finder) { T result = null; if (!isEmpty(list)) { for (T t : list) { if (finder.apply(t)) { result = t; break; } } } return result; }
@Nonnull public static <T> List<T> removeAll(@Nullable Collection<T> list, @Nonnull JPredicate<T> finder) { final List<T> result = new ArrayList<T>(); if (!isEmpty(list)) { for (Iterator<T> it = list.iterator(); it.hasNext(); ) { final T t = it.next(); if (finder.apply(t)) { result.add(t); it.remove(); } } } return result; }
@Nullable public static <T> T removeFirst(@Nullable Collection<T> list, @Nonnull JPredicate<T> finder) { T result = null; if (!isEmpty(list)) { for (Iterator<T> it = list.iterator(); it.hasNext(); ) { final T t = it.next(); if (finder.apply(t)) { result = t; it.remove(); break; } } } return result; }
@Nullable public static <T extends Number> T minMax(@Nullable Collection<T> numbers, @Nonnull ComparisonType comparisonType) { T result = null; if (!Collections.isEmpty(numbers)) { for (T number : numbers) { if (number != null) { if (result == null) { result = number; } else { result = minMax(number, result, comparisonType); } } } } return result; }
/** * @param locale locale for which default formatting will be applied * @param messagePattern message pattern which will be used for {@link java.text.MessageFormat} * @param parameters message parameters which will be used for {@link java.text.MessageFormat} * @return formatted message string according to default locale formatting, nested messages are processed properly * (for each message from parameter method {@link Message#getLocalizedMessage(java.util.Locale)} is called) */ @Nonnull public static String prepareMessage(@Nonnull Locale locale, @Nonnull String messagePattern, @Nonnull List<?> parameters) { String result; if (Collections.isEmpty(parameters)) { result = messagePattern; } else { final MessageFormat format = new MessageFormat(messagePattern); format.setLocale(locale); format.applyPattern(messagePattern); result = format.format(prepareParameters(parameters, locale)); } return result; }