| /*=============================================================================# |
| # Copyright (c) 2012, 2021 Stephan Wahlbrink and others. |
| # |
| # This program and the accompanying materials are made available under the |
| # terms of the Eclipse Public License 2.0 which is available at |
| # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| # which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| # |
| # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| # |
| # Contributors: |
| # Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation |
| #=============================================================================*/ |
| |
| package org.eclipse.statet.jcommons.collections; |
| |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.FIELD; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.PARAMETER; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.RETURN_TYPE; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.TYPE_ARGUMENT; |
| import static org.eclipse.statet.jcommons.lang.NullDefaultLocation.TYPE_BOUND; |
| |
| import java.util.Collection; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.RandomAccess; |
| |
| import org.eclipse.statet.jcommons.lang.NonNull; |
| import org.eclipse.statet.jcommons.lang.NonNullByDefault; |
| |
| |
| @NonNullByDefault |
| public class CollectionUtils { |
| |
| |
| public static String toString(final List<@NonNull ?> list, final String sep) { |
| final int n= list.size(); |
| if (n <= 0) { |
| return ""; //$NON-NLS-1$ |
| } |
| else if (n == 1) { |
| return list.get(0).toString(); |
| } |
| else if (list instanceof RandomAccess) { |
| final StringBuilder sb= new StringBuilder(list.get(0).toString()); |
| for (int i= 1; i < n; i++) { |
| sb.append(sep); |
| sb.append(list.get(i).toString()); |
| } |
| return sb.toString(); |
| } |
| else { |
| final Iterator<?> iter= list.iterator(); |
| final StringBuilder sb= new StringBuilder(iter.next().toString()); |
| for (int i= 1; i < n; i++) { |
| sb.append(sep); |
| sb.append(iter.next().toString()); |
| } |
| return sb.toString(); |
| } |
| } |
| |
| public static String toString(final Collection<@NonNull ?> c, final String sep) { |
| if (c instanceof List) { |
| return toString((List<?>) c, sep); |
| } |
| final int n= c.size(); |
| if (n <= 0) { |
| return ""; //$NON-NLS-1$ |
| } |
| else if (n == 1) { |
| return c.iterator().next().toString(); |
| } |
| else { |
| final Iterator<?> iter= c.iterator(); |
| final StringBuilder sb= new StringBuilder(iter.next().toString()); |
| for (int i= 1; i < n; i++) { |
| sb.append(sep); |
| sb.append(iter.next().toString()); |
| } |
| return sb.toString(); |
| } |
| } |
| |
| |
| @NonNullByDefault({ PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT }) |
| public static ImIdentityList<String> toIdentifierList(final String[] array) { |
| int count= 0; |
| for (int i= 0; i < array.length; i++) { |
| final String s= array[i]; |
| if (s != null && !s.isEmpty()) { |
| array[count++]= s.intern(); |
| } |
| } |
| return ImCollections.newIdentityList(array, 0, count); |
| } |
| |
| @NonNullByDefault({ PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT }) |
| public static ImIdentitySet<String> toIdentifierSet(final String[] array) { |
| int count= 0; |
| for (int i= 0; i < array.length; i++) { |
| final String s= array[i]; |
| if (s != null && !s.isEmpty()) { |
| array[count++]= s.intern(); |
| } |
| } |
| return ImCollections.newIdentitySet(array, 0, count); |
| } |
| |
| } |