1 package com.github.mygreen.supercsv.util;
2
3 import java.util.LinkedList;
4 import java.util.Objects;
5
6
7
8
9
10
11
12
13 public class StackUtils {
14
15
16
17
18
19
20
21 public static boolean equalsBottomElement(final LinkedList<String> stack, final String str) {
22
23 if(stack.isEmpty()) {
24 return false;
25 }
26
27 return stack.peekLast().equals(str);
28
29 }
30
31
32
33
34
35
36
37 public static boolean equalsAnyBottomElement(final LinkedList<String> stack, final String[] strs) {
38 Objects.requireNonNull("stack should not be null.");
39 Objects.requireNonNull("strs should not be null.");
40
41 if(stack.isEmpty()) {
42 return false;
43 }
44
45 final String bottom = stack.peekLast();
46 for(String str : strs) {
47 if(str.equals(bottom)) {
48 return true;
49 }
50 }
51
52 return false;
53
54 }
55
56
57
58
59
60
61
62 public static boolean equalsTopElement(final LinkedList<String> stack, final String str) {
63
64 if(stack.isEmpty()) {
65 return false;
66 }
67
68 return stack.peekFirst().equals(str);
69
70 }
71
72
73
74
75
76
77
78 public static boolean equalsAnyTopElement(final LinkedList<String> stack, final String[] strs) {
79
80 Objects.requireNonNull("stack should not be null.");
81 Objects.requireNonNull("strs should not be null.");
82
83 if(stack.isEmpty()) {
84 return false;
85 }
86
87 final String top = stack.peekFirst();
88 for(String str : strs) {
89 if(str.equals(top)) {
90 return true;
91 }
92 }
93
94 return false;
95
96 }
97
98
99
100
101
102
103 public static String popupAndConcat(final LinkedList<String> stack) {
104
105 StringBuilder value = new StringBuilder();
106
107 while(!stack.isEmpty()) {
108 value.append(stack.pollLast());
109 }
110
111 return value.toString();
112
113 }
114
115
116
117
118
119
120 public static String popup(final LinkedList<String> stack) {
121
122 if(stack.isEmpty()) {
123 return "";
124 }
125
126 return stack.pollFirst();
127 }
128
129 }