1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.lang3.util;
19
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.StringTokenizer;
25 import java.util.stream.Stream;
26 import java.util.stream.StreamSupport;
27
28 import org.apache.commons.lang3.ArrayUtils;
29
30 /**
31 * A {@link StringTokenizer} that implements {@link Iterable}{@code <String>} and conversion methods {@link #toList()} and {@link #toStream()}.
32 *
33 * @since 3.18.0
34 */
35 public class IterableStringTokenizer extends StringTokenizer implements Iterable<String> {
36
37 /**
38 * Constructs a new instance like {@link StringTokenizer#StringTokenizer(String, String, boolean)}.
39 *
40 * @param str a string to be parsed.
41 * @exception NullPointerException if str is {@code null}.
42 */
43 public IterableStringTokenizer(final String str) {
44 super(str);
45 }
46
47 /**
48 * Constructs a new instance like {@link StringTokenizer#StringTokenizer(String, String, boolean)}.
49 *
50 * @param str a string to be parsed.
51 * @param delim the delimiters.
52 * @exception NullPointerException if str is {@code null}.
53 */
54 public IterableStringTokenizer(final String str, final String delim) {
55 super(str, delim);
56 }
57
58 /**
59 * Constructs a new instance like {@link StringTokenizer#StringTokenizer(String, String, boolean)}.
60 *
61 * @param str a string to be parsed.
62 * @param delim the delimiters.
63 * @param returnDelims flag indicating whether to return the delimiters as tokens.
64 * @exception NullPointerException if str is {@code null}.
65 */
66 public IterableStringTokenizer(final String str, final String delim, final boolean returnDelims) {
67 super(str, delim, returnDelims);
68 }
69
70 @Override
71 public Iterator<String> iterator() {
72 return new Iterator<String>() {
73
74 @Override
75 public boolean hasNext() {
76 return hasMoreElements();
77 }
78
79 @Override
80 public String next() {
81 return Objects.toString(nextElement(), null);
82 }
83 };
84 }
85
86 /**
87 * Returns a new {@code String[]} containing the tokenizer elements.
88 *
89 * @return a new {@code String[]}.
90 */
91 public String[] toArray() {
92 return toList().toArray(ArrayUtils.EMPTY_STRING_ARRAY);
93 }
94
95 /**
96 * Returns a new {@link List} containing the tokenizer elements.
97 *
98 * @return a new {@link List}.
99 */
100 public List<String> toList() {
101 final List<String> list = new ArrayList<>();
102 forEach(list::add);
103 return list;
104 }
105
106 /**
107 * Returns a sequential stream on this Iterable instance.
108 *
109 * @return a sequential stream on this Iterable instance.
110 */
111 public Stream<String> toStream() {
112 return StreamSupport.stream(spliterator(), false);
113 }
114 }