summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-07-29 07:23:48 (EDT)
committerEike Stepper2007-07-29 07:23:48 (EDT)
commitf8d2c0ada768b162275e9bb50bd23859556058fc (patch)
tree943afa05af7e728bbdcd8f86a7906d1f093222cd
parent35eda8a5cc24a82bae8676548b747e6fdea0a446 (diff)
downloadcdo-f8d2c0ada768b162275e9bb50bd23859556058fc.zip
cdo-f8d2c0ada768b162275e9bb50bd23859556058fc.tar.gz
cdo-f8d2c0ada768b162275e9bb50bd23859556058fc.tar.bz2
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/EclipseMonitor.java31
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/LegacyMonitor.java21
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/Monitor.java63
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/RootMonitor.java42
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/SubMonitor.java45
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/LegacyUtil.java11
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/PROGRESS.java142
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/Test.java61
8 files changed, 416 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/EclipseMonitor.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/EclipseMonitor.java
new file mode 100644
index 0000000..68b58aa
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/EclipseMonitor.java
@@ -0,0 +1,31 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.internal.util.om.progress;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+
+/**
+ * @author Eike Stepper
+ */
+public class EclipseMonitor extends RootMonitor
+{
+ private IProgressMonitor eclipseMonitor;
+
+ public EclipseMonitor(IProgressMonitor eclipseMonitor)
+ {
+ this.eclipseMonitor = eclipseMonitor;
+ }
+
+ public IProgressMonitor getEclipseMonitor()
+ {
+ return eclipseMonitor;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/LegacyMonitor.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/LegacyMonitor.java
new file mode 100644
index 0000000..3ec2a27
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/LegacyMonitor.java
@@ -0,0 +1,21 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.internal.util.om.progress;
+
+/**
+ * @author Eike Stepper
+ */
+public class LegacyMonitor extends RootMonitor
+{
+ public LegacyMonitor()
+ {
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/Monitor.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/Monitor.java
new file mode 100644
index 0000000..b0be5af
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/Monitor.java
@@ -0,0 +1,63 @@
+package org.eclipse.net4j.internal.util.om.progress;
+
+import org.eclipse.net4j.util.om.PROGRESS;
+
+/**
+ * @author Eike Stepper
+ */
+public abstract class Monitor
+{
+ private static final int UNINITIALIZED = 0;
+
+ protected int totalWork = UNINITIALIZED;
+
+ protected int work;
+
+ public Monitor()
+ {
+ }
+
+ public int getTotalWork()
+ {
+ return totalWork;
+ }
+
+ public void begin(int totalWork, String task)
+ {
+ if (this.totalWork != UNINITIALIZED)
+ {
+ throw new IllegalStateException("Monitoring has already begun");
+ }
+
+ this.totalWork = totalWork;
+ if (task != null)
+ {
+ setTask(task);
+ }
+ }
+
+ public void worked(int work, String success)
+ {
+ if (this.totalWork == UNINITIALIZED)
+ {
+ throw new IllegalStateException("Monitoring has not yet begun");
+ }
+
+ this.work += work;
+ if (totalWork != PROGRESS.UNKNOWN && this.work > totalWork)
+ {
+ throw new IllegalStateException("Work has exceeded total work");
+ }
+
+ if (success != null)
+ {
+ onSuccess(success);
+ }
+ }
+
+ public abstract void onSuccess(String success);
+
+ public abstract String getTask();
+
+ public abstract void setTask(String task);
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/RootMonitor.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/RootMonitor.java
new file mode 100644
index 0000000..652b074
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/RootMonitor.java
@@ -0,0 +1,42 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.internal.util.om.progress;
+
+/**
+ * @author Eike Stepper
+ */
+public abstract class RootMonitor extends Monitor
+{
+ private transient String task;
+
+ public RootMonitor()
+ {
+ }
+
+ @Override
+ public void onSuccess(String success)
+ {
+ System.out.println(success);
+ }
+
+ @Override
+ public String getTask()
+ {
+ return task;
+ }
+
+ @Override
+ public void setTask(String task)
+ {
+ this.task = task;
+ System.out.println(task);
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/SubMonitor.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/SubMonitor.java
new file mode 100644
index 0000000..aefca3b
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/internal/util/om/progress/SubMonitor.java
@@ -0,0 +1,45 @@
+package org.eclipse.net4j.internal.util.om.progress;
+
+/**
+ * @author Eike Stepper
+ */
+public class SubMonitor extends Monitor
+{
+ private Monitor parent;
+
+ private int workFromParent;
+
+ public SubMonitor(Monitor parent, int workFromParent)
+ {
+ this.parent = parent;
+ this.workFromParent = workFromParent;
+ }
+
+ public Monitor getParent()
+ {
+ return parent;
+ }
+
+ public int getWorkFromParent()
+ {
+ return workFromParent;
+ }
+
+ @Override
+ public void onSuccess(String success)
+ {
+ parent.onSuccess(success);
+ }
+
+ @Override
+ public String getTask()
+ {
+ return parent.getTask();
+ }
+
+ @Override
+ public void setTask(String task)
+ {
+ parent.setTask(task);
+ }
+} \ No newline at end of file
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/LegacyUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/LegacyUtil.java
index 62917c3..abb28a7 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/LegacyUtil.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/LegacyUtil.java
@@ -11,6 +11,7 @@
package org.eclipse.net4j.util.om;
import org.eclipse.net4j.internal.util.om.LegacyBundle;
+import org.eclipse.net4j.internal.util.om.progress.LegacyMonitor;
/**
* @author Eike Stepper
@@ -36,4 +37,14 @@ public final class LegacyUtil
((LegacyBundle)bundles[i]).stop();
}
}
+
+ public static void startMonitoring()
+ {
+ PROGRESS.startMonitoring(new LegacyMonitor());
+ }
+
+ public static void stopMonitoring()
+ {
+ PROGRESS.stopMonitoring();
+ }
}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/PROGRESS.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/PROGRESS.java
new file mode 100644
index 0000000..3941124
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/PROGRESS.java
@@ -0,0 +1,142 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.om;
+
+import org.eclipse.net4j.internal.util.om.progress.Monitor;
+import org.eclipse.net4j.internal.util.om.progress.RootMonitor;
+import org.eclipse.net4j.internal.util.om.progress.SubMonitor;
+
+import java.text.MessageFormat;
+
+/**
+ * @author Eike Stepper
+ */
+public final class PROGRESS
+{
+ public static final int UNKNOWN = -1;
+
+ private static final ThreadLocal<Monitor> MONITOR = new ThreadLocal();
+
+ private PROGRESS()
+ {
+ }
+
+ public static void begin(int totalWork, String taskPattern, String... singularAndPlural)
+ {
+ Monitor monitor = getMonitor();
+ String task = null;
+ if (taskPattern != null)
+ {
+ task = format(totalWork, taskPattern, singularAndPlural);
+ }
+
+ monitor.begin(totalWork, task);
+ }
+
+ public static void worked(int work, String successPattern, String... singularAndPlural)
+ {
+ Monitor monitor = getMonitor();
+ String success = null;
+ if (successPattern != null)
+ {
+ success = format(work, successPattern, singularAndPlural);
+ }
+
+ monitor.worked(work, success);
+ }
+
+ public static void fork(int workFromParent, Runnable runnable, String successPattern, String... singularAndPlural)
+ {
+ Monitor monitor = getMonitor();
+ SubMonitor subMonitor = new SubMonitor(monitor, workFromParent);
+ MONITOR.set(subMonitor);
+
+ try
+ {
+ runnable.run();
+ if (successPattern != null)
+ {
+ String success = format(workFromParent, successPattern, singularAndPlural);
+ subMonitor.onSuccess(success);
+ }
+ }
+ finally
+ {
+ Monitor current = MONITOR.get();
+ if (current != subMonitor)
+ {
+ throw new IllegalStateException("Illegal monitor nesting");
+ }
+
+ MONITOR.set(subMonitor.getParent());
+ }
+ }
+
+ static void startMonitoring(RootMonitor rootMonitor)
+ {
+ Monitor monitor = MONITOR.get();
+ if (monitor != null)
+ {
+ throw new IllegalStateException("Monitoring has already been started");
+ }
+
+ MONITOR.set(rootMonitor);
+ }
+
+ static void stopMonitoring()
+ {
+ Monitor monitor = MONITOR.get();
+ if (monitor == null)
+ {
+ throw new IllegalStateException("Monitoring has not been started");
+ }
+
+ if (!(monitor instanceof RootMonitor))
+ {
+ throw new IllegalStateException("Illegal monitor nesting");
+ }
+
+ MONITOR.set(null);
+ }
+
+ private static String format(int totalWork, String pattern, String... singularAndPlural)
+ {
+ String task;
+ if (singularAndPlural != null && singularAndPlural.length != 0)
+ {
+ if (singularAndPlural.length != 2)
+ {
+ throw new IllegalArgumentException("Give exactly singular and plural form after the pattern");
+ }
+
+ String singular = singularAndPlural[0];
+ String plural = singularAndPlural[1];
+ String arg = String.valueOf(totalWork) + " " + (totalWork == 1 ? singular : plural);
+ task = MessageFormat.format(pattern, arg);
+ }
+ else
+ {
+ task = MessageFormat.format(pattern, totalWork);
+ }
+ return task;
+ }
+
+ private static Monitor getMonitor()
+ {
+ Monitor monitor = MONITOR.get();
+ if (monitor == null)
+ {
+ throw new IllegalStateException("No monitor available");
+ }
+
+ return monitor;
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/Test.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/Test.java
new file mode 100644
index 0000000..175c616
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/om/Test.java
@@ -0,0 +1,61 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.om;
+
+/**
+ * @author Eike Stepper
+ */
+public class Test
+{
+ public static void main(String[] args)
+ {
+ LegacyUtil.startMonitoring();
+ readClasses();
+ LegacyUtil.stopMonitoring();
+ }
+
+ /**
+ * Supports {@link PROGRESS progress monitoring}.
+ */
+ private static void readClasses()
+ {
+ int num = 3;
+ PROGRESS.begin(2 * num, "Reading {0}", "class", "classes");
+ for (int i = 0; i < num; i++)
+ {
+ // Create class buffer
+ PROGRESS.worked(1, "Created class buffer {0}");
+
+ // Read class
+ PROGRESS.fork(1, new Runnable()
+ {
+ public void run()
+ {
+ readFields();
+ }
+ }, "Read {0}", "class", "classes");
+ }
+ }
+
+ /**
+ * Supports {@link PROGRESS progress monitoring}.
+ */
+ private static void readFields()
+ {
+ int num = 5;
+ PROGRESS.begin(num, "Reading {0}", "field", "fields");
+ for (int i = 0; i < num; i++)
+ {
+ // Read field
+ PROGRESS.worked(1, "Read {0}", "field", "fields");
+ }
+ }
+}