All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openlca.git.util.FieldDefinition Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
package org.openlca.git.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class FieldDefinition {

	final Type type;
	String name;
	final List fields;
	final Function converter;
	String condition;
	List conditionFields;
	Function meetsCondition;

	public static FieldDefinition firstOf(String field) {
		return new FieldDefinition(Type.FIRST, field, null);
	}

	public static FieldDefinition firstOf(String field, Function converter) {
		return new FieldDefinition(Type.FIRST, field, converter);
	}

	public static FieldDefinition allOf(String field) {
		return new FieldDefinition(Type.ALL, field, null);
	}

	public static FieldDefinition allOf(String field, Function converter) {
		return new FieldDefinition(Type.ALL, field, converter);
	}

	public FieldDefinition name(String name) {
		this.name = name;
		return this;
	}

	public FieldDefinition ifIs(String condition) {
		return ifIs(condition, null);
	}

	public FieldDefinition ifIsNot(String condition) {
		return ifIs(condition, s -> !Boolean.parseBoolean(s));
	}


	public FieldDefinition ifHas(String condition) {
		return ifIs(condition, s -> s != null);
	}
	
	public FieldDefinition ifIs(String condition, Function converter) {
		this.condition = condition;
		this.conditionFields = condition != null ? Arrays.asList(condition.split("\\.")) : new ArrayList<>();
		this.meetsCondition = converter != null ? converter : s -> Boolean.parseBoolean(s);
		return this;
	}

	private FieldDefinition(Type type, String name, Function converter) {
		this.type = type;
		this.name = name;
		this.fields = Arrays.asList(name.split("\\."));
		this.converter = converter != null ? converter : s -> s;
	}

	boolean isConditional() {
		return conditionFields != null && !conditionFields.isEmpty();
	}

	static enum Type {
		FIRST, ALL;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy