KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > actions > Utils


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

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 JavaDoc;
34
35 public class Utils
36 {
37     static final java.text.SimpleDateFormat JavaDoc dateFormatter = new java.text.SimpleDateFormat JavaDoc("EEE' 'MMM' 'dd' 'HH:mm");
38
39     /**
40      * Sort the items in the correct order according to their parentID.
41      */

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 JavaDoc l = new java.util.ArrayList JavaDoc();
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 JavaDoc("error building complete tree "+l);
64         }
65
66         // copy data back to items
67
for (int i = 0; i < items.length; i++)
68             items[i] = destination[i];
69     }
70
71     /**
72      * Sort the items in the correct order according to their parentID.
73      * Top-level items (with parentID = 0) must be before their children in the incoming list.
74      */

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             // real work
86
ParentIDItemTreeNode parentNode = (ParentIDItemTreeNode)inMap.get(items[i].getParentID());
87             if (parentNode == null)
88                 throw new RuntimeException JavaDoc("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 JavaDoc formatDate(Date JavaDoc d)
99     {
100         return dateFormatter.format(d);
101     }
102
103     public static String JavaDoc buildIndent(long indent)
104     {
105         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
106
107         for (long i = 0; i < indent; i++)
108             s.append("<td>&nbsp;</td>");
109
110         return s.toString();
111     }
112
113     public static String JavaDoc buildStringOnlyIndent(long indent)
114     {
115         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
116
117         for (long i = 0; i < indent; i++)
118             s.append("&nbsp;&nbsp;&nbsp;");
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                 // put to list
132
destination[pos] = source[i];
133                 indents[pos] = indent;
134                 processed[i] = true;
135
136                 // process
137
pos = processFlatTree(destination[pos].getId(), source, destination, indents, processed, pos+1, indent+1);
138             }
139         }
140
141         return pos;
142     }
143 }
Popular Tags