summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2008-06-03 01:22:17 (EDT)
committerEike Stepper2008-06-03 01:22:17 (EDT)
commit4615c7071d3f923f03936df20baf93ad6fef8f63 (patch)
tree80070054f457a2a1206b9125d7746f69311b0b57
parent80dd19cec8729a254a47f5c10529b45d8d8908e8 (diff)
downloadcdo-4615c7071d3f923f03936df20baf93ad6fef8f63.zip
cdo-4615c7071d3f923f03936df20baf93ad6fef8f63.tar.gz
cdo-4615c7071d3f923f03936df20baf93ad6fef8f63.tar.bz2
[235264] Provide generic and transparent adapter support
https://bugs.eclipse.org/bugs/show_bug.cgi?id=235264
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/AdapterUtil.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/AdapterUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/AdapterUtil.java
new file mode 100644
index 0000000..57ca7ae
--- /dev/null
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/AdapterUtil.java
@@ -0,0 +1,82 @@
+/***************************************************************************
+ * Copyright (c) 2004 - 2008 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;
+
+/**
+ * @author Eike Stepper
+ */
+public final class AdapterUtil
+{
+ private AdapterUtil()
+ {
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <TYPE> TYPE adapt(Object object, Class<TYPE> type)
+ {
+ Object adapter = null;
+ if (type.isInstance(object))
+ {
+ adapter = object;
+ }
+ else
+ {
+ try
+ {
+ adapter = AdaptableHelper.adapt(object, type);
+ if (adapter == null)
+ {
+ adapter = AdapterManagerHelper.adapt(object, type);
+ }
+ }
+ catch (Throwable ignore)
+ {
+ }
+ }
+
+ return (TYPE)adapter;
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class AdaptableHelper
+ {
+ public static Object adapt(Object object, Class<?> type)
+ {
+ if (object instanceof org.eclipse.core.runtime.IAdaptable)
+ {
+ return ((org.eclipse.core.runtime.IAdaptable)object).getAdapter(type);
+ }
+
+ return null;
+ }
+ }
+
+ /**
+ * @author Eike Stepper
+ */
+ private static final class AdapterManagerHelper
+ {
+ private static org.eclipse.core.runtime.IAdapterManager adapterManager = org.eclipse.core.runtime.Platform
+ .getAdapterManager();
+
+ public static Object adapt(Object object, Class<?> type)
+ {
+ if (adapterManager != null)
+ {
+ return adapterManager.getAdapter(object, type);
+ }
+
+ return null;
+ }
+ }
+}