Coverage Report - org.kuali.rice.krms.impl.provider.repository.LazyProposition
 
Classes in this File Line Coverage Branch Coverage Complexity
LazyProposition
0%
0/24
0%
0/8
1.444
LazyProposition$1
0%
0/4
N/A
1.444
 
 1  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.krms.impl.provider.repository;
 17  
 
 18  
 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
 19  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 20  
 import org.kuali.rice.krms.framework.engine.Proposition;
 21  
 import org.kuali.rice.krms.framework.engine.PropositionResult;
 22  
 import org.kuali.rice.krms.framework.type.PropositionTypeService;
 23  
 import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
 24  
 
 25  
 import java.util.Collections;
 26  
 import java.util.List;
 27  
 
 28  
 /**
 29  
  * TODO... 
 30  
  * 
 31  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 32  
  *
 33  
  */
 34  
 final class LazyProposition implements Proposition {
 35  
 
 36  
         private final PropositionDefinition propositionDefinition;
 37  
         private final KrmsTypeResolver resolver;
 38  
         
 39  0
         private final Object mutex = new Object();
 40  
         
 41  
         // volatile for double-checked locking idiom
 42  
         private volatile Proposition proposition;
 43  
         
 44  0
         LazyProposition(PropositionDefinition propositionDefinition, KrmsTypeResolver resolver) {
 45  0
                 this.propositionDefinition = propositionDefinition;
 46  0
                 this.resolver = resolver;
 47  0
                 this.proposition = null;
 48  0
         }
 49  
         
 50  
         @Override
 51  
         public PropositionResult evaluate(ExecutionEnvironment environment) {
 52  0
                 return getProposition().evaluate(environment);
 53  
         }
 54  
         
 55  
         /**
 56  
          * Gets the proposition using a lazy double-checked locking mechanism as documented in Effective Java Item 71.
 57  
          */
 58  
         private Proposition getProposition() {
 59  0
                 Proposition localProposition = proposition;
 60  0
                 if (localProposition == null) {
 61  0
                         synchronized (mutex) {
 62  0
                                 localProposition = proposition;
 63  0
                                 if (localProposition == null) {
 64  0
                                         proposition = localProposition = constructProposition();
 65  
                                 }
 66  0
                         }
 67  
                 }
 68  0
                 return localProposition;
 69  
         }
 70  
         
 71  
         private Proposition constructProposition() {
 72  0
         Proposition proposition = null;
 73  0
         if (propositionDefinition != null) {
 74  0
                     PropositionTypeService propositionTypeService = resolver.getPropositionTypeService(propositionDefinition);
 75  0
                     proposition = propositionTypeService.loadProposition(propositionDefinition);
 76  
         }
 77  0
                 if (proposition == null) {
 78  0
                         proposition = new Proposition() {
 79  
                                 @Override
 80  
                                 public PropositionResult evaluate(ExecutionEnvironment environment) {
 81  0
                                         return new PropositionResult(true);
 82  
                                 }
 83  
 
 84  
                             @Override
 85  
                             public List<Proposition> getChildren() {
 86  0
                                 return Collections.emptyList();
 87  
                             }
 88  
                             
 89  
                             @Override
 90  
                             public boolean isCompound() {
 91  0
                                 return false;
 92  
                             }
 93  
                         };
 94  
                 }
 95  0
                 return proposition;
 96  
         }
 97  
 
 98  
         @Override
 99  
         public List<Proposition> getChildren() {
 100  0
             return getProposition().getChildren();
 101  
         }
 102  
         
 103  
         @Override
 104  
         public boolean isCompound() {
 105  0
             return getProposition().isCompound();
 106  
         }
 107  
 }