Line data Source code
1 : // Copyright 2026 The Authors. See the AUTHORS file for details.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // https://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : import '../../collections.dart' show Stack;
16 :
17 : /// Container for a string or a list of strings.
18 : class StringList {
19 : /// The strings held by this instance
20 : final Stack<String> _stack = Stack();
21 :
22 1 : StringList(String value) {
23 2 : _stack.push(value);
24 : }
25 :
26 : /// Creates a StringList from a string.
27 2 : StringList.fromString(String value) : this(value);
28 :
29 : /// Creates a StringList from a list of strings.
30 1 : StringList.fromList(List<String> values) {
31 2 : for (final s in values) {
32 2 : _stack.push(s);
33 : }
34 : }
35 :
36 : /// Returns the strings as a list
37 3 : List<String> get value => _stack.toList();
38 :
39 : /// Return all string values in a list.
40 2 : List<String> toList() => value;
41 :
42 : /// Returns the first string value.
43 0 : String? get firstOrNull => value.firstOrNull;
44 :
45 : /// Adds a string to the StringList, returning the updated StringList.
46 : ///
47 : /// `this` is returned for chaining.
48 1 : StringList add(String s) {
49 2 : _stack.push(s);
50 : return this;
51 : }
52 :
53 : /// Concatenates a string to latest string in the StringList.
54 : ///
55 : /// `this` is returned for chaining.
56 1 : StringList concatenate(String s) {
57 3 : final newValue = _stack.pop() + s;
58 2 : _stack.push(newValue);
59 : return this;
60 : }
61 :
62 : /// Returns the joined string.
63 3 : String join({String separator = ''}) => value.join(separator);
64 :
65 1 : @override
66 1 : String toString() => join();
67 :
68 1 : @override
69 : bool operator ==(Object other) =>
70 : identical(this, other) ||
71 1 : other is StringList &&
72 3 : runtimeType == other.runtimeType &&
73 3 : _stack == other._stack;
74 :
75 1 : @override
76 2 : int get hashCode => _stack.hashCode;
77 : }
|