blob: 615e77fc0814c1d05876e0282aa263b07c1819f8 [file] [log] [blame]
/*
* Copyright (c) Robert Bosch GmbH. All rights reserved.
*/
package org.eclipse.blockchain.core;
import java.util.ArrayList;
import java.util.List;
/**
* @author ADG5COB
*/
public class MultidimensionalArrayValueCreator {
private String[] reverseElements(final String[] input) {
String[] output = new String[input.length];
int ind = 0;
for (int x = (input.length - 1); x > -1; x--) {
output[ind++] = input[x];
}
return output;
}
/**
* This converts the given array input into a multi-dimensional list based on dimension
*
* @param inputArr - The array elements
* @param dimensionSizes - The sizes of various dimension's
* @return - Multidimensional list
*/
public List<Object> getValuesAsList(final Object[] inputArr, final String dimensionSizes) {
List<Object> theFinalObject = null;
ListCreator listCreator = new ListCreator();
String[] dimensionSizesArr = reverseElements(dimensionSizes.split(","));
int depth = 1;
if (dimensionSizesArr.length == 1) {
List<Object> theList = new ArrayList<>();
for (Object s : inputArr) {
theList.add(s);
}
theFinalObject = theList;
}
else {// This separates the iput elements into 1st level list's
// Create a parent container list
listCreator.getParentList(depth, true);
int currentDimensionSize = Integer.parseInt(dimensionSizesArr[dimensionSizesArr.length - 1]);
int cnt = 1;
for (int f = 0; f < inputArr.length; f++) {
if ((cnt == 1) || (cnt > currentDimensionSize) || (f == (inputArr.length - 1))) {
if ((f == (inputArr.length - 1))) {// This means the last element is being dealt so we have to add it to the
// list
listCreator.addToCurrentList(depth, false, inputArr[f]);
}
// create new List and Add it; and add previous list(if existing) to a parent list and reset cnt
cnt = 1;
if (!listCreator.isCurrentListEmpty()) {
listCreator.addToParentList(depth, false);
}
listCreator.addToCurrentList(depth, true, inputArr[f]);
}
else {
// add element to current list
listCreator.addToCurrentList(depth, false, inputArr[f]);
}
cnt++;
}
theFinalObject = listCreator.getParentList(depth, false);
depth++;
if (dimensionSizesArr.length > 2) {
for (int h = (dimensionSizesArr.length - 2); h > 0; h--) {
currentDimensionSize = Integer.parseInt(dimensionSizesArr[h]);
theFinalObject = listCreator.segmentList(theFinalObject, depth, currentDimensionSize);
depth++;
}
}
}
return theFinalObject;
}
}