Coverage Report - org.openpermis.policy.bean.basic.BasicAbstractCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
BasicAbstractCollection
62%
33/53
53%
17/32
2.615
 
 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.policy.bean.basic;
 11  
 
 12  
 import java.util.ArrayList;
 13  
 import java.util.Collection;
 14  
 import java.util.Iterator;
 15  
 import java.util.List;
 16  
 
 17  
 import org.openpermis.policy.PartProblemReporter;
 18  
 import org.openpermis.policy.PartProblemReporter.ProblemMessage;
 19  
 import org.openpermis.policy.bean.BeanCollection;
 20  
 import org.openpermis.policy.bean.PartBean;
 21  
 import org.openpermis.policy.bean.SerialNumber;
 22  
 
 23  
 
 24  
 /**
 25  
  * Abstract basic collection.
 26  
  * @param <P> generic type.
 27  
  * @since 0.3.0
 28  
  */
 29  
 public abstract class BasicAbstractCollection<P extends PartBean> 
 30  
         extends BasicPartBean
 31  
         implements BeanCollection<P>
 32  
 {
 33  
         //---- Static
 34  
         
 35  
         /**
 36  
          * @since 0.3.0
 37  
          */
 38  
         private static final long serialVersionUID = -5284544211072597574L;
 39  
 
 40  
         //---- State
 41  
         
 42  
         private List<P> collection;
 43  
         
 44  
         //---- Constructors
 45  
 
 46  
         /**
 47  
          * @since 0.3.0
 48  
          */
 49  
         protected BasicAbstractCollection (SerialNumber serialNumber) {
 50  6022
                 super(BeanCollection.class, serialNumber);
 51  6022
                 this.collection = new ArrayList<P>();
 52  6022
         }
 53  
 
 54  
         /**
 55  
          * @since 0.3.0
 56  
          */
 57  
         protected BasicAbstractCollection (SerialNumber serialNumber, Collection<P> collection) {
 58  3488
                 this(serialNumber);
 59  3488
                 if (collection != null) {
 60  2980
                         for (P p : collection) {
 61  2817
                                 if (p == null) {
 62  4
                                         throw new IllegalArgumentException("Collection contains null entries.");
 63  
                                 }
 64  
                         }
 65  2976
                         this.collection.addAll(collection);
 66  
                 }
 67  3484
         }
 68  
 
 69  
         //---- Methods
 70  
         
 71  
         /**
 72  
          * Returns the internal collection.
 73  
          * @return the internal collection.
 74  
          * @since 0.3.0
 75  
          */
 76  
         protected Collection<P> getCollection () {
 77  3
                 return this.collection;
 78  
         }
 79  
         
 80  
         /**
 81  
          * Returns an iterator for this collection.
 82  
          * @return an iterator for this collection.
 83  
          * @since 0.3.0
 84  
          */
 85  
         public Iterator<P> iterator () {
 86  4018
                 return this.collection.iterator();
 87  
         }
 88  
         
 89  
         /**
 90  
          * @since 0.3.0
 91  
          */
 92  
         public List<P> toList () {
 93  5372
                 final List<P> result = new ArrayList<P>(this.collection.size());
 94  5372
                 for (P partBean : this.collection) {
 95  4999
                         result.add(partBean);
 96  
                 }
 97  5372
                 return result;
 98  
         }
 99  
         
 100  
         /**
 101  
          * Returns true if collection type of part is an instance of this collection.
 102  
          * @param part a {@link BasicPart}.
 103  
          * @return true if collection type of part is an instance of this collection.
 104  
          * @since 0.3.0
 105  
          */
 106  
         protected abstract boolean isCollectionType (BasicPart part);
 107  
         
 108  
         //---- BasicPartBean
 109  
         
 110  
         /**
 111  
          * @since 0.3.0
 112  
          */
 113  
         protected boolean comparablePart (BasicPart part) {
 114  8668
                 return isCollectionType(part);
 115  
         }
 116  
         
 117  
         /**
 118  
          * @since 0.3.0
 119  
          */
 120  
         @Override
 121  
         @SuppressWarnings("unchecked")
 122  
         protected boolean equalPart (BasicPart part) {
 123  4334
                 final List<P> list = ((BasicAbstractCollection) part).toList();
 124  
 
 125  4334
                 for (P element : this.collection) {
 126  199
                         if (list.contains(element)) {
 127  185
                                 list.remove(element);
 128  
                         } else {
 129  14
                                 return false;
 130  
                         }
 131  
                 }
 132  4320
                 if (list.isEmpty()) {
 133  944
                         return true;
 134  
                 }
 135  3376
                 return false;
 136  
         }
 137  
 
 138  
         /**
 139  
          * @since 0.3.0
 140  
          */
 141  
         @Override
 142  
         protected int partHashCode () {
 143  104
                 int hash = 0;
 144  104
                 final Iterator<?> i = iterator();
 145  200
                 while (i.hasNext()) {
 146  96
                         final Object obj = i.next();
 147  96
                         if (obj != null) {
 148  96
                                 hash += obj.hashCode();
 149  
                         }
 150  96
                 }
 151  104
                 return hash;
 152  
         }
 153  
         
 154  
         /**
 155  
          * @since 0.3.0
 156  
          */
 157  
         @Override
 158  
         protected void appendPartDetails (StringBuilder sb) {
 159  0
                 appendDetails(sb, "size", Integer.valueOf(this.collection.size()));
 160  0
                 final StringBuilder serials = new StringBuilder();
 161  0
                 for (P partBean : this.collection) {
 162  0
                         if (serials.length() > 0) {
 163  0
                                 serials.append(", ");
 164  
                         }
 165  0
                         serials.append(partBean.getSerialNumber());
 166  
                 }
 167  0
                 appendDetails(sb, "serials", serials.toString());
 168  0
         }
 169  
         
 170  
         /**
 171  
          * @since 0.3.0
 172  
          */
 173  
         @Override
 174  
         public PartBean findBySerialNumber (SerialNumber partSerialNumber) {
 175  0
                 final PartBean superBean = super.findBySerialNumber(partSerialNumber);
 176  0
                 if (superBean != null) {
 177  0
                         return superBean;
 178  
                 }
 179  0
                 for (P part : this.collection) {
 180  0
                         if (part != null && partSerialNumber.equals(part.getSerialNumber())) {
 181  0
                                 return part;
 182  
                         }
 183  
                 }
 184  0
                 return null;
 185  
         }
 186  
 
 187  
         /**
 188  
          * @since 0.3.0
 189  
          */
 190  
         @Override
 191  
         public boolean isPartValid (PartProblemReporter reporter) {
 192  0
                 return isPartValid(reporter, false, true, false, false, false);
 193  
         }
 194  
 
 195  
         /**
 196  
          * Checks if a collection is set, elements are set and valid.
 197  
          * @param reporter reporter to use.
 198  
          * @param checkNotEmpty check if collection is empty.
 199  
          * @param checkValid check if collection's elements are valid.
 200  
          * @param checkNameUniqueness check if collection's elements are name-unique.
 201  
          * @return if all conditions are valid.
 202  
          * @since 0.3.0
 203  
          */
 204  
         private boolean isPartValid (
 205  
                 PartProblemReporter reporter,
 206  
                 boolean checkNotEmpty,
 207  
                 boolean checkValid,
 208  
                 boolean checkNameUniqueness,
 209  
                 boolean checkObjectUniqueness,
 210  
                 boolean checkIdentityUniqueness)
 211  
         {
 212  
                 boolean valid;
 213  0
                 valid = isChildCollectionValid(
 214  
                         reporter, 
 215  
                         this.collection,
 216  
                         checkNotEmpty, 
 217  
                         checkValid, 
 218  
                         checkNameUniqueness, 
 219  
                         checkObjectUniqueness, 
 220  
                         checkIdentityUniqueness
 221  
                 );
 222  0
                 if (!valid) {
 223  0
                         reportProblem(reporter, ProblemMessage.illegalCollection);
 224  
                 }
 225  0
                 return valid;
 226  
         }
 227  
         
 228  
 }