001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.transform;
014
015import java.util.*;
016
017import static org.apache.juneau.internal.ClassUtils.*;
018
019import org.apache.juneau.*;
020import org.apache.juneau.annotation.*;
021
022/**
023 * Parent class for all bean filters.
024 *
025 * <p>
026 * Bean filters are used to control aspects of how beans are handled during serialization and parsing.
027 *
028 * <p>
029 * Bean filters are created by {@link BeanFilterBuilder} which is the programmatic equivalent to the {@link Bean @Bean}
030 * annotation.
031 *
032 * <ul class='seealso'>
033 *    <li class='link'>{@doc juneau-marshall.Transforms.BeanFilters}
034 * </ul>
035 */
036public final class BeanFilter {
037
038   private final Class<?> beanClass;
039   private final Set<String> bpi, bpx, bpro, bpwo;
040   private final PropertyNamer propertyNamer;
041   private final Class<?> interfaceClass, stopClass;
042   private final boolean sortProperties, fluentSetters;
043   private final String typeName;
044   private final Class<?>[] beanDictionary;
045   private final PropertyFilter propertyFilter;
046
047   /**
048    * Constructor.
049    */
050   BeanFilter(BeanFilterBuilder<?> builder) {
051      this.beanClass = builder.beanClass;
052      this.typeName = builder.typeName;
053      this.bpi = new LinkedHashSet<>(builder.bpi);
054      this.bpx = new LinkedHashSet<>(builder.bpx);
055      this.bpro = new LinkedHashSet<>(builder.bpro);
056      this.bpwo = new LinkedHashSet<>(builder.bpwo);
057      this.interfaceClass = builder.interfaceClass;
058      this.stopClass = builder.stopClass;
059      this.sortProperties = builder.sortProperties;
060      this.fluentSetters = builder.fluentSetters;
061      this.propertyNamer = castOrCreate(PropertyNamer.class, builder.propertyNamer);
062      this.beanDictionary =
063         builder.dictionary == null
064         ? null
065         : builder.dictionary.toArray(new Class<?>[builder.dictionary.size()]);
066      this.propertyFilter =
067         builder.propertyFilter == null
068         ? PropertyFilter.DEFAULT
069         : castOrCreate(PropertyFilter.class, builder.propertyFilter);
070   }
071
072   /**
073    * Returns the bean class that this filter applies to.
074    *
075    * @return The bean class that this filter applies to.
076    */
077   public Class<?> getBeanClass() {
078      return beanClass;
079   }
080
081   /**
082    * Returns the dictionary name associated with this bean.
083    *
084    * @return The dictionary name associated with this bean, or <jk>null</jk> if no name is defined.
085    */
086   public String getTypeName() {
087      return typeName;
088   }
089
090   /**
091    * Returns the bean dictionary defined on this bean.
092    *
093    * @return The bean dictionary defined on this bean, or <jk>null</jk> if no bean dictionary is defined.
094    */
095   public Class<?>[] getBeanDictionary() {
096      return beanDictionary;
097   }
098
099   /**
100    * Returns the set and order of names of properties associated with a bean class.
101    *
102    * @return
103    *    The names of the properties associated with a bean class, or and empty set if all bean properties should
104    *    be used.
105    */
106   public Set<String> getBpi() {
107      return bpi;
108   }
109
110   /**
111    * Returns the list of properties to ignore on a bean.
112    *
113    * @return The names of the properties to ignore on a bean, or an empty set to not ignore any properties.
114    */
115   public Set<String> getBpx() {
116      return bpx;
117   }
118
119   /**
120    * Returns the list of read-only properties on a bean.
121    *
122    * @return The names of the read-only properties on a bean, or an empty set to not have any read-only properties.
123    */
124   public Set<String> getBpro() {
125      return bpro;
126   }
127
128   /**
129    * Returns the list of write-only properties on a bean.
130    *
131    * @return The names of the write-only properties on a bean, or an empty set to not have any write-only properties.
132    */
133   public Set<String> getBpwo() {
134      return bpwo;
135   }
136
137   /**
138    * Returns <jk>true</jk> if the properties defined on this bean class should be ordered alphabetically.
139    *
140    * <p>
141    * This method is only used when the {@link #getBpi()} method returns <jk>null</jk>.
142    * Otherwise, the ordering of the properties in the returned value is used.
143    *
144    * @return <jk>true</jk> if bean properties should be sorted.
145    */
146   public boolean isSortProperties() {
147      return sortProperties;
148   }
149
150   /**
151    * Returns <jk>true</jk> if we should find fluent setters.
152    *
153    * @return <jk>true</jk> if fluent setters should be found.
154    */
155   public boolean isFluentSetters() {
156      return fluentSetters;
157   }
158
159   /**
160    * Returns the {@link PropertyNamer} associated with the bean to tailor the names of bean properties.
161    *
162    * @return The property namer class, or <jk>null</jk> if no property namer is associated with this bean property.
163    */
164   public PropertyNamer getPropertyNamer() {
165      return propertyNamer;
166   }
167
168   /**
169    * Returns the interface class associated with this class.
170    *
171    * @return The interface class associated with this class, or <jk>null</jk> if no interface class is associated.
172    */
173   public Class<?> getInterfaceClass() {
174      return interfaceClass;
175   }
176
177   /**
178    * Returns the stop class associated with this class.
179    *
180    * @return The stop class associated with this class, or <jk>null</jk> if no stop class is associated.
181    */
182   public Class<?> getStopClass() {
183      return stopClass;
184   }
185
186   /**
187    * Calls the {@link PropertyFilter#readProperty(Object, String, Object)} method on the registered property filters.
188    *
189    * @param bean The bean from which the property was read.
190    * @param name The property name.
191    * @param value The value just extracted from calling the bean getter.
192    * @return The value to serialize.  Default is just to return the existing value.
193    */
194   public Object readProperty(Object bean, String name, Object value) {
195      return propertyFilter.readProperty(bean, name, value);
196   }
197
198   /**
199    * Calls the {@link PropertyFilter#writeProperty(Object, String, Object)} method on the registered property filters.
200    *
201    * @param bean The bean from which the property was read.
202    * @param name The property name.
203    * @param value The value just parsed.
204    * @return The value to serialize.  Default is just to return the existing value.
205    */
206   public Object writeProperty(Object bean, String name, Object value) {
207      return propertyFilter.writeProperty(bean, name, value);
208   }
209}