• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.networknt.schema.resource;
17 
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.function.Consumer;
23 import java.util.function.Function;
24 import java.util.function.Predicate;
25 
26 /**
27  * Schema Mappers used to map an ID indicated by an absolute IRI to a retrieval
28  * IRI.
29  */
30 public class SchemaMappers extends ArrayList<SchemaMapper> {
31     private static final long serialVersionUID = 1L;
32 
SchemaMappers()33     public SchemaMappers() {
34         super();
35     }
36 
SchemaMappers(Collection<? extends SchemaMapper> c)37     public SchemaMappers(Collection<? extends SchemaMapper> c) {
38         super(c);
39     }
40 
SchemaMappers(int initialCapacity)41     public SchemaMappers(int initialCapacity) {
42         super(initialCapacity);
43     }
44 
builder()45     public static Builder builder() {
46         return new Builder();
47     }
48 
49     public static class Builder {
50         private SchemaMappers values = new SchemaMappers();
51 
Builder()52         public Builder() {
53         }
54 
Builder(Builder copy)55         public Builder(Builder copy) {
56             this.values.addAll(copy.values);
57         }
58 
with(Builder builder)59         public Builder with(Builder builder) {
60             if (!builder.values.isEmpty()) {
61                 this.values.addAll(builder.values);
62             }
63             return this;
64         }
65 
66         /**
67          * Customize the schema mappers.
68          *
69          * @param customizer the customizer
70          * @return the builder
71          */
values(Consumer<List<SchemaMapper>> customizer)72         public Builder values(Consumer<List<SchemaMapper>> customizer) {
73             customizer.accept(this.values);
74             return this;
75         }
76 
77         /**
78          * Adds a schema mapper.
79          *
80          * @param schemaMapper the schema mapper
81          * @return the builder
82          */
add(SchemaMapper schemaMapper)83         public Builder add(SchemaMapper schemaMapper) {
84             this.values.add(schemaMapper);
85             return this;
86         }
87 
88         /**
89          * Maps a schema given a source prefix with a replacement.
90          *
91          * @param source      the source prefix
92          * @param replacement the replacement prefix
93          * @return the builder
94          */
mapPrefix(String source, String replacement)95         public Builder mapPrefix(String source, String replacement) {
96             this.values.add(new PrefixSchemaMapper(source, replacement));
97             return this;
98         }
99 
100         /**
101          * Sets the mappings.
102          *
103          * @param mappings the mappings
104          * @return the builder
105          */
mappings(Map<String, String> mappings)106         public Builder mappings(Map<String, String> mappings) {
107             this.values.add(new MapSchemaMapper(mappings));
108             return this;
109         }
110 
111         /**
112          * Sets the function that maps the IRI to another IRI.
113          *
114          * @param mappings the mappings
115          * @return the builder
116          */
mappings(Function<String, String> mappings)117         public Builder mappings(Function<String, String> mappings) {
118             this.values.add(new MapSchemaMapper(mappings));
119             return this;
120         }
121 
122         /**
123          * Sets the function that maps the IRI to another IRI if the predicate is true.
124          *
125          * @param test     the predicate
126          * @param mappings the mappings
127          * @return the builder
128          */
mappings(Predicate<String> test, Function<String, String> mappings)129         public Builder mappings(Predicate<String> test, Function<String, String> mappings) {
130             this.values.add(new MapSchemaMapper(test, mappings));
131             return this;
132         }
133 
134         /**
135          * Builds a {@link SchemaMappers}
136          *
137          * @return the schema mappers
138          */
build()139         public SchemaMappers build() {
140             return values;
141         }
142     }
143 
144 }
145