View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper.beans;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static com.google.common.base.Preconditions.checkState;
19  import static java.lang.String.format;
20  
21  import org.basepom.mojo.propertyhelper.TransformerRegistry;
22  
23  import java.io.File;
24  import java.util.Objects;
25  import java.util.Optional;
26  
27  import com.google.common.annotations.VisibleForTesting;
28  
29  public abstract class AbstractDefinition<T extends AbstractDefinition<T>> {
30  
31      /**
32       * Name of the build property to define. Field injected by Maven.
33       */
34      String id = null;
35  
36      /**
37       * True skips the parsing of this definition. Field injected by Maven.
38       */
39      boolean skip = false;
40  
41      /**
42       * Whether to export this number directly. Field injected by Maven.
43       */
44      boolean export = false;
45  
46      /**
47       * Name of the property from the properties file. Field injected by Maven.
48       */
49      String propertyName = null;
50  
51      /**
52       * Name of the properties file to persist the count. Field injected by Maven.
53       */
54      File propertyFile = null;
55  
56      /**
57       * What to do when the property is missing from the file. Field injected by Maven.
58       */
59      String onMissingFile = "fail";
60  
61      /**
62       * What to do when the property is missing from the file. Field injected by Maven.
63       */
64      String onMissingProperty = "fail";
65  
66      /**
67       * The initial value for this field. Field injected by Maven.
68       */
69      String initialValue = null;
70  
71      /**
72       * Format for this element. Field injected by Maven.
73       */
74      String format = null;
75  
76      /**
77       * Comma separated list of String transformers.
78       */
79      String transformers = null;
80  
81      protected AbstractDefinition() {
82      }
83  
84      public String getId() {
85          return id;
86      }
87  
88      @SuppressWarnings("unchecked")
89      @VisibleForTesting
90      public T setId(final String id) {
91          this.id = checkNotNull(id, "id is null").trim();
92          return (T) this;
93      }
94  
95      public boolean isSkip() {
96          return skip;
97      }
98  
99      @SuppressWarnings("unchecked")
100     @VisibleForTesting
101     public T setSkip(final boolean skip) {
102         this.skip = skip;
103         return (T) this;
104     }
105 
106     public Optional<String> getTransformers() {
107         return Optional.ofNullable(transformers);
108     }
109 
110     public Optional<String> getInitialValue() {
111         return Optional.ofNullable(initialValue);
112     }
113 
114     @SuppressWarnings("unchecked")
115     @VisibleForTesting
116     public T setInitialValue(final String initialValue) {
117         checkState(initialValue != null, "initialValue is null");
118         this.initialValue = initialValue.trim();
119         return (T) this;
120     }
121 
122     public boolean isExport() {
123         return export;
124     }
125 
126     @SuppressWarnings("unchecked")
127     @VisibleForTesting
128     public T setExport(final boolean export) {
129         this.export = export;
130         return (T) this;
131     }
132 
133     public String getPropertyName() {
134         return Objects.requireNonNullElse(propertyName, id);
135     }
136 
137     @SuppressWarnings("unchecked")
138     @VisibleForTesting
139     public T setPropertyName(final String propertyName) {
140         this.propertyName = checkNotNull(propertyName, "propertyName is null").trim();
141         return (T) this;
142     }
143 
144     public Optional<File> getPropertyFile() {
145         return Optional.ofNullable(propertyFile);
146     }
147 
148     @SuppressWarnings("unchecked")
149     @VisibleForTesting
150     public T setPropertyFile(final File propertyFile) {
151         this.propertyFile = checkNotNull(propertyFile, "propertyFile is null");
152         return (T) this;
153     }
154 
155     public IgnoreWarnFailCreate getOnMissingFile() {
156         return IgnoreWarnFailCreate.forString(onMissingFile);
157     }
158 
159     @SuppressWarnings("unchecked")
160     @VisibleForTesting
161     public T setOnMissingFile(final String onMissingFile) {
162         IgnoreWarnFailCreate.forString(onMissingFile);
163         this.onMissingFile = onMissingFile;
164         return (T) this;
165     }
166 
167     public IgnoreWarnFailCreate getOnMissingProperty() {
168         return IgnoreWarnFailCreate.forString(onMissingProperty);
169     }
170 
171     @SuppressWarnings("unchecked")
172     @VisibleForTesting
173     public T setOnMissingProperty(final String onMissingProperty) {
174         IgnoreWarnFailCreate.forString(onMissingProperty);
175         this.onMissingProperty = onMissingProperty;
176         return (T) this;
177     }
178 
179     public Optional<String> formatResult(final String value) {
180         final Optional<String> format = getFormat();
181         String res = format.isPresent() ? format(format.get(), value) : value;
182 
183         res = TransformerRegistry.applyTransformers(transformers, res);
184 
185         return Optional.ofNullable(res);
186     }
187 
188     public Optional<String> getFormat() {
189         return Optional.ofNullable(format);
190     }
191 
192     @SuppressWarnings("unchecked")
193     @VisibleForTesting
194     public T setFormat(final String format) {
195         this.format = checkNotNull(format, "format is null");
196         return (T) this;
197     }
198 
199     public void check() {
200         checkState(id != null, "the id element must not be empty!");
201     }
202 }