View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau;
18  
19  import static org.apache.juneau.commons.utils.ClassUtils.*;
20  import static org.apache.juneau.commons.utils.Utils.*;
21  
22  import java.util.*;
23  
24  import org.apache.juneau.annotation.*;
25  
26  /**
27   * Parent class for all non-bean filters.
28   *
29   * <p>
30   * Marshalled filters are used to control aspects of how POJOs are handled during serialization and parsing.
31   *
32   * <p>
33   * Marshalled filters are created by {@link Builder} which is the programmatic equivalent to the {@link Marshalled @Marshalled}
34   * annotation.
35   *
36   */
37  public class MarshalledFilter {
38  	/**
39  	 * Builder class.
40  	 */
41  	public static class Builder {
42  
43  		Class<?> marshalledClass;
44  
45  		Class<?> implClass;
46  		String example;
47  
48  		/**
49  		 * Constructor.
50  		 *
51  		 * @param marshalledClass The class that this filter applies to.
52  		 */
53  		protected Builder(Class<?> marshalledClass) {
54  			this.marshalledClass = marshalledClass;
55  		}
56  
57  		/**
58  		 * Applies the information in the specified list of {@link Marshalled @Marshalled} annotations to this filter.
59  		 *
60  		 * @param annotations The annotations to apply.
61  		 * @return This object.
62  		 */
63  		public Builder applyAnnotations(List<Marshalled> annotations) {
64  
65  			annotations.forEach(x -> {
66  				if (isNotVoid(x.implClass()))
67  					implClass(x.implClass());
68  				if (ne(x.example()))
69  					example(x.example());
70  			});
71  			return this;
72  		}
73  
74  		/**
75  		 * Creates a {@link MarshalledFilter} with settings in this builder class.
76  		 *
77  		 * @return A new {@link MarshalledFilter} instance.
78  		 */
79  		public MarshalledFilter build() {
80  			return new MarshalledFilter(this);
81  		}
82  
83  		/**
84  		 * POJO example in Simplified JSON format.
85  		 *
86  		 * @param value The new value for this annotation.
87  		 * @return This object.
88  		 */
89  		public Builder example(String value) {
90  			example = value;
91  			return this;
92  		}
93  
94  		/**
95  		 * Implementation class.
96  		 *
97  		 * @param value The new value for this setting.
98  		 * @return This object.
99  		 */
100 		public Builder implClass(Class<?> value) {
101 			implClass = value;
102 			return this;
103 		}
104 	}
105 
106 	/**
107 	 * Create a new instance of this POJO filter.
108 	 *
109 	 * @param <T> The POJO class being filtered.
110 	 * @param marshalledClass The POJO class being filtered.
111 	 * @return A new {@link Builder} object.
112 	 */
113 	public static <T> Builder create(Class<T> marshalledClass) {
114 		return new Builder(marshalledClass);
115 	}
116 
117 	private final Class<?> marshalledClass;
118 	private final Class<?> implClass;
119 	private final String example;
120 
121 	/**
122 	 * Constructor.
123 	 *
124 	 * @param builder The builder for this object.
125 	 */
126 	protected MarshalledFilter(Builder builder) {
127 		this.marshalledClass = builder.marshalledClass;
128 		this.implClass = builder.implClass;
129 		this.example = builder.example;
130 	}
131 
132 	/**
133 	 * Returns the example string with this class.
134 	 *
135 	 * @return The example string associated with this class, or <jk>null</jk> if no example string is associated.
136 	 */
137 	public String getExample() { return example; }
138 
139 	/**
140 	 * Returns the implementation class associated with this class.
141 	 *
142 	 * @return The implementation class associated with this class, or <jk>null</jk> if no implementation class is associated.
143 	 */
144 	public Class<?> getImplClass() { return implClass; }
145 
146 	/**
147 	 * Returns the class that this filter applies to.
148 	 *
149 	 * @return The class that this filter applies to.
150 	 */
151 	public Class<?> getMarshalledClass() { return marshalledClass; }
152 }