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

com.arosbio.commons.config.EnumConfig Maven / Gradle / Ivy

Go to download

Conformal AI package, including all data IO, transformations, machine learning models and predictor classes. Without inclusion of chemistry-dependent code.

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright (C) Aros Bio AB.
 *
 * CPSign is an Open Source Software that is dual licensed to allow you to choose a license that best suits your requirements:
 *
 * 1) GPLv3 (GNU General Public License Version 3) with Additional Terms, including an attribution clause as well as a limitation to use the software for commercial purposes.
 *
 * 2) CPSign Proprietary License that allows you to use CPSign for commercial activities, such as in a revenue-generating operation or environment, or integrate CPSign in your proprietary software without worrying about disclosing the source code of your proprietary software, which is required if you choose to use the software under GPLv3 license. See arosbio.com/cpsign/commercial-license for details.
 */
package com.arosbio.commons.config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;

import com.arosbio.commons.config.Configurable.ConfigParameter;
import com.arosbio.commons.config.Configurable.Sorter;

public class EnumConfig > implements ConfigParameter {
	
	public final static String TYPE = "enum/one-of";
	
	private final List names;
	private final EnumSet enumClazz;
	private final T defaultValue;
	private final List defaultGrid;
	private final Sorter sorting;

	private EnumConfig(Builder b){
		this.names = new ArrayList<>(b.names);
		this.defaultValue = b.defaultValue;
		this.enumClazz = b.enumClazz;
		if (b.defaultGrid!=null && !b.defaultGrid.isEmpty())
			this.defaultGrid = new ArrayList<>(b.defaultGrid);
		else
			this.defaultGrid = new ArrayList<>();
		this.sorting = b.sorting;
	}

	public static class Builder> {
		private List names;
		private T defaultValue;
		private EnumSet enumClazz;
		private List defaultGrid;
		private String description;
		private Sorter sorting = Sorter.none();

		public Builder(String name, EnumSet allowedValues, T defaultValue){
			this(Arrays.asList(name), allowedValues, defaultValue);
		}

		public Builder(List names,EnumSet allowedValues, T defaultValue){
			if (names==null || names.isEmpty())
				throw new IllegalArgumentException("Names must not be empty");
			this.names = names;
			this.enumClazz = allowedValues;
			this.defaultValue = defaultValue;
		}

		public Builder names(List names){
			if (names==null || names.isEmpty())
				throw new IllegalArgumentException("Names must not be empty");
			this.names = names;
			return this;
		}

		public Builder defaultValue(T value){
			this.defaultValue = value;
			return this;
		}

		public Builder defaultGrid(List grid){
			this.defaultGrid = grid;
			return this;
		}

		public Builder description(String description){
			this.description = description;
			return this;
		}

		public Builder sorting(Sorter sorting){
			this.sorting = sorting;
			return this;
		}

		public ConfigParameter build(){
			EnumConfig conf = new EnumConfig<>(this);
			if (description!=null && !description.isEmpty())
				return new DescribedConfig(conf, description);
			return conf;
		}
		
	}
	
	@Override
	public List getNames() {
		return names;
	}
	
	public Sorter getSorting(){
		return sorting;
	}
	
	public Object[] getEnumValues() {
		List allowedVals = new ArrayList<>();
		Iterator iter = enumClazz.iterator();
		while (iter.hasNext()) {
			allowedVals.add(iter.next());
		}
		
		return allowedVals.toArray();
	}
	
	public Iterator getEnumIterator(){
		return enumClazz.iterator();
	}
	
	@Override
	public String getType() {
		return TYPE;
	}
	
	public String toString() {
		return String.format("Parameter {%s}, type {%s}, allowed values {%s}, default value {%s}",
			names, TYPE, enumClazz,defaultValue);
	}
	
	public Object getDefault(){
		return defaultValue;
	}
	
	public List getDefaultGrid(){
		if (defaultGrid == null)
			return new ArrayList<>();
		return new ArrayList<>(defaultGrid);
	}
	
	public ConfigParameter withNames(List names) {
		return new EnumConfig.Builder<>(names, enumClazz,defaultValue)
			.defaultGrid(defaultGrid)
			.sorting(sorting)
			.build();
	}

}