001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau;
018
019import static org.apache.juneau.commons.utils.ClassUtils.*;
020import static org.apache.juneau.commons.utils.Utils.*;
021
022import java.util.*;
023
024import org.apache.juneau.annotation.*;
025
026/**
027 * Parent class for all non-bean filters.
028 *
029 * <p>
030 * Marshalled filters are used to control aspects of how POJOs are handled during serialization and parsing.
031 *
032 * <p>
033 * Marshalled filters are created by {@link Builder} which is the programmatic equivalent to the {@link Marshalled @Marshalled}
034 * annotation.
035 *
036 */
037public class MarshalledFilter {
038   /**
039    * Builder class.
040    */
041   public static class Builder {
042
043      Class<?> marshalledClass;
044
045      Class<?> implClass;
046      String example;
047
048      /**
049       * Constructor.
050       *
051       * @param marshalledClass The class that this filter applies to.
052       */
053      protected Builder(Class<?> marshalledClass) {
054         this.marshalledClass = marshalledClass;
055      }
056
057      /**
058       * Applies the information in the specified list of {@link Marshalled @Marshalled} annotations to this filter.
059       *
060       * @param annotations The annotations to apply.
061       * @return This object.
062       */
063      public Builder applyAnnotations(List<Marshalled> annotations) {
064
065         annotations.forEach(x -> {
066            if (isNotVoid(x.implClass()))
067               implClass(x.implClass());
068            if (ne(x.example()))
069               example(x.example());
070         });
071         return this;
072      }
073
074      /**
075       * Creates a {@link MarshalledFilter} with settings in this builder class.
076       *
077       * @return A new {@link MarshalledFilter} instance.
078       */
079      public MarshalledFilter build() {
080         return new MarshalledFilter(this);
081      }
082
083      /**
084       * POJO example in Simplified JSON format.
085       *
086       * @param value The new value for this annotation.
087       * @return This object.
088       */
089      public Builder example(String value) {
090         example = value;
091         return this;
092      }
093
094      /**
095       * Implementation class.
096       *
097       * @param value The new value for this setting.
098       * @return This object.
099       */
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}