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(final List<String> list) {
37          this.list = new ArrayList<String>(list);
38      }
39  
40      private MutableThreadContextStack(final MutableThreadContextStack stack) {
41          this.list = new ArrayList<String>(stack.list);
42      }
43  
44      @Override
45      public String pop() {
46          if (list.isEmpty()) {
47              return null;
48          }
49          final int last = list.size() - 1;
50          final String result = list.remove(last);
51          return result;
52      }
53  
54      @Override
55      public String peek() {
56          if (list.isEmpty()) {
57              return null;
58          }
59          final int last = list.size() - 1;
60          return list.get(last);
61      }
62  
63      @Override
64      public void push(final String message) {
65          list.add(message);
66      }
67  
68      @Override
69      public int getDepth() {
70          return list.size();
71      }
72  
73      @Override
74      public List<String> asList() {
75          return list;
76      }
77  
78      @Override
79      public void trim(final int depth) {
80          if (depth < 0) {
81              throw new IllegalArgumentException("Maximum stack depth cannot be negative");
82          }
83          if (list == null) {
84              return;
85          }
86          final List<String> copy = new ArrayList<String>(list.size());
87          final int count = Math.min(depth, list.size());
88          for (int i = 0; i < count; i++) {
89              copy.add(list.get(i));
90          }
91          list.clear();
92          list.addAll(copy);
93      }
94  
95      @Override
96      public ThreadContextStack copy() {
97          return new MutableThreadContextStack(this);
98      }
99  
100     @Override
101     public void clear() {
102         list.clear();
103     }
104 
105     @Override
106     public int size() {
107         return list.size();
108     }
109 
110     @Override
111     public boolean isEmpty() {
112         return list.isEmpty();
113     }
114 
115     @Override
116     public boolean contains(final Object o) {
117         return list.contains(o);
118     }
119 
120     @Override
121     public Iterator<String> iterator() {
122         return list.iterator();
123     }
124 
125     @Override
126     public Object[] toArray() {
127         return list.toArray();
128     }
129 
130     @Override
131     public <T> T[] toArray(final T[] ts) {
132         return list.toArray(ts);
133     }
134 
135     @Override
136     public boolean add(final String s) {
137         return list.add(s);
138     }
139 
140     @Override
141     public boolean remove(final Object o) {
142         return list.remove(o);
143     }
144 
145     @Override
146     public boolean containsAll(final Collection<?> objects) {
147         return list.containsAll(objects);
148     }
149 
150     @Override
151     public boolean addAll(final Collection<? extends String> strings) {
152         return list.addAll(strings);
153     }
154 
155     @Override
156     public boolean removeAll(final Collection<?> objects) {
157         return list.removeAll(objects);
158     }
159 
160     @Override
161     public boolean retainAll(final Collection<?> objects) {
162         return list.retainAll(objects);
163     }
164 
165     @Override
166     public String toString() {
167         return String.valueOf(list);
168     }
169 }