com.hotels.plunger.DataBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nexusreleasetest Show documentation
Show all versions of nexusreleasetest Show documentation
Framework to simplify testing Cascading applications
The newest version!
/**
* Copyright 2015 Expedia Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hotels.plunger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import cascading.tuple.Fields;
import cascading.tuple.FieldsResolverException;
import cascading.tuple.Tuple;
import cascading.tuple.TupleEntry;
import cascading.tuple.Tuples;
/**
* Convenience class for building {@link Tuple} based input data for {@link PlungerFlow} based cascading unit tests.
*
* @see {@link Data}, {@link TupleListTap}.
*/
public class DataBuilder {
private final List list;
private final Fields fields;
private final Class>[] types;
private TupleEntry tupleEntry;
private Fields fieldMask;
/**
* Constructs a new tuple source builder that will provide {@link Tuple Tuples} that contain values consistent with
* the declared {@link Fields}.
*/
public DataBuilder(Fields fields) {
this(fields, fields.getTypesClasses());
}
/**
* Constructs a new tuple source builder that will provide {@link Tuple Tuples} that contain values consistent with
* the declared {@link Fields}. Adding tuples will also coerce types.
*/
public DataBuilder(Fields fields, Class>[] types) {
this.fields = fields;
fieldMask = fields;
this.types = types;
list = new ArrayList();
if (types != null && types.length != fields.size()) {
throw new IllegalArgumentException("There must be the same number of types as fields");
}
}
/** Creates a new {@link Tuple} with {@code null} values for each field. */
public DataBuilder newTuple() {
flushTupleEntry();
tupleEntry = new TupleEntry(fields, new Tuple(new Object[fields.size()]));
return this;
}
/** Creates a new {@link Tuple} with the specified values. */
public DataBuilder addTuple(Object... values) {
newTuple();
setTuple(values);
return this;
}
/** Creates a new {@link Tuple} with the specified values. */
public DataBuilder addTuple(Tuple tuple) {
newTuple();
List