View Javadoc

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    *      http://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  package org.apache.logging.log4j.spi;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   *
26   */
27  public class MutableThreadContextStack implements ThreadContextStack {
28  
29      private static final long serialVersionUID = 50505011L;
30  
31      /**
32       * The underlying list (never null).
33       */
34      private final List<String> list;
35  
36      public MutableThreadContextStack(List<String> list) {
37          this.list = new ArrayList<String>(list);
38      }
39  
40      private MutableThreadContextStack(MutableThreadContextStack stack) {
41          this.list = new ArrayList<String>(stack.list);
42      }
43  
44      public String pop() {
45          if (list.isEmpty()) {
46              return null;
47          }
48          final int last = list.size() - 1;
49          final String result = list.remove(last);
50          return result;
51      }
52  
53      public String peek() {
54          if (list.isEmpty()) {
55              return null;
56          }
57          final int last = list.size() - 1;
58          return list.get(last);
59      }
60  
61      public void push(final String message) {
62          list.add(message);
63      }
64  
65      public int getDepth() {
66          return list.size();
67      }
68  
69      public List<String> asList() {
70          return list;
71      }
72  
73      public void trim(final int depth) {
74          if (depth < 0) {
75              throw new IllegalArgumentException("Maximum stack depth cannot be negative");
76          }
77          if (list == null) {
78              return;
79          }
80          final List<String> copy = new ArrayList<String>(list.size());
81          int count = Math.min(depth, list.size());
82          for(int i = 0; i < count; i++) {
83              copy.add(list.get(i));
84          }
85          list.clear();
86          list.addAll(copy);
87      }
88  
89      public ThreadContextStack copy() {
90          return new MutableThreadContextStack(this);
91      }
92  
93      public void clear() {
94          list.clear();
95      }
96  
97      public int size() {
98          return list.size();
99      }
100 
101     public boolean isEmpty() {
102         return list.isEmpty();
103     }
104 
105     public boolean contains(Object o) {
106         return list.contains(o);
107     }
108 
109     public Iterator<String> iterator() {
110         return list.iterator();
111     }
112 
113     public Object[] toArray() {
114         return list.toArray();
115     }
116 
117     public <T> T[] toArray(T[] ts) {
118         return list.toArray(ts);
119     }
120 
121     public boolean add(String s) {
122         return list.add(s);
123     }
124 
125     public boolean remove(Object o) {
126         return list.remove(o);
127     }
128 
129     public boolean containsAll(Collection<?> objects) {
130         return list.containsAll(objects);
131     }
132 
133     public boolean addAll(Collection<? extends String> strings) {
134         return list.addAll(strings);
135     }
136 
137     public boolean removeAll(Collection<?> objects) {
138         return list.removeAll(objects);
139     }
140 
141     public boolean retainAll(Collection<?> objects) {
142         return list.retainAll(objects);
143     }
144 }