Coverage Report - org.apache.creadur.whisker.model.Descriptor
 
Classes in this File Line Coverage Branch Coverage Complexity
Descriptor
59%
29/49
53%
16/30
1.45
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *   http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied.  See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.creadur.whisker.model;
 20  
 
 21  
 import java.util.Collection;
 22  
 import java.util.Map;
 23  
 import java.util.Set;
 24  
 
 25  
 /**
 26  
  * High level description of licensing qualities.
 27  
  */
 28  
 public class Descriptor {
 29  
 
 30  
     /** Principle license for main work. */
 31  
     private final License primaryLicense;
 32  
     /** Optional additional primary copyright notice*/
 33  
     private final String primaryCopyrightNotice;
 34  
     /** Individual or group with main responsible for main work. */
 35  
     private final String primaryOrganisationId;
 36  
     /** A NOTICE for the main work, for inclusion alongside the LICENSE. */
 37  
     private final String primaryNotice;
 38  
     /** License meta-data, indexed by id. */
 39  
     private final Map<String, License> licenses;
 40  
     /** Notice meta-data, indexed by id. */
 41  
     private final Map<String, String> notices;
 42  
     /** Directories expected to be contained within the release. */
 43  
     private final Collection<WithinDirectory> contents;
 44  
 
 45  
     /**
 46  
      * Constructs a description of the expected licensing qualities of a
 47  
      * distribution, with no additional primary copyright notice.
 48  
      * 
 49  
      * @param primaryLicense
 50  
      *            not null
 51  
      * @param primaryOrganisationId
 52  
      *            not null
 53  
      * @param primaryNotice
 54  
      *            possibly null
 55  
      * @param licenses
 56  
      *            not null, possibly empty
 57  
      * @param notices
 58  
      *            not null, possibly empty
 59  
      * @param organisations
 60  
      *            not null, possibly empty
 61  
      * @param contents
 62  
      *            not null, possibly empty
 63  
      */
 64  
     public Descriptor(final License primaryLicense,
 65  
             final String primaryOrganisationId, 
 66  
             final String primaryNotice,
 67  
             final Map<String, License> licenses,
 68  
             final Map<String, String> notices,
 69  
             final Map<String, Organisation> organisations,
 70  
             final Collection<WithinDirectory> contents) {
 71  15
         this(   primaryLicense,
 72  
                 null,
 73  
                 primaryOrganisationId,
 74  
                 primaryNotice,
 75  
                 licenses,
 76  
                 notices,
 77  
                 organisations, 
 78  
                 contents);
 79  15
     }
 80  
     
 81  
     /**
 82  
      * Constructs a description of the expected licensing qualities of a
 83  
      * distribution, with a primary additional copyright notice.
 84  
      * 
 85  
      * @param primaryLicense
 86  
      *            not null
 87  
      * @param primaryCopyrightNotice 
 88  
      *            optional primary copyright notice, possibly null
 89  
      *            
 90  
      * @param primaryOrganisationId
 91  
      *            not null
 92  
      * @param primaryNotice
 93  
      *            possibly null
 94  
      * @param licenses
 95  
      *            not null, possibly empty
 96  
      * @param notices
 97  
      *            not null, possibly empty
 98  
      * @param organisations
 99  
      *            not null, possibly empty
 100  
      * @param contents
 101  
      *            not null, possibly empty
 102  
      */
 103  
     public Descriptor(final License primaryLicense,
 104  
             final String primaryCopyrightNotice,
 105  
             final String primaryOrganisationId, 
 106  
             final String primaryNotice,
 107  
             final Map<String, License> licenses,
 108  
             final Map<String, String> notices,
 109  
             final Map<String, Organisation> organisations,
 110  
             final Collection<WithinDirectory> contents) {
 111  15
         super();
 112  15
         this.primaryLicense = primaryLicense;
 113  15
         this.primaryCopyrightNotice = primaryCopyrightNotice;
 114  15
         this.primaryOrganisationId = primaryOrganisationId;
 115  15
         this.primaryNotice = primaryNotice;
 116  15
         this.licenses = licenses;
 117  15
         this.notices = notices;
 118  15
         this.contents = contents;
 119  15
     }
 120  
 
 121  
     /**
 122  
      * Gets an additional copyright notice needed 
 123  
      * for some primary licenses.
 124  
      * @return optional primary copyright notice,
 125  
      * possibly null
 126  
      */
 127  
     public String getPrimaryCopyrightNotice() {
 128  0
         return primaryCopyrightNotice;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Is there a primary copyright notice?
 133  
      * @return true if a primary copyright notice
 134  
      * has been set, false otherwise
 135  
      */
 136  
     public boolean isPrimaryCopyrightNotice() {
 137  0
         return primaryCopyrightNotice != null;
 138  
     }
 139  
     
 140  
     /**
 141  
      * Gets the principle NOTICE for the main work.
 142  
      * 
 143  
      * @return the primaryNotice
 144  
      */
 145  
     public String getPrimaryNotice() {
 146  0
         return this.primaryNotice;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Collates NOTICE meta-data for resources.
 151  
      * 
 152  
      * @return not null, possibly empty
 153  
      */
 154  
     public Map<String, Collection<Resource>> getResourceNotices() {
 155  9
         final NoticeCollator collator = new NoticeCollator();
 156  9
         traverse(collator);
 157  9
         return collator.resourceNotices(this.notices);
 158  
     }
 159  
 
 160  
     /**
 161  
      * Collates NOTICE meta-data not linked to any resource.
 162  
      * 
 163  
      * @return not null, possibly empty
 164  
      */
 165  
     public Set<String> getOtherNotices() {
 166  0
         final NoticeCollator collator = new NoticeCollator();
 167  0
         traverse(collator);
 168  0
         return collator.notices(this.notices);
 169  
     }
 170  
 
 171  
     /**
 172  
      * Gets the license with the given id.
 173  
      * 
 174  
      * @param id
 175  
      *            not null
 176  
      * @return the license with the given id, or null
 177  
      */
 178  
     public License license(final String id) {
 179  0
         return this.licenses.get(id);
 180  
     }
 181  
 
 182  
     /**
 183  
      * Gets the principle license under which the work is licensed.
 184  
      * 
 185  
      * @return the principle license, not null
 186  
      */
 187  
     public License getPrimaryLicense() {
 188  0
         return this.primaryLicense;
 189  
     }
 190  
 
 191  
     /**
 192  
      * Gets the contents expected in the distribution.
 193  
      * 
 194  
      * @return not null, possibly null
 195  
      */
 196  
     public Collection<WithinDirectory> getContents() {
 197  9
         return this.contents;
 198  
     }
 199  
 
 200  
     /**
 201  
      * Is the given license the principle license for the main work?
 202  
      * 
 203  
      * @param license
 204  
      *            not null
 205  
      * @return true when the given license is the primary license, not null
 206  
      */
 207  
     public boolean isPrimary(final License license) {
 208  0
         return this.primaryLicense.equals(license);
 209  
     }
 210  
 
 211  
     /**
 212  
      * Is the given individual or group the principle organisation with
 213  
      * responsibility for the main work.
 214  
      * 
 215  
      * @param byOrganisation
 216  
      *            not null
 217  
      * @return true when the given organisation is primary
 218  
      */
 219  
     public boolean isPrimary(final ByOrganisation byOrganisation) {
 220  0
         return byOrganisation.getId().equals(this.primaryOrganisationId);
 221  
     }
 222  
 
 223  
     /**
 224  
      * Is this directory expected to contain only material with the primary
 225  
      * license?
 226  
      * 
 227  
      * @param directory
 228  
      *            not null
 229  
      * @return true when the directory contains only material with the primary
 230  
      *         license, false otherwise
 231  
      */
 232  
     public boolean isOnlyPrimary(final WithinDirectory directory) {
 233  0
         final LicenseAndOrganisationCollator collator = new LicenseAndOrganisationCollator();
 234  0
         directory.accept(collator);
 235  0
         return collator.isOnlyLicense(getPrimaryLicense())
 236  
                 && collator.isOnlyOrganisation(this.primaryOrganisationId);
 237  
     }
 238  
 
 239  
     /**
 240  
      * Is this collection of resources licensed under the primary license by the
 241  
      * primary organisation?
 242  
      * 
 243  
      * @param license
 244  
      *            not null
 245  
      * @return true when the contents are all licensed under the primary license
 246  
      *         by the primary organisation
 247  
      */
 248  
     public boolean isOnlyPrimary(final WithLicense license) {
 249  0
         final LicenseAndOrganisationCollator collator = new LicenseAndOrganisationCollator();
 250  0
         license.accept(collator);
 251  0
         return collator.isOnlyLicense(getPrimaryLicense())
 252  
                 && collator.isOnlyOrganisation(this.primaryOrganisationId);
 253  
     }
 254  
 
 255  
     /**
 256  
      * Traverses the content directories.
 257  
      * 
 258  
      * @param visitor
 259  
      *            possibly null
 260  
      */
 261  
     public void traverse(final Visitor visitor) {
 262  9
         for (final WithinDirectory directory : getContents()) {
 263  3
             directory.accept(visitor);
 264  
         }
 265  9
     }
 266  
 
 267  
     /**
 268  
      * Traverses the given directory.
 269  
      * 
 270  
      * @param visitor
 271  
      *            possibly null
 272  
      * @param directoryName
 273  
      *            not null
 274  
      */
 275  
     public void traverseDirectory(final Visitor visitor,
 276  
             final String directoryName) {
 277  0
         for (final WithinDirectory directory : getContents()) {
 278  0
             if (directory.isNamed(directoryName)) {
 279  0
                 directory.accept(visitor);
 280  
             }
 281  
         }
 282  0
     }
 283  
 
 284  
     /**
 285  
      * Is a NOTICE document required?
 286  
      * 
 287  
      * @return true when a NOTICE is required, false otherwise
 288  
      */
 289  
     public boolean isNoticeRequired() {
 290  12
         return primaryNoticeExists() || resourceNoticesExist();
 291  
     }
 292  
 
 293  
     /**
 294  
      * Does any resource have a required notice?
 295  
      * 
 296  
      * @return true when at least one required third party notice exists, false
 297  
      *         otherwise
 298  
      */
 299  
     public boolean resourceNoticesExist() {
 300  9
         return !getResourceNotices().isEmpty();
 301  
     }
 302  
 
 303  
     /**
 304  
      * Does the work described have a primary notice?
 305  
      * 
 306  
      * @return true unless the primary notice is null or whitespace
 307  
      */
 308  
     public boolean primaryNoticeExists() {
 309  12
         return (this.primaryNotice != null)
 310  
                 && !"".equals(this.primaryNotice.trim());
 311  
     }
 312  
 
 313  
     /**
 314  
      * Is this the work of the primary organisation only?
 315  
      * 
 316  
      * @return true when no third party resources are contained, false when
 317  
      *         third party resources exist. In particular, true when
 318  
      *         contents are empty.
 319  
      */
 320  
     public boolean isPrimaryOnly() {
 321  
         final boolean result;
 322  3
         if (contents.size() > 0) {
 323  2
             final LicenseAndOrganisationCollator collator = new LicenseAndOrganisationCollator();
 324  2
             for (final WithinDirectory directory : contents) {
 325  3
                 directory.accept(collator);
 326  
             }
 327  2
             result = collator.isOnlyOrganisation(primaryOrganisationId);
 328  2
         } else {
 329  1
             result = true;
 330  
         }
 331  3
         return result;
 332  
     }
 333  
 }