Coverage Report - org.apache.johnzon.core.ThreadLocalBufferCache
 
Classes in this File Line Coverage Branch Coverage Complexity
ThreadLocalBufferCache
0 %
0/11
0 %
0/2
1,4
ThreadLocalBufferCache$1
0 %
0/2
N/A
1,4
 
 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
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.core;
 20  
 
 21  
 import java.io.Serializable;
 22  
 
 23  
 public abstract class ThreadLocalBufferCache<T> implements Serializable {
 24  
     // alloc are expensive so using a very trivial buffer, we remove from TL to say we use it and reset it when we are done (close)
 25  
     private final ThreadLocal<T> buffers;
 26  
     private final int defaultSize;
 27  
 
 28  0
     public ThreadLocalBufferCache(final int defaultSize) {
 29  0
         this.buffers = new ThreadLocal<T>() {
 30  
             @Override
 31  
             protected T initialValue() {
 32  0
                 return ThreadLocalBufferCache.this.newValue(defaultSize);
 33  
             }
 34  
         };
 35  0
         this.defaultSize = defaultSize;
 36  0
     }
 37  
 
 38  
     protected abstract T newValue(final int defaultSize);
 39  
 
 40  
     public T getCache() {
 41  0
         final T cachedLoadedChars = buffers.get();
 42  0
         if (cachedLoadedChars == null) {
 43  0
             return newValue(defaultSize);
 44  
         }
 45  0
         buffers.set(null); // remove just kills the buffers
 46  0
         return cachedLoadedChars;
 47  
     }
 48  
 
 49  
 
 50  
     public void release(final T buffer) {
 51  0
         buffers.set(buffer);
 52  0
     }
 53  
 }