|
Not So Simple Code
|
public void update(boolean force) {
// long startTime= 0;
// if (DEBUG) {
// dumpStatistics();
// startTime= (new Date()).getTime();
// }
if (isDirty() || force) {
if (toolBarExist()) {
int oldCount = toolBar.getItemCount();
// clean contains all active items without double separators
IContributionItem[] items = getItems();
ArrayList clean = new ArrayList(items.length);
IContributionItem separator = null;
// long cleanStartTime= 0;
// if (DEBUG) {
// cleanStartTime= (new Date()).getTime();
// }
for (int i = 0; i < items.length; ++i) {
IContributionItem ci = items[i];
if (!ci.isVisible()) {
continue;
}
if (ci.isSeparator()) {
// delay creation until necessary
// (handles both adjacent separators, and separator at
// end)
separator = ci;
} else {
if (separator != null) {
if (clean.size() > 0) {
clean.add(separator);
}
separator = null;
}
clean.add(ci);
}
}
// if (DEBUG) {
// System.out.println(" Time needed to build clean vector: " +
// ((new Date()).getTime() - cleanStartTime));
// }
// determine obsolete items (removed or non active)
ToolItem[] mi = toolBar.getItems();
ArrayList toRemove = new ArrayList(mi.length);
for (int i = 0; i < mi.length; i++) {
Object data = mi[i].getData();
if (data == null
|| !clean.contains(data)
|| (data instanceof IContributionItem && ((IContributionItem) data)
.isDynamic())) {
toRemove.add(mi[i]);
}
}
// Turn redraw off if the number of items to be added
// is above a certain threshold, to minimize flicker,
// otherwise the toolbar can be seen to redraw after each item.
// Do this before any modifications are made.
// We assume each contribution item will contribute at least one
// toolbar item.
boolean useRedraw = (clean.size() - (mi.length - toRemove
.size())) >= 3;
try {
if (useRedraw) {
toolBar.setRedraw(false);
}
// remove obsolete items
for (int i = toRemove.size(); --i >= 0;) {
ToolItem item = (ToolItem) toRemove.get(i);
if (!item.isDisposed()) {
Control ctrl = item.getControl();
if (ctrl != null) {
item.setControl(null);
ctrl.dispose();
}
item.dispose();
}
}
// add new items
IContributionItem src, dest;
mi = toolBar.getItems();
int srcIx = 0;
int destIx = 0;
for (Iterator e = clean.iterator(); e.hasNext();) {
src = (IContributionItem) e.next();
// get corresponding item in SWT widget
if (srcIx < mi.length) {
dest = (IContributionItem) mi[srcIx].getData();
} else {
dest = null;
}
if (dest != null && src.equals(dest)) {
srcIx++;
destIx++;
continue;
}
if (dest != null && dest.isSeparator()
&& src.isSeparator()) {
mi[srcIx].setData(src);
srcIx++;
destIx++;
continue;
}
int start = toolBar.getItemCount();
src.fill(toolBar, destIx);
int newItems = toolBar.getItemCount() - start;
for (int i = 0; i < newItems; i++) {
ToolItem item = toolBar.getItem(destIx++);
item.setData(src);
}
}
// remove any old tool items not accounted for
for (int i = mi.length; --i >= srcIx;) {
ToolItem item = mi[i];
if (!item.isDisposed()) {
Control ctrl = item.getControl();
if (ctrl != null) {
item.setControl(null);
ctrl.dispose();
}
item.dispose();
}
}
setDirty(false);
// turn redraw back on if we turned it off above
} finally {
if (useRedraw) {
toolBar.setRedraw(true);
}
}
int newCount = toolBar.getItemCount();
relayout(toolBar, oldCount, newCount);
}
}
// if (DEBUG) {
// System.out.println(" Time needed for update: " + ((new
// Date()).getTime() - startTime));
// System.out.println();
// }
}
Redundant boolean logic:
if (!check_part(baz)) then
# do something
end
def check_part(foo)
if (foo) then
return true
else
return false
end
end
# why not just write this:
unless baz
# do something
end
nicely split code...
def is_valid_time_format(str)
if(!is_day_valid(str))
return false
end
if(!is_month_valid(str))
return false
end
if(!is_date_valid(str))
return false
end
if(!is_year_valid(str))
return false
end
if(!is_time_valid(str))
return false
end
return true
end
|
Not So Simple Code |
|