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  
37      public InputSource(String modelId, String location) {
38          this.modelId = modelId;
39          this.location = location;
40          this.inputs = null;
41      }
42  
43      public InputSource(Collection<InputSource> inputs) {
44          this.modelId = null;
45          this.location = null;
46          this.inputs = ImmutableCollections.copy(inputs);
47      }
48  
49      /**
50       * Get the path/URL of the POM or {@code null} if unknown.
51       *
52       * @return the location
53       */
54      public String getLocation() {
55          return this.location;
56      }
57  
58      /**
59       * Get the identifier of the POM in the format {@code <groupId>:<artifactId>:<version>}.
60       *
61       * @return the model id
62       */
63      public String getModelId() {
64          return this.modelId;
65      }
66  
67      @Override
68      public boolean equals(Object o) {
69          if (this == o) {
70              return true;
71          }
72          if (o == null || getClass() != o.getClass()) {
73              return false;
74          }
75          InputSource that = (InputSource) o;
76          return Objects.equals(modelId, that.modelId)
77                  && Objects.equals(location, that.location)
78                  && Objects.equals(inputs, that.inputs);
79      }
80  
81      @Override
82      public int hashCode() {
83          return Objects.hash(modelId, location, inputs);
84      }
85  
86      Stream<InputSource> sources() {
87          return inputs != null ? inputs.stream() : Stream.of(this);
88      }
89  
90      @Override
91      public String toString() {
92          if (inputs != null) {
93              return inputs.stream().map(InputSource::toString).collect(Collectors.joining(", ", "merged[", "]"));
94          }
95          return getModelId() + " " + getLocation();
96      }
97  
98      public static InputSource merge(InputSource src1, InputSource src2) {
99          return new InputSource(Stream.concat(src1.sources(), src2.sources()).collect(Collectors.toSet()));
100     }
101 }