blob: 0ba0c60a335dfb690e108f354ee0296f5cd270cf [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
*/
@SuppressWarnings("unchecked")
public class ListCreator {
List<Object> currentList;
List<Object> parentList;
/**
* This returns the list that is currently processed
*
* @param depth - The current dimension level
* @param isNew - if true creates a new list
* @return - The List
*/
public List<Object> getCurrentList(final int depth, final boolean isNew) {
if (isNew) {
this.currentList = (List<Object>) assignList(depth);
}
return this.currentList;
}
/**
* Adds the element to the current list
*
* @param depth - The current dimension level
* @param isNew - if true creates a new list
* @param element - The element to be added
*/
public <T> void addToCurrentList(final int depth, final boolean isNew, final T element) {
getCurrentList(depth, isNew).add(element);
}
/**
* This returns the currently processed list's parent container
*
* @param depth - The current dimension level
* @param isNew - if true creates a new list
* @return - The List
*/
public List<Object> getParentList(final int depth, final boolean isNew) {
if (isNew) {
this.parentList = (List<Object>) assignList(depth + 1);
}
return this.parentList;
}
/**
* Adds the current list to the parent list
*
* @param depth - The current dimension level
* @param isNew - if true creates a new list
*/
public void addToParentList(final int depth, final boolean isNew) {
getParentList(depth + 1, isNew).add(getCurrentList(depth, isNew));
}
/**
* @return - true if current list is empty, false otherwise
*/
public boolean isCurrentListEmpty() {
return (this.currentList == null) || this.currentList.isEmpty();
}
/**
* @return - true if current list is empty, false otherwise
*/
public boolean isParentListEmpty() {
return (this.parentList == null) || this.parentList.isEmpty();
}
/**
* This method is used to split the current list according to the given dimension and package's into the parent list
*
* @param pList - The parent container
* @param depth - The current dimension depth
* @param currentDimension - The size of current depth
* @return - The segmented list
*/
public List<Object> segmentList(final Object pList, final int depth, final int currentDimension) {
getParentList(depth, true);
getCurrentList(depth, true);
List<Object> current = (List<Object>) pList;
int cnt = 1;
for (int i = 0; i < current.size(); i++) {
if (cnt <= currentDimension) {
this.currentList.add(current.get(i));
}
else {
this.parentList.add(this.currentList);
getCurrentList(depth, true);
this.currentList.add(current.get(i));
cnt = 1;
}
cnt++;
if (i == (current.size() - 1)) {// This means the last iteration
this.parentList.add(this.currentList);
}
}
return this.parentList;
}
private Object assignList(final int depth) {
switch (depth) {
case 1:
List<?> theList1 = new ArrayList<>();
return theList1;
case 2:
List<List<?>> theList2 = new ArrayList<>();
return theList2;
case 3:
List<List<List<?>>> theList3 = new ArrayList<>();
return theList3;
case 4:
List<List<List<List<?>>>> theList4 = new ArrayList<>();
return theList4;
case 5:
List<List<List<List<List<?>>>>> theList5 = new ArrayList<>();
return theList5;
case 6:
List<List<List<List<List<List<?>>>>>> theList6 = new ArrayList<>();
return theList6;
case 7:
List<List<List<List<List<List<List<?>>>>>>> theList7 = new ArrayList<>();
return theList7;
case 8:
List<List<List<List<List<List<List<List<?>>>>>>>> theList8 = new ArrayList<>();
return theList8;
case 9:
List<List<List<List<List<List<List<List<List<?>>>>>>>>> theList9 = new ArrayList<>();
return theList9;
case 10:
List<List<List<List<List<List<List<List<List<List<?>>>>>>>>>> theList10 = new ArrayList<>();
return theList10;
case 11:
List<List<List<List<List<List<List<List<List<List<List<?>>>>>>>>>>> theList11 = new ArrayList<>();
return theList11;
case 12:
List<List<List<List<List<List<List<List<List<List<List<List<?>>>>>>>>>>>> theList12 = new ArrayList<>();
return theList12;
case 13:
List<List<List<List<List<List<List<List<List<List<List<List<List<?>>>>>>>>>>>>> theList13 = new ArrayList<>();
return theList13;
case 14:
List<List<List<List<List<List<List<List<List<List<List<List<List<List<?>>>>>>>>>>>>>> theList14 =
new ArrayList<>();
return theList14;
case 15:
List<List<List<List<List<List<List<List<List<List<List<List<List<List<List<?>>>>>>>>>>>>>>> theList15 =
new ArrayList<>();
return theList15;
default:
throw new UnsupportedOperationException("Stack too deep maximum limit is 15");
}
}
}