1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.api;
20
21 import java.nio.file.Path;
22 import java.time.Instant;
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import org.apache.maven.api.annotations.Experimental;
27 import org.apache.maven.api.annotations.Nonnull;
28 import org.apache.maven.api.annotations.Nullable;
29 import org.apache.maven.api.annotations.ThreadSafe;
30
31 import static java.util.Objects.requireNonNull;
32
33
34
35
36
37
38 @Experimental
39 @ThreadSafe
40 public interface ProtoSession {
41
42
43
44
45
46
47
48 @Nonnull
49 Map<String, String> getUserProperties();
50
51
52
53
54
55
56
57
58 @Nonnull
59 Map<String, String> getSystemProperties();
60
61
62
63
64
65
66 @Nonnull
67 Instant getStartTime();
68
69
70
71
72
73
74
75
76
77 @Nonnull
78 Path getTopDirectory();
79
80
81
82
83
84
85
86
87
88
89 @Nonnull
90 Path getRootDirectory();
91
92
93
94
95 @Nonnull
96 default Builder toBuilder() {
97 try {
98 return new Builder(
99 getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), getRootDirectory());
100 } catch (IllegalStateException e) {
101 return new Builder(getUserProperties(), getSystemProperties(), getStartTime(), getTopDirectory(), null);
102 }
103 }
104
105
106
107
108 static Builder newBuilder() {
109 return new Builder().withStartTime(MonotonicClock.now());
110 }
111
112 class Builder {
113 private Map<String, String> userProperties;
114 private Map<String, String> systemProperties;
115 private Instant startTime;
116 private Path topDirectory;
117 private Path rootDirectory;
118
119 private Builder() {}
120
121 private Builder(
122 Map<String, String> userProperties,
123 Map<String, String> systemProperties,
124 Instant startTime,
125 Path topDirectory,
126 Path rootDirectory) {
127 this.userProperties = userProperties;
128 this.systemProperties = systemProperties;
129 this.startTime = startTime;
130 this.topDirectory = topDirectory;
131 this.rootDirectory = rootDirectory;
132 }
133
134 public Builder withUserProperties(@Nonnull Map<String, String> userProperties) {
135 this.userProperties = new HashMap<>(userProperties);
136 return this;
137 }
138
139 public Builder withSystemProperties(@Nonnull Map<String, String> systemProperties) {
140 this.systemProperties = new HashMap<>(systemProperties);
141 return this;
142 }
143
144 public Builder withStartTime(@Nonnull Instant startTime) {
145 this.startTime = requireNonNull(startTime, "startTime");
146 return this;
147 }
148
149 public Builder withTopDirectory(@Nonnull Path topDirectory) {
150 this.topDirectory = requireNonNull(topDirectory, "topDirectory");
151 return this;
152 }
153
154 public Builder withRootDirectory(@Nullable Path rootDirectory) {
155 this.rootDirectory = rootDirectory;
156 return this;
157 }
158
159 public ProtoSession build() {
160 return new Impl(userProperties, systemProperties, startTime, topDirectory, rootDirectory);
161 }
162
163 private static class Impl implements ProtoSession {
164 private final Map<String, String> userProperties;
165 private final Map<String, String> systemProperties;
166 private final Instant startTime;
167 private final Path topDirectory;
168 private final Path rootDirectory;
169
170 private Impl(
171 Map<String, String> userProperties,
172 Map<String, String> systemProperties,
173 Instant startTime,
174 Path topDirectory,
175 Path rootDirectory) {
176 this.userProperties = requireNonNull(userProperties);
177 this.systemProperties = requireNonNull(systemProperties);
178 this.startTime = requireNonNull(startTime);
179 this.topDirectory = requireNonNull(topDirectory);
180 this.rootDirectory = rootDirectory;
181 }
182
183 @Override
184 public Map<String, String> getUserProperties() {
185 return userProperties;
186 }
187
188 @Override
189 public Map<String, String> getSystemProperties() {
190 return systemProperties;
191 }
192
193 @Override
194 public Instant getStartTime() {
195 return startTime;
196 }
197
198 @Override
199 public Path getTopDirectory() {
200 return topDirectory;
201 }
202
203 @Override
204 public Path getRootDirectory() {
205 if (rootDirectory == null) {
206 throw new IllegalStateException("root directory not set");
207 }
208 return rootDirectory;
209 }
210 }
211 }
212 }