001/*
002 * Copyright (c) 2007-2022 The Cascading Authors. All Rights Reserved.
003 *
004 * Project and contact information: https://cascading.wensel.net/
005 *
006 * This file is part of the Cascading project.
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package cascading.nested.json;
022
023import java.beans.ConstructorProperties;
024import java.util.Collections;
025import java.util.Map;
026
027import cascading.nested.core.NestedCreateFunction;
028import cascading.operation.SerPredicate;
029import cascading.tuple.Fields;
030import com.fasterxml.jackson.databind.JsonNode;
031import com.fasterxml.jackson.databind.node.ArrayNode;
032
033/**
034 * Class JSONCreateFunction provides for the ability to simply set multiple tuple values onto a new JSON object.
035 * <p>
036 * If a {@link SerPredicate} is supplied, it will be tested on the current value, if {@code true}, the value
037 * will be set on the final JSON object. This is useful for prevent null values from being set, artificially
038 * increasing the size of the final object.
039 *
040 * @see <a href=https://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-03">draft-ietf-appsawg-json-pointer-03</a>
041 * @see NestedCreateFunction for more details.
042 */
043public class JSONCreateFunction extends NestedCreateFunction<JsonNode, ArrayNode>
044  {
045  /**
046   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
047   * a new JSON object, where all the JSON object attributes are the argument field names.
048   *
049   * @param fieldDeclaration of Fields
050   */
051  @ConstructorProperties({"fieldDeclaration"})
052  public JSONCreateFunction( Fields fieldDeclaration )
053    {
054    super( JSONCoercibleType.TYPE, fieldDeclaration );
055    }
056
057  /**
058   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
059   * a new JSON object, where all the JSON object attributes are the argument field names.
060   * <p>
061   * The {@code rootPointer} values specifies the base name of the final pointer path. If {@code rootPointer} is
062   * "/person" and and argument is passed with field name "fullName", the value will be placed in "/person/fullName".
063   *
064   * @param fieldDeclaration of Fields
065   * @param rootPointer      of String
066   */
067  @ConstructorProperties({"fieldDeclaration", "rootPointer"})
068  public JSONCreateFunction( Fields fieldDeclaration, String rootPointer )
069    {
070    super( JSONCoercibleType.TYPE, fieldDeclaration, rootPointer );
071    }
072
073  /**
074   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
075   * a new JSON object, where all the JSON object attributes are the argument field names.
076   *
077   * @param coercibleType    of JSONCoercibleType
078   * @param fieldDeclaration of Fields
079   */
080  @ConstructorProperties({"coercibleType", "fieldDeclaration"})
081  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration )
082    {
083    super( coercibleType, fieldDeclaration );
084    }
085
086  /**
087   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
088   * a new JSON object, where all the JSON object attributes are the argument field names.
089   * <p>
090   * The {@code rootPointer} values specifies the base name of the final pointer path. If {@code rootPointer} is
091   * "/person" and and argument is passed with field name "fullName", the value will be placed in "/person/fullName".
092   *
093   * @param coercibleType    of JSONCoercibleType
094   * @param fieldDeclaration of Fields
095   * @param rootPointer      of String
096   */
097  @ConstructorProperties({"coercibleType", "fieldDeclaration", "rootPointer"})
098  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, String rootPointer )
099    {
100    super( coercibleType, fieldDeclaration, rootPointer );
101    }
102
103  /**
104   * Creates a new JSONCreateFunction instance that maps the given {@code fromFields} value to the location
105   * declared by the {@code stringPointer}.
106   *
107   * @param fieldDeclaration of Fields
108   * @param fromFields       of Fields
109   * @param stringPointer    of String
110   */
111  @ConstructorProperties({"fieldDeclaration", "fromFields", "stringPointer"})
112  public JSONCreateFunction( Fields fieldDeclaration, Fields fromFields, String stringPointer )
113    {
114    this( fieldDeclaration, Collections.singletonMap( fromFields, stringPointer ) );
115    }
116
117  /**
118   * Creates a new JSONCreateFunction instance that maps all the Fields declared in the
119   * {@code pointerMap} to the declared path pointers.
120   *
121   * @param fieldDeclaration of Fields
122   * @param pointerMap       of Map
123   */
124  @ConstructorProperties({"fieldDeclaration", "pointerMap"})
125  public JSONCreateFunction( Fields fieldDeclaration, Map<Fields, String> pointerMap )
126    {
127    super( JSONCoercibleType.TYPE, fieldDeclaration, pointerMap );
128    }
129
130  /**
131   * Creates a new JSONCreateFunction instance that maps the given {@code fromFields} value to the location
132   * declared by the {@code stringPointer}.
133   *
134   * @param coercibleType    of JSONCoercibleType
135   * @param fieldDeclaration of Fields
136   * @param fromFields       of Fields
137   * @param stringPointer    of String
138   */
139  @ConstructorProperties({"coercibleType", "fieldDeclaration", "fromFields", "stringPointer"})
140  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, Fields fromFields, String stringPointer )
141    {
142    this( coercibleType, fieldDeclaration, Collections.singletonMap( fromFields, stringPointer ) );
143    }
144
145  /**
146   * Creates a new JSONCreateFunction instance that maps all the Fields declared in the
147   * {@code pointerMap} to the declared path pointers.
148   *
149   * @param coercibleType    of JSONCoercibleType
150   * @param fieldDeclaration of Fields
151   * @param pointerMap       of Map
152   */
153  @ConstructorProperties({"coercibleType", "fieldDeclaration", "pointerMap"})
154  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, Map<Fields, String> pointerMap )
155    {
156    super( coercibleType, fieldDeclaration, pointerMap );
157    }
158
159  /**
160   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
161   * a new JSON object, where all the JSON object attributes are the argument field names.
162   *
163   * @param fieldDeclaration   of Fields
164   * @param defaultValueFilter of Predicate
165   */
166  @ConstructorProperties({"fieldDeclaration", "defaultValueFilter"})
167  public JSONCreateFunction( Fields fieldDeclaration, SerPredicate<?> defaultValueFilter )
168    {
169    super( JSONCoercibleType.TYPE, fieldDeclaration, defaultValueFilter );
170    }
171
172  /**
173   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
174   * a new JSON object, where all the JSON object attributes are the argument field names.
175   * <p>
176   * The {@code rootPointer} values specifies the base name of the final pointer path. If {@code rootPointer} is
177   * "/person" and and argument is passed with field name "fullName", the value will be placed in "/person/fullName".
178   *
179   * @param fieldDeclaration   of Fields
180   * @param rootPointer        of String
181   * @param defaultValueFilter of Predicate
182   */
183  @ConstructorProperties({"fieldDeclaration", "rootPointer", "defaultValueFilter"})
184  public JSONCreateFunction( Fields fieldDeclaration, String rootPointer, SerPredicate<?> defaultValueFilter )
185    {
186    super( JSONCoercibleType.TYPE, fieldDeclaration, rootPointer, defaultValueFilter );
187    }
188
189  /**
190   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
191   * a new JSON object, where all the JSON object attributes are the argument field names.
192   *
193   * @param coercibleType      of JSONCoercibleType
194   * @param fieldDeclaration   of Fields
195   * @param defaultValueFilter of Predicate
196   */
197  @ConstructorProperties({"coercibleType", "fieldDeclaration", "defaultValueFilter"})
198  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, SerPredicate<?> defaultValueFilter )
199    {
200    super( coercibleType, fieldDeclaration, defaultValueFilter );
201    }
202
203  /**
204   * Creates a new JSONCreateFunction instance that will pivot all resolved arguments fields into
205   * a new JSON object, where all the JSON object attributes are the argument field names.
206   * <p>
207   * The {@code rootPointer} values specifies the base name of the final pointer path. If {@code rootPointer} is
208   * "/person" and and argument is passed with field name "fullName", the value will be placed in "/person/fullName".
209   *
210   * @param coercibleType      of JSONCoercibleType
211   * @param fieldDeclaration   of Fields
212   * @param rootPointer        of String
213   * @param defaultValueFilter of Predicate
214   */
215  @ConstructorProperties({"coercibleType", "fieldDeclaration", "rootPointer", "defaultValueFilter"})
216  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, String rootPointer, SerPredicate<?> defaultValueFilter )
217    {
218    super( coercibleType, fieldDeclaration, rootPointer, defaultValueFilter );
219    }
220
221  /**
222   * Creates a new JSONCreateFunction instance that maps the given {@code fromFields} value to the location
223   * declared by the {@code stringPointer}.
224   *
225   * @param fieldDeclaration   of Fields
226   * @param defaultValueFilter of Predicate
227   * @param fromFields         of Fields
228   * @param stringPointer      of String
229   */
230  @ConstructorProperties({"fieldDeclaration", "defaultValueFilter", "fromFields", "stringPointer"})
231  public JSONCreateFunction( Fields fieldDeclaration, SerPredicate<?> defaultValueFilter, Fields fromFields, String stringPointer )
232    {
233    this( fieldDeclaration, defaultValueFilter, Collections.singletonMap( fromFields, stringPointer ) );
234    }
235
236  /**
237   * Creates a new JSONCreateFunction instance that maps all the Fields declared in the
238   * {@code pointerMap} to the declared path pointers.
239   *
240   * @param fieldDeclaration   of Fields
241   * @param defaultValueFilter of Predicate
242   * @param pointerMap         of Map
243   */
244  @ConstructorProperties({"fieldDeclaration", "defaultValueFilter", "pointerMap"})
245  public JSONCreateFunction( Fields fieldDeclaration, SerPredicate<?> defaultValueFilter, Map<Fields, String> pointerMap )
246    {
247    super( JSONCoercibleType.TYPE, fieldDeclaration, defaultValueFilter, pointerMap );
248    }
249
250  /**
251   * Creates a new JSONCreateFunction instance that maps the given {@code fromFields} value to the location
252   * declared by the {@code stringPointer}.
253   *
254   * @param coercibleType      of JSONCoercibleType
255   * @param fieldDeclaration   of Fields
256   * @param defaultValueFilter of Predicate
257   * @param fromFields         of Fields
258   * @param stringPointer      of String
259   */
260  @ConstructorProperties({"coercibleType", "fieldDeclaration", "defaultValueFilter", "fromFields", "stringPointer"})
261  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, SerPredicate<?> defaultValueFilter, Fields fromFields, String stringPointer )
262    {
263    this( coercibleType, fieldDeclaration, defaultValueFilter, Collections.singletonMap( fromFields, stringPointer ) );
264    }
265
266  /**
267   * Creates a new JSONCreateFunction instance that maps all the Fields declared in the
268   * {@code pointerMap} to the declared path pointers.
269   *
270   * @param coercibleType      of JSONCoercibleType
271   * @param fieldDeclaration   of Fields
272   * @param defaultValueFilter of Predicate
273   * @param pointerMap         of Map
274   */
275  @ConstructorProperties({"coercibleType", "fieldDeclaration", "defaultValueFilter", "pointerMap"})
276  public JSONCreateFunction( JSONCoercibleType coercibleType, Fields fieldDeclaration, SerPredicate<?> defaultValueFilter, Map<Fields, String> pointerMap )
277    {
278    super( coercibleType, fieldDeclaration, defaultValueFilter, pointerMap );
279    }
280  }