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.model.building;
20  
21  import org.apache.maven.api.model.DependencyManagement;
22  import org.apache.maven.api.model.Model;
23  
24  /**
25   * Describes a tag used by the model builder to access a {@link ModelCache}. This interface basically aggregates a name
26   * and a class to provide some type safety when working with the otherwise untyped cache.
27   *
28   * @author Benjamin Bentmann
29   * @param <T> The type of data associated with the tag.
30   */
31  interface ModelCacheTag<T> {
32  
33      /**
34       * Gets the name of the tag.
35       *
36       * @return The name of the tag, must not be {@code null}.
37       */
38      String getName();
39  
40      /**
41       * Gets the type of data associated with this tag.
42       *
43       * @return The type of data, must not be {@code null}.
44       */
45      Class<T> getType();
46  
47      /**
48       * Creates a copy of the data suitable for storage in the cache. The original data to store can be mutated after the
49       * cache is populated but the state of the cache must not change so we need to make a copy.
50       *
51       * @param data The data to store in the cache, must not be {@code null}.
52       * @return The data being stored in the cache, never {@code null}.
53       */
54      T intoCache(T data);
55  
56      /**
57       * Creates a copy of the data suitable for retrieval from the cache. The retrieved data can be mutated after the
58       * cache is queried but the state of the cache must not change so we need to make a copy.
59       *
60       * @param data The data to retrieve from the cache, must not be {@code null}.
61       * @return The data being retrieved from the cache, never {@code null}.
62       */
63      T fromCache(T data);
64  
65      /**
66       * The tag used for the raw model without profile activation
67       */
68      ModelCacheTag<ModelData> RAW = new ModelCacheTag<ModelData>() {
69  
70          @Override
71          public String getName() {
72              return "raw";
73          }
74  
75          @Override
76          public Class<ModelData> getType() {
77              return ModelData.class;
78          }
79  
80          @Override
81          public ModelData intoCache(ModelData data) {
82              return data;
83          }
84  
85          @Override
86          public ModelData fromCache(ModelData data) {
87              return data;
88          }
89      };
90  
91      /**
92       * The tag used to denote an effective dependency management section from an imported model.
93       */
94      ModelCacheTag<DependencyManagement> IMPORT = new ModelCacheTag<DependencyManagement>() {
95  
96          @Override
97          public String getName() {
98              return "import";
99          }
100 
101         @Override
102         public Class<DependencyManagement> getType() {
103             return DependencyManagement.class;
104         }
105 
106         @Override
107         public DependencyManagement intoCache(DependencyManagement data) {
108             return data;
109         }
110 
111         @Override
112         public DependencyManagement fromCache(DependencyManagement data) {
113             return data;
114         }
115     };
116 
117     /**
118      * The tag used for the file model without profile activation
119      * @since 4.0.0
120      */
121     ModelCacheTag<Model> FILE = new ModelCacheTag<Model>() {
122         @Override
123         public String getName() {
124             return "file";
125         }
126 
127         @Override
128         public Class<Model> getType() {
129             return Model.class;
130         }
131 
132         @Override
133         public Model intoCache(Model data) {
134             return data;
135         }
136 
137         @Override
138         public Model fromCache(Model data) {
139             return data;
140         }
141     };
142 }