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.model.DependencyManagement;
22 import org.apache.maven.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 to denote raw model data.
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 Model model = (data.getModel() != null) ? data.getModel().clone() : null;
83 return new ModelData(data.getSource(), model, data.getGroupId(), data.getArtifactId(), data.getVersion());
84 }
85
86 @Override
87 public ModelData fromCache(ModelData data) {
88 return intoCache(data);
89 }
90 };
91
92 /**
93 * The tag used to denote an effective dependency management section from an imported model.
94 */
95 ModelCacheTag<DependencyManagement> IMPORT = new ModelCacheTag<DependencyManagement>() {
96
97 @Override
98 public String getName() {
99 return "import";
100 }
101
102 @Override
103 public Class<DependencyManagement> getType() {
104 return DependencyManagement.class;
105 }
106
107 @Override
108 public DependencyManagement intoCache(DependencyManagement data) {
109 return (data != null) ? data.clone() : null;
110 }
111
112 @Override
113 public DependencyManagement fromCache(DependencyManagement data) {
114 return intoCache(data);
115 }
116 };
117 }