View Javadoc
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.rat.license;
20  
21  import java.util.Objects;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.apache.rat.analysis.IHeaderMatcher;
25  
26  /**
27   * A simple implementation of ILicense.
28   */
29  class SimpleLicense implements ILicense {
30  
31      private ILicenseFamily family;
32      private IHeaderMatcher matcher;
33      private String derivedFrom;
34      private String notes;
35      private String name;
36      private String id;
37  
38      SimpleLicense(ILicenseFamily family, IHeaderMatcher matcher, String derivedFrom, String notes, String name, String id) {
39          Objects.requireNonNull(matcher, "Matcher must not be null");
40          Objects.requireNonNull(family, "Family must not be null");  
41          this.family = family;
42          this.matcher = matcher;
43          this.derivedFrom = derivedFrom;
44          this.notes = notes;
45          this.name = StringUtils.defaultIfBlank(name, family.getFamilyName());
46          this.id = StringUtils.defaultIfBlank(id, family.getFamilyCategory().trim());
47      }
48  
49      @Override
50      public String toString() {
51          return String.format( "%s:%s", getId(), getName());
52      }
53  
54      public ILicenseFamily getFamily() {
55          return family;
56      }
57  
58      public void setFamily(ILicenseFamily family) {
59          this.family = family;
60      }
61  
62      public IHeaderMatcher getMatcher() {
63          return matcher;
64      }
65  
66      public void setMatcher(IHeaderMatcher matcher) {
67          this.matcher = matcher;
68      }
69  
70      public void setDerivedFrom(String derivedFrom) {
71          this.derivedFrom = derivedFrom;
72      }
73  
74      @Override
75      public String getId() {
76          return id;
77      }
78  
79      @Override
80      public void reset() {
81          matcher.reset();
82      }
83  
84      @Override
85      public State matches(String line) {
86          return matcher.matches(line);
87      }
88      
89      @Override
90      public State finalizeState() {
91          return matcher.finalizeState();
92      }
93  
94      @Override
95      public State currentState() {
96          return matcher.currentState();
97      }
98  
99      @Override
100     public ILicenseFamily getLicenseFamily() {
101         return family;
102     }
103 
104     @Override
105     public int compareTo(ILicense other) {
106         return ILicense.getComparator().compare(this, other);
107     }
108 
109     @Override
110     public String getNotes() {
111         return notes;
112     }
113 
114     @Override
115     public String derivedFrom() {
116         return derivedFrom;
117     }
118     
119     @Override
120     public String getName() {
121         return name;
122     }
123 }