Coverage Report - org.apache.tiles.template.ComposeStackUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ComposeStackUtil
84%
11/13
100%
6/6
2.333
 
 1  
 /*
 2  
  * $Id: ComposeStackUtil.java 1305937 2012-03-27 18:15:15Z nlebas $
 3  
  *
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  * http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 package org.apache.tiles.template;
 23  
 
 24  
 import java.util.Deque;
 25  
 import java.util.LinkedList;
 26  
 import java.util.Map;
 27  
 
 28  
 import org.apache.tiles.request.Request;
 29  
 
 30  
 /**
 31  
  * Utilities to work with compose stacks.
 32  
  *
 33  
  * @version $Rev: 1305937 $ $Date: 2012-03-28 05:15:15 +1100 (Wed, 28 Mar 2012) $
 34  
  * @since 3.0.0
 35  
  */
 36  
 public final class ComposeStackUtil {
 37  
 
 38  
     /**
 39  
      * The name of the attribute that holds the compose stack.
 40  
      */
 41  
     public static final String COMPOSE_STACK_ATTRIBUTE_NAME = "org.apache.tiles.template.COMPOSE_STACK";
 42  
 
 43  
     /**
 44  
      * Private constructor to avoid instantiation.
 45  
      */
 46  0
     private ComposeStackUtil() {
 47  
 
 48  0
     }
 49  
 
 50  
     /**
 51  
      * Finds the first ancestor in the stack, that is assignable to the given class.
 52  
      *
 53  
      * @param composeStack The compose stack to evaluate.
 54  
      * @param clazz The class to check.
 55  
      * @return The first ancestor that is assignable to the class, or null if not found.
 56  
      * @since 3.0.0
 57  
      */
 58  
     public static Object findAncestorWithClass(Deque<Object> composeStack, Class<?> clazz) {
 59  7
         for (Object obj : composeStack) {
 60  14
             if (clazz.isAssignableFrom(obj.getClass())) {
 61  6
                 return obj;
 62  
             }
 63  8
         }
 64  
 
 65  1
         return null;
 66  
     }
 67  
 
 68  
     /**
 69  
      * Returns the current compose stack, or creates a new one if not present.
 70  
      *
 71  
      * @param request The request.
 72  
      * @return The compose stack.
 73  
      * @since 3.0.0
 74  
      */
 75  
     @SuppressWarnings("unchecked")
 76  
     public static Deque<Object> getComposeStack(Request request) {
 77  13
         Map<String, Object> requestScope = request.getContext("request");
 78  13
         Deque<Object> composeStack = (Deque<Object>) requestScope
 79  
                 .get(COMPOSE_STACK_ATTRIBUTE_NAME);
 80  13
         if (composeStack == null) {
 81  3
             composeStack = new LinkedList<Object>();
 82  3
             requestScope.put(ComposeStackUtil.COMPOSE_STACK_ATTRIBUTE_NAME, composeStack);
 83  
         }
 84  13
         return composeStack;
 85  
     }
 86  
 }