package com.bayer.edam.migration.attic;

import java.util.ArrayList;
import java.util.List;

public class Tesssst {

	public static void main(String[] args) {
		List<String> entryCategories = new ArrayList<String>();
		entryCategories.add("aaa/bbb");
		entryCategories.add("aaa/bbb/ccc/ddd");
		entryCategories.add("aaa");
		entryCategories.add("xxx/yyy/zzz");
		entryCategories.add("xxx/yyy/zzz/nnn");
		entryCategories.add("xxx/yyy/zzz");
		entryCategories.add("xxx/yyy/zzz");
		entryCategories.add("xxx/yyy/zzz/ttt");
		
		List<String> categories = new ArrayList<String>();
		for (String entryCategory : entryCategories) {
			List<String> tmpCategories = new ArrayList<String>();
			boolean match = false;
			for (String category : categories) {
				if (match) {
					System.out.println("Adding " + category);
					tmpCategories.add(category);
					continue;
				}
				
				if (category.length() < entryCategory.length() && category.equals(entryCategory.substring(0, category.length()))) {
					System.out.println("Adding " + entryCategory);
					tmpCategories.add(entryCategory);
					match = true;
				}
				else if (category.length() >= entryCategory.length() && entryCategory.equals(category.substring(0, entryCategory.length()))) {
					System.out.println("Adding " + category);
					tmpCategories.add(category);
					match = true;
				}
				else {
					System.out.println("Adding " + category);
					tmpCategories.add(category);
				}
			}
			
			if (!match) {
				System.out.println("Adding " + entryCategory);
				tmpCategories.add(entryCategory);
				match = true;
			}
			
			categories = tmpCategories;
		}
		
		System.out.println("\nOutcome:");
		for (String s : categories) {
			System.out.println(s);
		}

	}

}
