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.maven.api.model;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Objects;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  
28  /**
29   * Represents the source of a model input, such as a POM file.
30   * <p>
31   * This class tracks the origin of model elements, including their location in source files
32   * and relationships between imported models. It's used for error reporting and debugging
33   * to help identify where specific model elements came from.
34   *
35   * @since 4.0.0
36   */
37  public class InputSource implements Serializable {
38  
39      private final String modelId;
40      private final String location;
41      private final List<InputSource> inputs;
42      private final InputLocation importedFrom;
43  
44      public InputSource(String modelId, String location) {
45          this(modelId, location, null);
46      }
47  
48      public InputSource(String modelId, String location, InputLocation importedFrom) {
49          this.modelId = modelId;
50          this.location = location;
51          this.inputs = null;
52          this.importedFrom = importedFrom;
53      }
54  
55      public InputSource(Collection<InputSource> inputs) {
56          this.modelId = null;
57          this.location = null;
58          this.inputs = ImmutableCollections.copy(inputs);
59          this.importedFrom = null;
60      }
61  
62      /**
63       * Get the path/URL of the POM or {@code null} if unknown.
64       *
65       * @return the location
66       */
67      public String getLocation() {
68          return this.location;
69      }
70  
71      /**
72       * Get the identifier of the POM in the format {@code <groupId>:<artifactId>:<version>}.
73       *
74       * @return the model id
75       */
76      public String getModelId() {
77          return this.modelId;
78      }
79  
80      /**
81       * Gets the parent InputLocation where this InputLocation may have been imported from.
82       * Can return {@code null}.
83       *
84       * @return InputLocation
85       * @since 4.0.0
86       */
87      public InputLocation getImportedFrom() {
88          return importedFrom;
89      }
90  
91      @Override
92      public boolean equals(Object o) {
93          if (this == o) {
94              return true;
95          }
96          if (o == null || getClass() != o.getClass()) {
97              return false;
98          }
99          InputSource that = (InputSource) o;
100         return Objects.equals(modelId, that.modelId)
101                 && Objects.equals(location, that.location)
102                 && Objects.equals(inputs, that.inputs);
103     }
104 
105     @Override
106     public int hashCode() {
107         return Objects.hash(modelId, location, inputs);
108     }
109 
110     Stream<InputSource> sources() {
111         return inputs != null ? inputs.stream() : Stream.of(this);
112     }
113 
114     @Override
115     public String toString() {
116         if (inputs != null) {
117             return inputs.stream().map(InputSource::toString).collect(Collectors.joining(", ", "merged[", "]"));
118         }
119         return getModelId() + " " + getLocation();
120     }
121 
122     public static InputSource merge(InputSource src1, InputSource src2) {
123         return new InputSource(Stream.concat(src1.sources(), src2.sources()).collect(Collectors.toSet()));
124     }
125 }