Coverage Report - org.openpermis.editor.policy.presenter.PolicyPartPool
 
Classes in this File Line Coverage Branch Coverage Complexity
PolicyPartPool
66%
30/45
27%
6/22
2.5
PolicyPartPool$DefaultPartFinder
100%
2/2
N/A
2.5
PolicyPartPool$PartFinder
N/A
N/A
2.5
 
 1  
 /*
 2  
  * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport
 3  
  *                     (http://www.vbs.admin.ch)
 4  
  * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch)
 5  
  * All rights reserved.
 6  
  *
 7  
  * Licensed under the Open Permis License which accompanies this distribution,
 8  
  * and is available at http://www.openpermis.org/BSDlicenceKent.txt
 9  
  */
 10  
 package org.openpermis.editor.policy.presenter;
 11  
 
 12  
 import static org.jdesktop.observablecollections.ObservableCollections.observableList;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.HashSet;
 16  
 import java.util.List;
 17  
 import java.util.Set;
 18  
 
 19  
 import org.jdesktop.observablecollections.ObservableList;
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import org.openpermis.policy.bean.PartBean;
 24  
 import org.openpermis.policy.bean.PolicyBean;
 25  
 import org.openpermis.policy.bean.SerialNumber;
 26  
 
 27  
 /**
 28  
  * Pool-Collections for a Policy.
 29  
  * @param <P> type of PartBean
 30  
  * @since 0.1.0
 31  
  */
 32  4
 public class PolicyPartPool<P extends PartBean>
 33  
 {
 34  
 
 35  
         //---- Static
 36  
 
 37  
         /**
 38  
          * The logger object of this class.
 39  
          * @since 0.1.0
 40  
          */
 41  1
         private static final Logger LOGGER =
 42  
                 LoggerFactory.getLogger(PolicyPartPool.class);
 43  
 
 44  
         //---- State
 45  
 
 46  
         private final ObservableList<P> poolList;
 47  
 
 48  
         private final Class<P> type;
 49  
         
 50  
         private PartFinder<P> partFinder;
 51  
 
 52  
         //---- Constructors
 53  
 
 54  
         /**
 55  
          * Creates a new pool.
 56  
          * @since 0.1.0
 57  
          */
 58  
         public PolicyPartPool (PolicyBean policy, Class<P> type) {
 59  4
                 super();
 60  4
                 this.poolList = observableList(new ArrayList<P>());
 61  4
                 this.type = type;
 62  4
                 this.partFinder = new DefaultPartFinder();
 63  4
                 mergePool(policy, false);
 64  4
         }
 65  
         
 66  
         /**
 67  
          * Creates a new pool.
 68  
          * @since 0.3.0
 69  
          */
 70  
         public PolicyPartPool (PolicyBean policy, PartFinder<P> partFinder) {
 71  2
                 super();
 72  2
                 this.poolList = observableList(new ArrayList<P>());
 73  2
                 this.type = null;
 74  2
                 this.partFinder = partFinder;
 75  2
                 mergePool(policy, false);
 76  2
         }
 77  
 
 78  
         //---- Methods
 79  
 
 80  
         /**
 81  
          * Finds a part by its serial number in a list.
 82  
          * @param list the list to search.
 83  
          * @param serial the serial to search for.
 84  
          * @return the part index or {@code -1} if it is not contained.
 85  
          * @since 0.1.0
 86  
          */
 87  
         protected int getIndexBySerialNumber (List<? extends PartBean> list, SerialNumber serial) {
 88  2
                 for (int i = 0; i < list.size(); i++) {
 89  0
                         final PartBean partBean = list.get(i);
 90  0
                         if (partBean != null) {
 91  0
                                 if (serial.equals(partBean.getSerialNumber())) {
 92  0
                                         return i;
 93  
                                 }
 94  
                         }
 95  
                 }
 96  2
                 return -1;
 97  
         }
 98  
 
 99  
         /**
 100  
          * Merges the specified list of parts with the rules supplied.
 101  
          * @param policy the policy.
 102  
          * @param expunge indicates if unused parts are removed from the list.
 103  
          * @since 0.1.0
 104  
          */
 105  
         public void mergePool (PolicyBean policy, boolean expunge) {
 106  6
                 mergePool(this.partFinder.getParts(policy), expunge);
 107  6
         }
 108  
 
 109  
         /**
 110  
          * Merges the specified list of parts with the rules supplied.
 111  
          * @param addList elements to add/update in the pool.
 112  
          * @param expunge indicates if unused parts are removed from the list.
 113  
          * @since 0.1.0
 114  
          */
 115  
         public void mergePool (List<P> addList, boolean expunge) {
 116  
                 // first get serials of all pool-parts
 117  6
                 final Set<SerialNumber> serials = new HashSet<SerialNumber>();
 118  6
                 for (final P part : this.poolList) {
 119  0
                         serials.add(part.getSerialNumber());
 120  
                 }
 121  
 
 122  
                 // loop addList and update (i.e. serial found)
 123  
                 // or add (i.e. serial not found) its elements to pool
 124  6
                 for (final P part : addList) {
 125  2
                         final SerialNumber serial = part.getSerialNumber();
 126  2
                         final int index = getIndexBySerialNumber(this.poolList, serial);
 127  2
                         if (index != -1) {
 128  0
                                 if (serials.contains(serial)) {
 129  0
                                         LOGGER.debug("Updating [{}] [{}].", part.getClass().getName(), part);
 130  0
                                         this.poolList.set(index, part);
 131  0
                                         serials.remove(serial);
 132  
                                 }
 133  
                         } else {
 134  2
                                 LOGGER.debug("Adding [{}] [{}].", part.getClass().getName(), part);
 135  2
                                 this.poolList.add(part);
 136  
                         }
 137  2
                 }
 138  
 
 139  
                 // clean rest of elements, if wished
 140  6
                 if (expunge && !serials.isEmpty()) {
 141  0
                         for (final SerialNumber serial : serials) {
 142  0
                                 final int index = getIndexBySerialNumber(this.poolList, serial);
 143  0
                                 if (index != -1) {
 144  0
                                         LOGGER.debug(
 145  
                                                 "Removing [{}] [{}].",
 146  
                                                 this.poolList.get(index).getClass().getName(),
 147  
                                                 this.poolList.get(index)
 148  
                                         );
 149  0
                                         this.poolList.remove(index);
 150  
                                 }
 151  0
                         }
 152  
                 }
 153  6
         }
 154  
 
 155  
         /**
 156  
          * @since 0.1.0
 157  
          */
 158  
         public ObservableList<P> getPoolList () {
 159  1
                 return this.poolList;
 160  
         }
 161  
         
 162  
         //---- PartFinder
 163  
         
 164  
         /**
 165  
          * A part finder finds parts.
 166  
          * @since 0.3.0
 167  
          */
 168  
         public static interface PartFinder<P> {
 169  
                 
 170  
                 /**
 171  
                  * Returns the parts of the current type contained in policy's.
 172  
                  * @param policy the policy
 173  
                  * @return list of found parts in the policy
 174  
                  * @since 0.3.0
 175  
                  */
 176  
                 List<P> getParts (PolicyBean policy);
 177  
         }
 178  
         
 179  4
         class DefaultPartFinder implements PartFinder<P> {
 180  
                 public List<P> getParts (PolicyBean policy) {
 181  4
                         return policy.getPartsList(PolicyPartPool.this.type);
 182  
                 }
 183  
         }
 184  
         
 185  
 }