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

de.citec.tcs.alignment.primitives.Booleans Maven / Gradle / Ivy

Go to download

This module contains convenience functions to interface with primitive datatypes more comfortably.

The newest version!
/*
 * TCS Alignment Toolbox Version 3
 * 
 * Copyright (C) 2016
 * Benjamin Paaßen
 * AG Theoretical Computer Science
 * Centre of Excellence Cognitive Interaction Technology (CITEC)
 * University of Bielefeld
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */
package de.citec.tcs.alignment.primitives;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import lombok.NonNull;

/**
 * This class provides static functions to transform boolean arrays to lists of Booleans.
 *
 * @author Benjamin Paassen - bpaassen(at)techfak.uni-bielefeld.de
 */
public class Booleans {

	private Booleans() {

	}

	/**
	 * Returns the Booleans contained in the given array as a list.
	 *
	 * @param arr an array of booleans.
	 *
	 * @return the Booleans contained in the given array as a list.
	 */
	public static List transformSingle(@NonNull final boolean[] arr) {
		final ArrayList list = new ArrayList<>(arr.length);
		for (final boolean n : arr) {
			list.add(n);
		}
		return list;
	}

	/**
	 * Returns a list of lists of Booleans, one list per row in the given matrix.
	 * Null sequences will be filtered out.
	 *
	 * @param mat a matrix of booleans.
	 *
	 * @return a list of lists of Booleans, one list per row in the given matrix.
	 */
	public static List> transform(@NonNull final boolean[][] mat) {
		return transform(Arrays.asList(mat));
	}

	/**
	 * Returns a list of lists of Booleans, one list per boolean array in the given list.
	 * Null sequences will be filtered out.
	 *
	 * @param lst a list of boolean arrays.
	 *
	 * @return a list of lists of Booleans, one list per boolean array in the given list.
	 */
	public static List> transform(@NonNull final List lst) {
		final ArrayList> list = new ArrayList<>(lst.size());
		for (final boolean[] arr : lst) {
			if (arr == null) {
				continue;
			}
			list.add(transformSingle(arr));
		}
		return list;
	}

	/**
	 * Returns a list of lists of integers, one for each object in the given array.
	 * This is particular useful as interface to MATLAB. It is assumed that each object is meant to
	 * be one sequence, that is, either a java string or a character array. If it is neither, an
	 * IllegalArgumentException is thrown. Null sequences will be filtered out.
	 *
	 * @param arr an object array containing one sequence per entry. So each entry is expected to be
	 * a java string or a character array.
	 *
	 * @return a list containing a list of characters for each entry in the given Object array.
	 */
	public static List> transformObjects(@NonNull final Object[] arr) {
		final ArrayList> list = new ArrayList<>(arr.length);
		for (final Object seq : arr) {
			if (seq == null) {
				continue;
			}
			if (boolean[].class.isAssignableFrom(seq.getClass())) {
				list.add(transformSingle((boolean[]) seq));
			} else if (Boolean[].class.isAssignableFrom(seq.getClass())) {
				list.add(Arrays.asList((Boolean[]) seq));
			} else {
				throw new IllegalArgumentException("The given input is not a matrix of booleans"
						+ " and can thus not be interpreted "
						+ "as a data set of integers sequences. Found class: " + seq.getClass().getName());
			}
		}
		return list;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy