@@ -112,6 +112,64 @@ data class Environment(
112112 " GOPATH"
113113 )
114114 }
115+
116+ /* *
117+ * Merge this [Environment] with the given [other] [Environment].
118+ *
119+ * Both [Environment]s must have the same properties except for [variables] and [toolVersions], otherwise an
120+ * [IllegalArgumentException] is thrown.
121+ */
122+ operator fun plus (other : Environment ) =
123+ Environment (
124+ ortVersion = ortVersion.also {
125+ require(it == other.ortVersion) {
126+ " Cannot merge Environments with different ORT versions: '$ortVersion ' != '${other.ortVersion} '."
127+ }
128+ },
129+ buildJdk = buildJdk.also {
130+ require(it == other.buildJdk) {
131+ " Cannot merge Environments with different build JDKs: '$buildJdk ' != '${other.buildJdk} '."
132+ }
133+ },
134+ javaVersion = javaVersion.also {
135+ require(it == other.javaVersion) {
136+ " Cannot merge Environments with different Java versions: '$javaVersion ' != '${other.javaVersion} '."
137+ }
138+ },
139+ os = os.also {
140+ require(it == other.os) {
141+ " Cannot merge Environments with different operating systems: '$os ' != '${other.os} '."
142+ }
143+ },
144+ processors = processors.also {
145+ require(it == other.processors) {
146+ " Cannot merge Environments with different processor counts: '$processors ' != '${other.processors} '."
147+ }
148+ },
149+ maxMemory = maxMemory.also {
150+ require(it == other.maxMemory) {
151+ " Cannot merge Environments with different memory amounts: '$maxMemory ' != '${other.maxMemory} '."
152+ }
153+ },
154+ variables = run {
155+ val keyIntersection by lazy { variables.keys.intersect(other.variables.keys) }
156+
157+ require(variables == other.variables || keyIntersection.isEmpty()) {
158+ " Cannot merge Environments with conflicting variable keys: $keyIntersection "
159+ }
160+
161+ variables + other.variables
162+ },
163+ toolVersions = run {
164+ val keyIntersection by lazy { toolVersions.keys.intersect(other.toolVersions.keys) }
165+
166+ require(toolVersions == other.toolVersions || keyIntersection.isEmpty()) {
167+ " Cannot merge Environments with conflicting tool versions keys: $keyIntersection "
168+ }
169+
170+ toolVersions + other.toolVersions
171+ }
172+ )
115173}
116174
117175/* *
0 commit comments