src/jdk.internal.vm.compiler/share/classes/org.graalvm.util/src/org/graalvm/util/CollectionsUtil.java
changeset 48861 47f19ff9903c
parent 47216 71c04702a3d5
child 50858 2d3e99a72541
equal deleted inserted replaced
48860:5bce1b7e7800 48861:47f19ff9903c
    35 import java.util.function.Supplier;
    35 import java.util.function.Supplier;
    36 
    36 
    37 /**
    37 /**
    38  * This class contains utility methods for commonly used functional patterns for collections.
    38  * This class contains utility methods for commonly used functional patterns for collections.
    39  */
    39  */
    40 public class CollectionsUtil {
    40 public final class CollectionsUtil {
       
    41 
       
    42     private CollectionsUtil() {
       
    43     }
    41 
    44 
    42     /**
    45     /**
    43      * Concatenates two iterables into a single iterable. The iterator exposed by the returned
    46      * Concatenates two iterables into a single iterable. The iterator exposed by the returned
    44      * iterable does not support {@link Iterator#remove()} even if the input iterables do.
    47      * iterable does not support {@link Iterator#remove()} even if the input iterables do.
    45      *
    48      *
    91             }
    94             }
    92 
    95 
    93         };
    96         };
    94     }
    97     }
    95 
    98 
       
    99     /**
       
   100      * Returns whether all elements in {@code inputs} match {@code predicate}. May not evaluate
       
   101      * {@code predicate} on all elements if not necessary for determining the result. If
       
   102      * {@code inputs} is empty then {@code true} is returned and {@code predicate} is not evaluated.
       
   103      *
       
   104      * @return {@code true} if either all elements in {@code inputs} match {@code predicate} or
       
   105      *         {@code inputs} is empty, otherwise {@code false}.
       
   106      */
    96     public static <T> boolean allMatch(T[] inputs, Predicate<T> predicate) {
   107     public static <T> boolean allMatch(T[] inputs, Predicate<T> predicate) {
    97         return allMatch(Arrays.asList(inputs), predicate);
   108         return allMatch(Arrays.asList(inputs), predicate);
    98     }
   109     }
    99 
   110 
       
   111     /**
       
   112      * Returns whether all elements in {@code inputs} match {@code predicate}. May not evaluate
       
   113      * {@code predicate} on all elements if not necessary for determining the result. If
       
   114      * {@code inputs} is empty then {@code true} is returned and {@code predicate} is not evaluated.
       
   115      *
       
   116      * @return {@code true} if either all elements in {@code inputs} match {@code predicate} or
       
   117      *         {@code inputs} is empty, otherwise {@code false}.
       
   118      */
   100     public static <T> boolean allMatch(Iterable<T> inputs, Predicate<T> predicate) {
   119     public static <T> boolean allMatch(Iterable<T> inputs, Predicate<T> predicate) {
   101         for (T t : inputs) {
   120         for (T t : inputs) {
   102             if (!predicate.test(t)) {
   121             if (!predicate.test(t)) {
   103                 return false;
   122                 return false;
   104             }
   123             }
   105         }
   124         }
   106         return true;
   125         return true;
   107     }
   126     }
   108 
   127 
       
   128     /**
       
   129      * Returns whether any elements in {@code inputs} match {@code predicate}. May not evaluate
       
   130      * {@code predicate} on all elements if not necessary for determining the result. If
       
   131      * {@code inputs} is empty then {@code false} is returned and {@code predicate} is not
       
   132      * evaluated.
       
   133      *
       
   134      * @return {@code true} if any elements in {@code inputs} match {@code predicate}, otherwise
       
   135      *         {@code false}.
       
   136      */
   109     public static <T> boolean anyMatch(T[] inputs, Predicate<T> predicate) {
   137     public static <T> boolean anyMatch(T[] inputs, Predicate<T> predicate) {
   110         return anyMatch(Arrays.asList(inputs), predicate);
   138         return anyMatch(Arrays.asList(inputs), predicate);
   111     }
   139     }
   112 
   140 
       
   141     /**
       
   142      * Returns whether any elements in {@code inputs} match {@code predicate}. May not evaluate
       
   143      * {@code predicate} on all elements if not necessary for determining the result. If
       
   144      * {@code inputs} is empty then {@code false} is returned and {@code predicate} is not
       
   145      * evaluated.
       
   146      *
       
   147      * @return {@code true} if any elements in {@code inputs} match {@code predicate}, otherwise
       
   148      *         {@code false}.
       
   149      */
   113     public static <T> boolean anyMatch(Iterable<T> inputs, Predicate<T> predicate) {
   150     public static <T> boolean anyMatch(Iterable<T> inputs, Predicate<T> predicate) {
   114         for (T t : inputs) {
   151         for (T t : inputs) {
   115             if (predicate.test(t)) {
   152             if (predicate.test(t)) {
   116                 return true;
   153                 return true;
   117             }
   154             }
   118         }
   155         }
   119         return false;
   156         return false;
   120     }
   157     }
   121 
   158 
       
   159     /**
       
   160      * Returns a new list consisting of the elements in {@code inputs} that match {@code predicate}.
       
   161      *
       
   162      * @return the new list.
       
   163      */
   122     public static <T> List<T> filterToList(List<T> inputs, Predicate<? super T> predicate) {
   164     public static <T> List<T> filterToList(List<T> inputs, Predicate<? super T> predicate) {
   123         return filterToList(inputs, predicate, ArrayList::new);
   165         return filterToList(inputs, predicate, ArrayList::new);
   124     }
   166     }
   125 
   167 
       
   168     /**
       
   169      * Appends elements of {@code inputs} that match {@code predicate} to the list generated by
       
   170      * {@code listGenerator}.
       
   171      *
       
   172      * @return the list generated by {@code listGenerator}.
       
   173      */
   126     public static <T> List<T> filterToList(List<T> inputs, Predicate<? super T> predicate, Supplier<List<T>> listGenerator) {
   174     public static <T> List<T> filterToList(List<T> inputs, Predicate<? super T> predicate, Supplier<List<T>> listGenerator) {
   127         List<T> resultList = listGenerator.get();
   175         List<T> resultList = listGenerator.get();
   128         for (T t : inputs) {
   176         for (T t : inputs) {
   129             if (predicate.test(t)) {
   177             if (predicate.test(t)) {
   130                 resultList.add(t);
   178                 resultList.add(t);
   132         }
   180         }
   133         return resultList;
   181         return resultList;
   134     }
   182     }
   135 
   183 
   136     /**
   184     /**
   137      * Filters the inputs, maps them given the mapping function and adds them in the array provided
   185      * Filters {@code inputs} with {@code predicate}, applies {@code mapper} and adds them in the
   138      * by the generator.
   186      * array provided by {@code arrayGenerator}.
       
   187      *
       
   188      * @return the array provided by {@code arrayGenerator}.
   139      */
   189      */
   140     public static <T, R> R[] filterAndMapToArray(T[] inputs, Predicate<? super T> predicate, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
   190     public static <T, R> R[] filterAndMapToArray(T[] inputs, Predicate<? super T> predicate, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
   141         List<R> resultList = new ArrayList<>();
   191         List<R> resultList = new ArrayList<>();
   142         for (T t : inputs) {
   192         for (T t : inputs) {
   143             if (predicate.test(t)) {
   193             if (predicate.test(t)) {
   146         }
   196         }
   147         return resultList.toArray(arrayGenerator.apply(resultList.size()));
   197         return resultList.toArray(arrayGenerator.apply(resultList.size()));
   148     }
   198     }
   149 
   199 
   150     /**
   200     /**
   151      * Maps the inputs given the mapping function and adds them in the array provided by the
   201      * Applies {@code mapper} on the elements in {@code inputs} and adds them in the array provided
   152      * generator.
   202      * by {@code arrayGenerator}.
       
   203      *
       
   204      * @return the array provided by {@code arrayGenerator}.
   153      */
   205      */
   154     public static <T, R> R[] mapToArray(T[] inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
   206     public static <T, R> R[] mapToArray(T[] inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
   155         return mapToArray(Arrays.asList(inputs), mapper, arrayGenerator);
   207         return mapToArray(Arrays.asList(inputs), mapper, arrayGenerator);
   156     }
   208     }
   157 
   209 
       
   210     /**
       
   211      * Applies {@code mapper} on the elements in {@code inputs} and adds them in the array provided
       
   212      * by {@code arrayGenerator}.
       
   213      *
       
   214      * @return the array provided by {@code arrayGenerator}.
       
   215      */
   158     public static <T, R> R[] mapToArray(Collection<T> inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
   216     public static <T, R> R[] mapToArray(Collection<T> inputs, Function<? super T, ? extends R> mapper, IntFunction<R[]> arrayGenerator) {
   159         R[] result = arrayGenerator.apply(inputs.size());
   217         R[] result = arrayGenerator.apply(inputs.size());
   160         int idx = 0;
   218         int idx = 0;
   161         for (T t : inputs) {
   219         for (T t : inputs) {
   162             result[idx++] = mapper.apply(t);
   220             result[idx++] = mapper.apply(t);
   163         }
   221         }
   164         return result;
   222         return result;
   165     }
   223     }
   166 
   224 
       
   225     /**
       
   226      * Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
       
   227      * by {@code delimiter}.
       
   228      *
       
   229      * @return a new String that is composed from {@code inputs}.
       
   230      */
   167     public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter) {
   231     public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter) {
   168         return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, "", "");
   232         return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, "", "");
   169     }
   233     }
   170 
   234 
       
   235     /**
       
   236      * Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
       
   237      * by {@code delimiter} and starting with {@code prefix}.
       
   238      *
       
   239      * @return a new String that is composed from {@code inputs}.
       
   240      */
   171     public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix) {
   241     public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix) {
   172         return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, prefix, "");
   242         return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, prefix, "");
   173     }
   243     }
   174 
   244 
       
   245     /**
       
   246      * Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
       
   247      * by {@code delimiter} and starting with {@code prefix} and ending with {@code suffix}.
       
   248      *
       
   249      * @return a new String that is composed from {@code inputs}.
       
   250      */
   175     public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix, String suffix) {
   251     public static <T, R> String mapAndJoin(T[] inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix, String suffix) {
   176         return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, prefix, suffix);
   252         return mapAndJoin(Arrays.asList(inputs), mapper, delimiter, prefix, suffix);
   177     }
   253     }
   178 
   254 
       
   255     /**
       
   256      * Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
       
   257      * by {@code delimiter}.
       
   258      *
       
   259      * @return a new String that is composed from {@code inputs}.
       
   260      */
   179     public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter) {
   261     public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter) {
   180         return mapAndJoin(inputs, mapper, delimiter, "", "");
   262         return mapAndJoin(inputs, mapper, delimiter, "", "");
   181     }
   263     }
   182 
   264 
       
   265     /**
       
   266      * Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
       
   267      * by {@code delimiter} and starting with {@code prefix}.
       
   268      *
       
   269      * @return a new String that is composed from {@code inputs}.
       
   270      */
   183     public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix) {
   271     public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix) {
   184         return mapAndJoin(inputs, mapper, delimiter, prefix, "");
   272         return mapAndJoin(inputs, mapper, delimiter, prefix, "");
   185     }
   273     }
   186 
   274 
       
   275     /**
       
   276      * Applies {@code mapper} on the elements in {@code inputs}, and joins them together separated
       
   277      * by {@code delimiter} and starting with {@code prefix} and ending with {@code suffix}.
       
   278      *
       
   279      * @return a new String that is composed from {@code inputs}.
       
   280      */
   187     public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix, String suffix) {
   281     public static <T, R> String mapAndJoin(Iterable<T> inputs, Function<? super T, ? extends R> mapper, String delimiter, String prefix, String suffix) {
   188         StringBuilder strb = new StringBuilder();
   282         StringBuilder strb = new StringBuilder();
   189         String sep = "";
   283         String sep = "";
   190         for (T t : inputs) {
   284         for (T t : inputs) {
   191             strb.append(sep).append(prefix).append(mapper.apply(t)).append(suffix);
   285             strb.append(sep).append(prefix).append(mapper.apply(t)).append(suffix);
   192             sep = delimiter;
   286             sep = delimiter;
   193         }
   287         }
   194         return strb.toString();
   288         return strb.toString();
   195     }
   289     }
       
   290 
   196 }
   291 }