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   * Class InputSource.
30   */
31  public class InputSource implements Serializable {
32  
33      private final String modelId;
34      private final String location;
35      private final List<InputSource> inputs;
36      private final InputLocation importedFrom;
37  
38      public InputSource(String modelId, String location) {
39          this(modelId, location, null);
40      }
41  
42      public InputSource(String modelId, String location, InputLocation importedFrom) {
43          this.modelId = modelId;
44          this.location = location;
45          this.inputs = null;
46          this.importedFrom = importedFrom;
47      }
48  
49      public InputSource(Collection<InputSource> inputs) {
50          this.modelId = null;
51          this.location = null;
52          this.inputs = ImmutableCollections.copy(inputs);
53          this.importedFrom = null;
54      }
55  
56      /**
57       * Get the path/URL of the POM or {@code null} if unknown.
58       *
59       * @return the location
60       */
61      public String getLocation() {
62          return this.location;
63      }
64  
65      /**
66       * Get the identifier of the POM in the format {@code <groupId>:<artifactId>:<version>}.
67       *
68       * @return the model id
69       */
70      public String getModelId() {
71          return this.modelId;
72      }
73  
74      /**
75       * Gets the parent InputLocation where this InputLocation may have been imported from.
76       * Can return {@code null}.
77       *
78       * @return InputLocation
79       * @since 4.0.0
80       */
81      public InputLocation getImportedFrom() {
82          return importedFrom;
83      }
84  
85      @Override
86      public boolean equals(Object o) {
87          if (this == o) {
88              return true;
89          }
90          if (o == null || getClass() != o.getClass()) {
91              return false;
92          }
93          InputSource that = (InputSource) o;
94          return Objects.equals(modelId, that.modelId)
95                  && Objects.equals(location, that.location)
96                  && Objects.equals(inputs, that.inputs);
97      }
98  
99      @Override
100     public int hashCode() {
101         return Objects.hash(modelId, location, inputs);
102     }
103 
104     Stream<InputSource> sources() {
105         return inputs != null ? inputs.stream() : Stream.of(this);
106     }
107 
108     @Override
109     public String toString() {
110         if (inputs != null) {
111             return inputs.stream().map(InputSource::toString).collect(Collectors.joining(", ", "merged[", "]"));
112         }
113         return getModelId() + " " + getLocation();
114     }
115 
116     public static InputSource merge(InputSource src1, InputSource src2) {
117         return new InputSource(Stream.concat(src1.sources(), src2.sources()).collect(Collectors.toSet()));
118     }
119 }