Coverage Report - org.apache.shindig.social.core.util.xstream.ThreadSafeWriterStack
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadSafeWriterStack
74%
20/27
50%
7/14
0
ThreadSafeWriterStack$1
100%
2/2
N/A
0
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations under the License.
 17  
  */
 18  
 package org.apache.shindig.social.core.util.xstream;
 19  
 
 20  
 import com.google.common.collect.Lists;
 21  
 
 22  
 import java.util.ArrayList;
 23  
 import java.util.List;
 24  
 
 25  
 /**
 26  
  * A simple implementation of a WriterStack that can be shared amongst multiple
 27  
  * threads and will record the state of each thread. This cannot however be
 28  
  * shared amongst multiple writers on multiple threads as this would lead to an
 29  
  * inconsistent state. In the shindig implementation this is not an issue as the
 30  
  * serialization process is atomic below the API.
 31  
  */
 32  
 public class ThreadSafeWriterStack implements WriterStack {
 33  
   /**
 34  
    * A thread local holder for the stack.
 35  
    */
 36  74
   private ThreadLocal<List<Object[]>> stackHolder = new ThreadLocal<List<Object[]>>() { 
 37  101
       protected List<Object[]> initialValue() {
 38  27
         return Lists.newArrayList();
 39  
       }
 40  
   };
 41  
 
 42  
   /**
 43  
    * Create a {@link WriterStack} that is thread safe. The stack will store its
 44  
    * contents on the thread so this class can be shared amongst multiple
 45  
    * threads, but obviously there must be only one instance of the class per
 46  
    * writer per thread.
 47  
    */
 48  74
   public ThreadSafeWriterStack() {
 49  74
   }
 50  
 
 51  
   /**
 52  
    * Add an element name to the stack on the current thread.
 53  
    *
 54  
    * @param name
 55  
    *          the node name just added.
 56  
    */
 57  
   public void push(String name, NamespaceSet namespaceSet) {
 58  547
     stackHolder.get().add(new Object[]{name, namespaceSet});
 59  547
   }
 60  
 
 61  
   /**
 62  
    * Remove a node name from the stack on the current thread.
 63  
    *
 64  
    * @return the node name just ended.
 65  
    */
 66  
   public String pop() {
 67  547
     List<Object[]> stack = stackHolder.get();
 68  547
     if (stack.size() == 0) {
 69  0
       return null;
 70  
     } else {
 71  547
       Object[] o =  stack.remove(stack.size() - 1);
 72  547
       if ( o != null && o.length > 0 ) {
 73  547
         return (String) o[0];
 74  
       }
 75  0
       return null;
 76  
     }
 77  
   }
 78  
 
 79  
   /**
 80  
    * {@inheritDoc}
 81  
    * @see org.apache.shindig.social.core.util.xstream.WriterStack#peek()
 82  
    */
 83  
   public String peek() {
 84  101
     return (String) peek(0);
 85  
   }
 86  
 
 87  
   /**
 88  
    * {@inheritDoc}
 89  
    * @see org.apache.shindig.social.core.util.xstream.WriterStack#peekNamespace()
 90  
    */
 91  
   public NamespaceSet peekNamespace() {
 92  1094
     return (NamespaceSet) peek(1);
 93  
   }
 94  
   /**
 95  
    * Look at the node name on the top of the stack on the current thread.
 96  
    *
 97  
    * @return the current node name.
 98  
    */
 99  
   public Object peek(int i) {
 100  1195
     List<Object[]> stack = stackHolder.get();
 101  1195
     if (stack.size() == 0) {
 102  111
       return null;
 103  
     } else {
 104  1084
       Object[] o = stack.get(stack.size() - 1);
 105  1084
       if ( o != null && o.length > i ) {
 106  1084
         return o[i];
 107  
       }
 108  0
       return null;
 109  
     }
 110  
   }
 111  
 
 112  
   /**
 113  
    * Reset the stack back to the default state.
 114  
    *
 115  
    * @see org.apache.shindig.social.core.util.xstream.WriterStack#reset()
 116  
    */
 117  
   public void reset() {
 118  37
     stackHolder.get().clear();
 119  37
   }
 120  
 
 121  
   /**
 122  
    * {@inheritDoc}
 123  
    * @see org.apache.shindig.social.core.util.xstream.WriterStack#size()
 124  
    */
 125  
   public int size() {
 126  0
     List<Object[]> s = stackHolder.get();
 127  0
     if ( s == null ) {
 128  0
       return 0;
 129  
     } else {
 130  0
       return s.size();
 131  
     }
 132  
   }
 133  
 
 134  
 }