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

com.github.TKnudsen.ComplexDataObject.model.tools.DataConversion Maven / Gradle / Ivy

Go to download

A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.

The newest version!
package com.github.TKnudsen.ComplexDataObject.model.tools;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.apache.commons.lang3.ArrayUtils;

/**
 * little helpers for the conversion of general data structures
 *
 * 

* Copyright: Copyright (c) 2017-2020 *

* * @author Juergen Bernard * @version 1.07 */ public class DataConversion { /** * converts a collection of Numbers to an array of double primitives. * * @param values * @return */ public static double[] toPrimitives(Collection values) { if (values == null) return null; return values.stream().mapToDouble(Number::doubleValue).toArray(); } /** * converts a array of Doubles to an array of double primitives. * * @param values * @return */ public static double[] toPrimitives(Double[] values) { if (values == null) return null; return ArrayUtils.toPrimitive(values); } /** * converts a collection of Integers to an array of int primitives. * * @param values * @return */ public static int[] toIntPrimitives(Collection values) { if (values == null) return null; return values.stream().mapToInt(Number::intValue).toArray(); } /** * Converts an array of double primitives to an array of Double objects. * * @param values * @return */ public static Double[] doublePrimitivesToArray(double[] values) { if (values == null) return null; Double[] array = new Double[values.length]; for (int i = 0; i < values.length; i++) array[i] = values[i]; return array; } /** * Converts an array of double primitives to a List of Double objects. * * @param values * @return */ public static List doublePrimitivesToList(double[] values) { return Arrays.asList(DataConversion.doublePrimitivesToArray(values)); } /** * Converts an array of int primitives to a List of Integer objects. * * @param values * @return */ public static List intPrimitivesToList(int[] values) { return IntStream.of(values).boxed().collect(Collectors.toCollection(ArrayList::new)); } /** * Converts an array to a List. * * @param values * @return */ public static List arrayToList(T[] values) { return new ArrayList(Arrays.asList(values)); } /** * Converts a given Collection to an array. * * @param collection * @param classType * @return */ public static T[] collectionToArray(Collection collection, Class classType) { List list = collectionToList(collection); return listToArray(list, classType); } /** * converts a given Collection into a List. * * @param collection * @return */ public static List collectionToList(Collection collection) { List list; if (collection instanceof List) list = (List) collection; else list = new ArrayList(collection); return list; } /** * Converts a Set into a List. * * @param set * @return */ public static List setToList(Set set) { List list = new ArrayList<>(); list.addAll(set); return list; } /** * abstracts a List of concrete Doubles to Numbers * * @param values * @return */ public static List numberListFromDoubleList(List values) { List numbers = new ArrayList<>(); for (Double d : values) numbers.add(d); return numbers; } /** * Converts a given List to an array. * * @param list * @param classType * @return */ public static T[] listToArray(List list, Class classType) { if (list == null) return null; if (classType == null) throw new IllegalArgumentException("DataConversion.listToArray: class information missing."); @SuppressWarnings("unchecked") T[] array = (T[]) Array.newInstance(classType, list.size()); for (int i = 0; i < list.size(); ++i) { array[i] = list.get(i); } return array; } /** * creates a list with constant values, according to a given value t. * * @param * @param t * @param count * @return */ public static List constantValueList(T t, int count) { List list = new ArrayList(count); for (int i = 0; i < count; i++) list.add(t); return list; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy