View Javadoc
1   package com.github.mygreen.supercsv.util;
2   
3   import java.util.LinkedList;
4   import java.util.Objects;
5   
6   
7   /**
8    * {@link LinkedList}に対するユーティリティクラス。
9    * @since 2.0
10   * @author T.TSUCHIE
11   *
12   */
13  public class StackUtils {
14      
15      /**
16       * スタックの最後の要素(一番下の要素)が引数で指定した文字列と等しいかどうか比較する。
17       * @param stack
18       * @param str 比較対象の文字列
19       * @return 
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       * @param stack
34       * @param strs
35       * @return
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       * @param stack
59       * @param str 比較対象の文字列
60       * @return 
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       * @param stack
75       * @param strs 比較する文字列の配列
76       * @return
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      * @param stack
101      * @return
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      * @param stack
118      * @return スタックが空の場合は空文字を返す。
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 }