/** * Iterates over each navigation element for the PsiElements in {@code psiElementCollection}. * * @param psiElementCollection Collection of PsiElements that aren't guaranteed to be navigation elements, such as * the binary elements in {@code .beam} files. * @param function Return {@code false} to stop processing and abandon enumeration early * @return {@code true} if all calls to {@code function} returned {@code true} */ private static boolean forEachNavigationElement(@NotNull Collection<? extends PsiElement> psiElementCollection, @NotNull Function<PsiElement, Boolean> function) { boolean keepProcessing = true; for (PsiElement psiElement : psiElementCollection) { /* The psiElement may be a ModuleImpl from a .beam. Using #getNaviationElement() ensures a source (either true source or decompiled) is used. */ keepProcessing = function.fun(psiElement.getNavigationElement()); if (!keepProcessing) { break; } } return keepProcessing; }
/** * Creates a list of parameter presentations. For clarity we expand parameters declared as `a, b, c int` into `a int, b int, c int`. */ @NotNull public static List<String> getParameterPresentations(@NotNull GoParameters parameters, @NotNull Function<PsiElement, String> typePresentationFunction) { List<GoParameterDeclaration> paramDeclarations = parameters.getParameterDeclarationList(); List<String> paramPresentations = ContainerUtil.newArrayListWithCapacity(2 * paramDeclarations.size()); for (GoParameterDeclaration paramDeclaration : paramDeclarations) { boolean isVariadic = paramDeclaration.isVariadic(); List<GoParamDefinition> paramDefinitionList = paramDeclaration.getParamDefinitionList(); for (GoParamDefinition paramDefinition : paramDefinitionList) { String separator = isVariadic ? " ..." : " "; paramPresentations.add(paramDefinition.getText() + separator + typePresentationFunction.fun(paramDeclaration.getType())); } if (paramDefinitionList.isEmpty()) { String separator = isVariadic ? "..." : ""; paramPresentations.add(separator + typePresentationFunction.fun(paramDeclaration.getType())); } } return paramPresentations; }
@Nullable public static <T> T transformStdoutLine(@NotNull com.intellij.execution.process.ProcessOutput output, @NotNull Function<String, T> lineTransformer) { List<String> lines; if (output.getExitCode() != 0 || output.isTimeout() || output.isCancelled()) { lines = ContainerUtil.emptyList(); } else { lines = output.getStdoutLines(); } T transformed = null; for (String line : lines) { transformed = lineTransformer.fun(line); if (transformed != null) { break; } } return transformed; }
String correspondingCanonicalName = correspondingName.fun(canonicalName);
return presentationFunction.fun(element);
public void setItems(final List<T> items, @Nullable Function<T, String> converter) { clear(); for (T item : items) { String text = converter != null ? converter.fun(item) : item.toString(); addItem(item, text, false); } }
@Override public U next() { return mapper.fun(iterator.next()); }
@NotNull public static <K,V> V[] convert(@NotNull K[] from, @NotNull V[] to, @NotNull Function<K,V> fun) { if (to.length < from.length) { @SuppressWarnings("unchecked") V[] array = (V[])Array.newInstance(to.getClass().getComponentType(), from.length); to = array; } for (int i = 0; i < from.length; i++) { to[i] = fun.fun(from[i]); } return to; }
@NotNull @Contract(pure=true) public static <T, V> V[] map(@NotNull T[] arr, @NotNull Function<T, V> mapping, @NotNull V[] emptyArray) { if (arr.length==0) { assert emptyArray.length == 0 : "You must pass an empty array"; return emptyArray; } List<V> result = new ArrayList<V>(arr.length); for (T t : arr) { result.add(mapping.fun(t)); } return result.toArray(emptyArray); }
/** * @return read-only set consisting of the elements from collection converted by mapper */ @NotNull @Contract(pure=true) public static <T, V> Set<V> map2Set(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) { if (collection.isEmpty()) return Collections.emptySet(); Set <V> set = new HashSet<V>(collection.size()); for (final T t : collection) { set.add(mapper.fun(t)); } return set; }
/** * @return read-only list consisting of the lists (made by listGenerator) added together */ @NotNull @Contract(pure=true) public static <T, V> List<T> concat(@NotNull Iterable<? extends V> list, @NotNull Function<V, Collection<? extends T>> listGenerator) { List<T> result = new ArrayList<T>(); for (final V v : list) { result.addAll(listGenerator.fun(v)); } return result.isEmpty() ? ContainerUtil.<T>emptyList() : result; }
/** * @return read-only list consisting of the elements from the iterable converted by mapping */ @NotNull @Contract(pure=true) public static <T,V> List<V> map(@NotNull Iterable<? extends T> iterable, @NotNull Function<T, V> mapping) { List<V> result = new ArrayList<V>(); for (T t : iterable) { result.add(mapping.fun(t)); } return result.isEmpty() ? ContainerUtil.<V>emptyList() : result; }
/** * @return read-only list consisting of the elements from the array converted by mapping */ @NotNull @Contract(pure=true) public static <T, V> List<V> map(@NotNull T[] array, @NotNull Function<T, V> mapping) { List<V> result = new ArrayList<V>(array.length); for (T t : array) { result.add(mapping.fun(t)); } return result.isEmpty() ? ContainerUtil.<V>emptyList() : result; }
/** * @return read-only list consisting of the elements from the iterable converted by mapping */ @NotNull @Contract(pure=true) public static <T,V> List<V> map(@NotNull Collection<? extends T> iterable, @NotNull Function<T, V> mapping) { if (iterable.isEmpty()) return emptyList(); List<V> result = new ArrayList<V>(iterable.size()); for (T t : iterable) { result.add(mapping.fun(t)); } return result; }
@NotNull @Contract(pure = true) public static <T, KEY, VALUE> Map<KEY, VALUE> map2MapNotNull(@NotNull Collection<? extends T> collection, @NotNull Function<T, Pair<KEY, VALUE>> mapper) { final Map<KEY, VALUE> set = new THashMap<KEY, VALUE>(collection.size()); for (T t : collection) { Pair<KEY, VALUE> pair = mapper.fun(t); if (pair != null) { set.put(pair.first, pair.second); } } return set; }
@NotNull @Contract(pure=true) public static <T, KEY, VALUE> Map<KEY, VALUE> map2Map(@NotNull Collection<? extends T> collection, @NotNull Function<T, Pair<KEY, VALUE>> mapper) { final Map<KEY, VALUE> set = new THashMap<KEY, VALUE>(collection.size()); for (T t : collection) { Pair<KEY, VALUE> pair = mapper.fun(t); set.put(pair.first, pair.second); } return set; }
/** * @return read-only list consisting of the elements from collection converted by mapper */ @NotNull @Contract(pure=true) public static <T, V> List<V> map2List(@NotNull Collection<? extends T> collection, @NotNull Function<T, V> mapper) { if (collection.isEmpty()) return emptyList(); List<V> list = new ArrayList<V>(collection.size()); for (final T t : collection) { list.add(mapper.fun(t)); } return list; }
public static String getParametersString(List<String> paramsTypes, int offset, int mask, Function<String, String> substitutor, Function<Integer, List<String>> annoProvider, Function<String, String> shortener) { StringBuilder sb = new StringBuilder(); for (int i = offset; i < paramsTypes.size(); i += 2) { if (i > offset) sb.append(", "); String type = paramsTypes.get(i); String name = paramsTypes.get(i + 1); if (type.startsWith("<") && type.endsWith(">")) { type = substitutor.fun(type); } if (BnfConstants.AST_NODE_CLASS.equals(type)) name = "node"; if (type.endsWith("ElementType")) name = "type"; if (type.endsWith("Stub")) name = "stub"; if ((mask & 1) == 1) { List<String> annos = annoProvider.fun(i); for (String s : annos) { if (s.startsWith("kotlin.")) continue; sb.append("@").append(shortener.fun(s)).append(" "); } sb.append(shortener.fun(type)); } if ((mask & 3) == 3) sb.append(" "); if ((mask & 2) == 2) sb.append(name); } return sb.toString(); }
/** * @return read-only list consisting of the elements from the array converted by mapping with nulls filtered out */ @NotNull @Contract(pure=true) public static <T, V> V[] mapNotNull(@NotNull T[] array, @NotNull Function<T, V> mapping, @NotNull V[] emptyArray) { List<V> result = new ArrayList<V>(array.length); for (T t : array) { V v = mapping.fun(t); if (v != null) { result.add(v); } } if (result.isEmpty()) { assert emptyArray.length == 0 : "You must pass an empty array"; return emptyArray; } return result.toArray(emptyArray); }
@NotNull @Contract(pure = true) public static <T> String join(@NotNull Iterable<? extends T> items, @NotNull Function<? super T, String> f, @NotNull @NonNls String separator) { final StringBuilder result = new StringBuilder(); for (T item : items) { String string = f.fun(item); if (string != null && !string.isEmpty()) { if (result.length() != 0) result.append(separator); result.append(string); } } return result.toString(); }