1 25 26 package net.killingar.forum.actions; 27 28 29 import it.unimi.dsi.fastutil.longs.Long2ObjectAVLTreeMap; 30 import net.killingar.forum.internal.ParentIDItem; 31 import net.killingar.forum.internal.ParentIDItemTreeNode; 32 33 import java.util.Date ; 34 35 public class Utils 36 { 37 static final java.text.SimpleDateFormat dateFormatter = new java.text.SimpleDateFormat ("EEE' 'MMM' 'dd' 'HH:mm"); 38 39 42 public static void buildFlatTree(ParentIDItem items[], long indents[]) 43 { 44 ParentIDItem destination[] = new ParentIDItem[items.length]; 45 boolean processed[] = new boolean[items.length]; 46 47 for (int i = 0; i < processed.length; i++) 48 processed[i] = false; 49 50 int foo = processFlatTree(0, items, destination, indents, processed, 0, 0); 51 if (foo != items.length) 52 { 53 java.util.ArrayList l = new java.util.ArrayList (); 54 55 for (int i = 0; i < processed.length; i++) 56 { 57 if (!processed[i]) 58 { 59 l.add(items[i]); 60 } 61 } 62 63 throw new RuntimeException ("error building complete tree "+l); 64 } 65 66 for (int i = 0; i < items.length; i++) 68 items[i] = destination[i]; 69 } 70 71 75 public static ParentIDItemTreeNode buildTree(ParentIDItem items[], Long2ObjectAVLTreeMap inMap) 76 { 77 ParentIDItemTreeNode node = new ParentIDItemTreeNode(); 78 node.indent = -1; 79 80 inMap.clear(); 81 inMap.put(-1, node); 82 83 for (int i = 0; i < items.length; i++) 84 { 85 ParentIDItemTreeNode parentNode = (ParentIDItemTreeNode)inMap.get(items[i].getParentID()); 87 if (parentNode == null) 88 throw new RuntimeException ("item "+items[i]+" points at non-existent parent item "+items[i].getParentID()); 89 90 ParentIDItemTreeNode childNode = new ParentIDItemTreeNode(items[i], parentNode.indent+1); 91 parentNode.getSubnodesAlways().add(childNode); 92 inMap.put(items[i].getId(), childNode); 93 } 94 95 return node; 96 } 97 98 public static String formatDate(Date d) 99 { 100 return dateFormatter.format(d); 101 } 102 103 public static String buildIndent(long indent) 104 { 105 StringBuffer s = new StringBuffer (); 106 107 for (long i = 0; i < indent; i++) 108 s.append("<td> </td>"); 109 110 return s.toString(); 111 } 112 113 public static String buildStringOnlyIndent(long indent) 114 { 115 StringBuffer s = new StringBuffer (); 116 117 for (long i = 0; i < indent; i++) 118 s.append(" "); 119 120 return s.toString(); 121 } 122 123 private static int processFlatTree(long parentID, ParentIDItem source[], ParentIDItem destination[], long indents[], boolean processed[], int pos, int indent) 124 { 125 for (int i = 0; i < source.length; i++) 126 { 127 if (processed[i])continue; 128 129 if (source[i].getParentID() == parentID) 130 { 131 destination[pos] = source[i]; 133 indents[pos] = indent; 134 processed[i] = true; 135 136 pos = processFlatTree(destination[pos].getId(), source, destination, indents, processed, pos+1, indent+1); 138 } 139 } 140 141 return pos; 142 } 143 } | Popular Tags |