1
2
3
4
5
6
7
8
9
10
11
12
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
33
34 String id = null;
35
36
37
38
39 boolean skip = false;
40
41
42
43
44 boolean export = false;
45
46
47
48
49 String propertyName = null;
50
51
52
53
54 File propertyFile = null;
55
56
57
58
59 String onMissingFile = "fail";
60
61
62
63
64 String onMissingProperty = "fail";
65
66
67
68
69 String initialValue = null;
70
71
72
73
74 String format = null;
75
76
77
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 }