Coverage Report - org.kuali.rice.krms.impl.provider.repository.LazyProposition
 
Classes in this File Line Coverage Branch Coverage Complexity
LazyProposition
0%
0/22
0%
0/6
1.333
LazyProposition$1
0%
0/4
N/A
1.333
 
 1  
 /*
 2  
  * Copyright 2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 1.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/ecl1.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 java.util.Collections;
 19  
 import java.util.List;
 20  
 
 21  
 import org.kuali.rice.krms.api.engine.ExecutionEnvironment;
 22  
 import org.kuali.rice.krms.api.repository.proposition.PropositionDefinition;
 23  
 import org.kuali.rice.krms.api.repository.type.KrmsTypeDefinition;
 24  
 import org.kuali.rice.krms.framework.engine.Proposition;
 25  
 import org.kuali.rice.krms.framework.engine.PropositionResult;
 26  
 import org.kuali.rice.krms.framework.type.PropositionTypeService;
 27  
 import org.kuali.rice.krms.impl.type.KrmsTypeResolver;
 28  
 
 29  
 /**
 30  
  * TODO... 
 31  
  * 
 32  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 33  
  *
 34  
  */
 35  
 final class LazyProposition implements Proposition {
 36  
 
 37  
         private final PropositionDefinition propositionDefinition;
 38  
         private final KrmsTypeResolver resolver;
 39  
         
 40  0
         private final Object mutex = new Object();
 41  
         
 42  
         // volatile for double-checked locking idiom
 43  
         private volatile Proposition proposition;
 44  
         
 45  0
         LazyProposition(PropositionDefinition propositionDefinition, KrmsTypeResolver resolver) {
 46  0
                 this.propositionDefinition = propositionDefinition;
 47  0
                 this.resolver = resolver;
 48  0
                 this.proposition = null;
 49  0
         }
 50  
         
 51  
         @Override
 52  
         public PropositionResult evaluate(ExecutionEnvironment environment) {
 53  0
                 return getProposition().evaluate(environment);
 54  
         }
 55  
         
 56  
         /**
 57  
          * Gets the proposition using a lazy double-checked locking mechanism as documented in Effective Java Item 71.
 58  
          */
 59  
         private Proposition getProposition() {
 60  0
                 Proposition localProposition = proposition;
 61  0
                 if (localProposition == null) {
 62  0
                         synchronized (mutex) {
 63  0
                                 localProposition = proposition;
 64  0
                                 if (localProposition == null) {
 65  0
                                         proposition = localProposition = constructProposition();
 66  
                                 }
 67  0
                         }
 68  
                 }
 69  0
                 return localProposition;
 70  
         }
 71  
         
 72  
         private Proposition constructProposition() {
 73  0
                 PropositionTypeService propositionTypeService = resolver.getPropositionTypeService(propositionDefinition);
 74  0
                 Proposition proposition = propositionTypeService.loadProposition(propositionDefinition);
 75  0
                 if (proposition == null) {
 76  0
                         proposition = new Proposition() {
 77  
                                 @Override
 78  
                                 public PropositionResult evaluate(ExecutionEnvironment environment) {
 79  0
                                         return new PropositionResult(true);
 80  
                                 }
 81  
 
 82  
                             @Override
 83  
                             public List<Proposition> getChildren() {
 84  0
                                 return Collections.emptyList();
 85  
                             }
 86  
                             
 87  
                             @Override
 88  
                             public boolean isCompound() {
 89  0
                                 return false;
 90  
                             }
 91  
                         };
 92  
                 }
 93  0
                 return proposition;
 94  
         }
 95  
 
 96  
         @Override
 97  
         public List<Proposition> getChildren() {
 98  0
             return getProposition().getChildren();
 99  
         }
 100  
         
 101  
         @Override
 102  
         public boolean isCompound() {
 103  0
             return getProposition().isCompound();
 104  
         }
 105  
 }