Coverage Report - org.apache.tapestry5.internal.pageload.AssemblerContext
 
Classes in this File Line Coverage Branch Coverage Complexity
AssemblerContext
100%
23/23
100%
3/3
0
AssemblerContext$1
100%
3/3
N/A
0
 
 1  
 // Copyright 2009 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.internal.pageload;
 16  
 
 17  
 import org.apache.tapestry5.internal.parser.TemplateToken;
 18  
 import org.apache.tapestry5.internal.parser.TokenType;
 19  
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 20  
 import org.apache.tapestry5.runtime.RenderCommand;
 21  
 
 22  
 import java.util.List;
 23  
 
 24  
 /**
 25  
  * Used when constructing a {@link org.apache.tapestry5.internal.pageload.AssemblerContext}, encapsulating the
 26  
  * assembler, the {@link org.apache.tapestry5.internal.pageload.TokenStream} for the component's template, and helping
 27  
  * to consolidate composable render commands (that is, a series of render commands that are not components can be
 28  
  * replaced with a single {@link org.apache.tapestry5.internal.pageload.CompositeRenderCommand} which reduces the number
 29  
  * of render operations for the page).
 30  
  */
 31  
 class AssemblerContext implements TokenStream
 32  
 {
 33  
     final ComponentAssembler assembler;
 34  
 
 35  
     final TokenStream stream;
 36  
 
 37  736
     private final List<RenderCommand> composable = CollectionFactory.newList();
 38  
 
 39  
     AssemblerContext(ComponentAssembler assembler, TokenStream stream)
 40  736
     {
 41  736
         this.assembler = assembler;
 42  736
         this.stream = stream;
 43  736
     }
 44  
 
 45  
     public boolean more()
 46  
     {
 47  1018
         return stream.more();
 48  
     }
 49  
 
 50  
     public TemplateToken next()
 51  
     {
 52  4088
         return stream.next();
 53  
     }
 54  
 
 55  
     public <T extends TemplateToken> T next(Class<T> type)
 56  
     {
 57  11860
         return stream.next(type);
 58  
     }
 59  
 
 60  
     public TokenType peekType()
 61  
     {
 62  25214
         return stream.peekType();
 63  
     }
 64  
 
 65  
     void addComposable(RenderCommand command)
 66  
     {
 67  10368
         composable.add(command);
 68  10368
     }
 69  
 
 70  
     void flushComposable()
 71  
     {
 72  6722
         switch (composable.size())
 73  
         {
 74  
             case 0:
 75  3890
                 return;
 76  
 
 77  
             case 1:
 78  1292
                 addRenderCommand(composable.get(0));
 79  1292
                 break;
 80  
 
 81  
             default:
 82  1540
                 addRenderCommand(new CompositeRenderCommand(composable.toArray(new RenderCommand[composable.size()])));
 83  
                 break;
 84  
         }
 85  
 
 86  2832
         composable.clear();
 87  2832
     }
 88  
 
 89  
     void add(PageAssemblyAction action)
 90  
     {
 91  6264
         flushComposable();
 92  
 
 93  6264
         assembler.add(action);
 94  6264
     }
 95  
 
 96  
     private void addRenderCommand(final RenderCommand command)
 97  
     {
 98  2832
         assembler.add(new PageAssemblyAction()
 99  
         {
 100  2832
             public void execute(PageAssembly pageAssembly)
 101  
             {
 102  6865
                 pageAssembly.addRenderCommand(command);
 103  6865
             }
 104  
         });
 105  2832
     }
 106  
 }