]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/internal/Bug254570Workaround.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / internal / Bug254570Workaround.java
index 86b257a746ed53a0045f37b8d5be917ca1e50e24..a16ed5124d1dbc36f1c67ab205c7f1ef8504ab0e 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.browsing.ui.swt.internal;\r
-\r
-import java.lang.reflect.Field;\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-\r
-import org.eclipse.jface.viewers.IElementComparer;\r
-import org.eclipse.jface.viewers.ISelection;\r
-import org.eclipse.jface.viewers.StructuredSelection;\r
-\r
-/**\r
- * This class works around Eclipse bug #254570 - StructuredSelection is missing\r
- * {@link #hashCode()} implementation.\r
- * \r
- * <p>\r
- * Use {@link #wrapSelection(ISelection)} to obtain a wrapper for\r
- * StructuredSelection instances that "injects" a hashCode implementation.\r
- * \r
- * @author Tuukka Lehtonen\r
- * @deprecated does not work, do not use, causes problems with DB requests:\r
- * <pre>\r
- * Exception in thread "Query Thread 1" java.lang.IllegalArgumentException: Equal objects must have equal hashcodes. During rehashing, Trove discovered that the following two objects claim to be equal (as in java.lang.Object.equals()) but their hashCodes (or those calculated by your TObjectHashingStrategy) are not equal.This violates the general contract of java.lang.Object.hashCode().  See bullet point two in that method's documentation. object #1 =LazyViewpoint[22182778].childQuery; object #2 =LazyViewpoint[2672086].childQuery\r
- * at org.simantics.db.impl.query.StableObjectHash.throwObjectContractViolation(StableObjectHash.java:348)\r
- * at org.simantics.db.impl.query.StableHashMap.rehash(StableHashMap.java:410)\r
- * at gnu.trove.THash.postInsertHook(THash.java:368)\r
- * at org.simantics.db.impl.query.StableHashMap.doPut(StableHashMap.java:214)\r
- * at org.simantics.db.impl.query.StableHashMap.put(StableHashMap.java:182)\r
- * at org.simantics.db.impl.query.QueryProcessor.performForEach(QueryProcessor.java:1433)\r
- * at org.simantics.db.impl.query.QueryProcessor.runRead(QueryProcessor.java:1020)\r
- * at org.simantics.db.impl.query.QueryProcessor$4.run(QueryProcessor.java:1178)\r
- * at org.simantics.db.impl.query.QueryProcessor$1.run(QueryProcessor.java:680)\r
- * </pre>\r
- */\r
-@Deprecated\r
-class Bug254570Workaround extends StructuredSelection {\r
-\r
-    private final Object[]         elements;\r
-    private final IElementComparer comparer;\r
-\r
-    public static ISelection wrapSelection(ISelection s) {\r
-        if (s == null)\r
-            return null;\r
-        try {\r
-            return StructuredSelection.class.equals(s.getClass()) ? Bug254570Workaround.make((StructuredSelection) s) : s;\r
-        } catch (SecurityException e) {\r
-            throw new RuntimeException("Workaround for #254570 failed", e);\r
-        } catch (NoSuchFieldException e) {\r
-            throw new RuntimeException("Workaround for #254570 failed", e);\r
-        } catch (IllegalArgumentException e) {\r
-            throw new RuntimeException("Workaround for #254570 failed", e);\r
-        } catch (IllegalAccessException e) {\r
-            throw new RuntimeException("Workaround for #254570 failed", e);\r
-        }\r
-        //return s;\r
-    }\r
-\r
-    public static Bug254570Workaround make(StructuredSelection s) throws SecurityException,\r
-    NoSuchFieldException, IllegalArgumentException, IllegalAccessException {\r
-        if (s == null)\r
-            throw new NullPointerException("null StructuredSelection");\r
-\r
-        Class<?> clazz = s.getClass();\r
-        Field elementsField = null;\r
-        Field comparerField = null;\r
-        for (Field f : clazz.getDeclaredFields()) {\r
-            if ("elements".equals(f.getName()))\r
-                elementsField = f;\r
-            else if ("comparer".equals(f.getName()))\r
-                comparerField = f;\r
-        }\r
-        if (elementsField == null)\r
-            throw new IllegalArgumentException("could not find field 'elements'");\r
-        if (comparerField == null)\r
-            throw new IllegalArgumentException("could not find field 'comparer'");\r
-\r
-        Object[] elements = getAccessible(elementsField, s);\r
-        IElementComparer comparer = getAccessible(comparerField, s);\r
-\r
-        return new Bug254570Workaround(elements, comparer);\r
-    }\r
-\r
-    private static <T> T getAccessible(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {\r
-        boolean accessible = f.isAccessible();\r
-        try {\r
-            if (!accessible)\r
-                f.setAccessible(true);\r
-            @SuppressWarnings("unchecked")\r
-            T t = (T) f.get(obj);\r
-            return t;\r
-        } finally {\r
-            if (!accessible)\r
-                f.setAccessible(false);\r
-        }\r
-    }\r
-\r
-    private Bug254570Workaround(Object[] elements, IElementComparer comparer) {\r
-        super(elements == null ? Collections.emptyList() : Arrays.asList(elements), comparer);\r
-        this.elements = elements;\r
-        this.comparer = comparer;\r
-    }\r
-\r
-    private int hashCode(Object element) {\r
-        return element == null ? 0 :\r
-            comparer != null ? comparer.hashCode(element) : element.hashCode();\r
-    }\r
-\r
-    /**\r
-     * StructuredSelection.hashCode is not implemented, need to implement\r
-     * it here.\r
-     */\r
-    @Override\r
-    public int hashCode() {\r
-        int code = 31;\r
-        if (elements != null) {\r
-            for (int i = 0; i < elements.length; i++) {\r
-                code = code * 17 + hashCode(elements[i]);\r
-            }\r
-        }\r
-        return code;\r
-    }\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2012 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.swt.internal;
+
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Collections;
+
+import org.eclipse.jface.viewers.IElementComparer;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.StructuredSelection;
+
+/**
+ * This class works around Eclipse bug #254570 - StructuredSelection is missing
+ * {@link #hashCode()} implementation.
+ * 
+ * <p>
+ * Use {@link #wrapSelection(ISelection)} to obtain a wrapper for
+ * StructuredSelection instances that "injects" a hashCode implementation.
+ * 
+ * @author Tuukka Lehtonen
+ * @deprecated does not work, do not use, causes problems with DB requests:
+ * <pre>
+ * Exception in thread "Query Thread 1" java.lang.IllegalArgumentException: Equal objects must have equal hashcodes. During rehashing, Trove discovered that the following two objects claim to be equal (as in java.lang.Object.equals()) but their hashCodes (or those calculated by your TObjectHashingStrategy) are not equal.This violates the general contract of java.lang.Object.hashCode().  See bullet point two in that method's documentation. object #1 =LazyViewpoint[22182778].childQuery; object #2 =LazyViewpoint[2672086].childQuery
+ * at org.simantics.db.impl.query.StableObjectHash.throwObjectContractViolation(StableObjectHash.java:348)
+ * at org.simantics.db.impl.query.StableHashMap.rehash(StableHashMap.java:410)
+ * at gnu.trove.THash.postInsertHook(THash.java:368)
+ * at org.simantics.db.impl.query.StableHashMap.doPut(StableHashMap.java:214)
+ * at org.simantics.db.impl.query.StableHashMap.put(StableHashMap.java:182)
+ * at org.simantics.db.impl.query.QueryProcessor.performForEach(QueryProcessor.java:1433)
+ * at org.simantics.db.impl.query.QueryProcessor.runRead(QueryProcessor.java:1020)
+ * at org.simantics.db.impl.query.QueryProcessor$4.run(QueryProcessor.java:1178)
+ * at org.simantics.db.impl.query.QueryProcessor$1.run(QueryProcessor.java:680)
+ * </pre>
+ */
+@Deprecated
+class Bug254570Workaround extends StructuredSelection {
+
+    private final Object[]         elements;
+    private final IElementComparer comparer;
+
+    public static ISelection wrapSelection(ISelection s) {
+        if (s == null)
+            return null;
+        try {
+            return StructuredSelection.class.equals(s.getClass()) ? Bug254570Workaround.make((StructuredSelection) s) : s;
+        } catch (SecurityException e) {
+            throw new RuntimeException("Workaround for #254570 failed", e);
+        } catch (NoSuchFieldException e) {
+            throw new RuntimeException("Workaround for #254570 failed", e);
+        } catch (IllegalArgumentException e) {
+            throw new RuntimeException("Workaround for #254570 failed", e);
+        } catch (IllegalAccessException e) {
+            throw new RuntimeException("Workaround for #254570 failed", e);
+        }
+        //return s;
+    }
+
+    public static Bug254570Workaround make(StructuredSelection s) throws SecurityException,
+    NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
+        if (s == null)
+            throw new NullPointerException("null StructuredSelection");
+
+        Class<?> clazz = s.getClass();
+        Field elementsField = null;
+        Field comparerField = null;
+        for (Field f : clazz.getDeclaredFields()) {
+            if ("elements".equals(f.getName()))
+                elementsField = f;
+            else if ("comparer".equals(f.getName()))
+                comparerField = f;
+        }
+        if (elementsField == null)
+            throw new IllegalArgumentException("could not find field 'elements'");
+        if (comparerField == null)
+            throw new IllegalArgumentException("could not find field 'comparer'");
+
+        Object[] elements = getAccessible(elementsField, s);
+        IElementComparer comparer = getAccessible(comparerField, s);
+
+        return new Bug254570Workaround(elements, comparer);
+    }
+
+    private static <T> T getAccessible(Field f, Object obj) throws IllegalArgumentException, IllegalAccessException {
+        boolean accessible = f.isAccessible();
+        try {
+            if (!accessible)
+                f.setAccessible(true);
+            @SuppressWarnings("unchecked")
+            T t = (T) f.get(obj);
+            return t;
+        } finally {
+            if (!accessible)
+                f.setAccessible(false);
+        }
+    }
+
+    private Bug254570Workaround(Object[] elements, IElementComparer comparer) {
+        super(elements == null ? Collections.emptyList() : Arrays.asList(elements), comparer);
+        this.elements = elements;
+        this.comparer = comparer;
+    }
+
+    private int hashCode(Object element) {
+        return element == null ? 0 :
+            comparer != null ? comparer.hashCode(element) : element.hashCode();
+    }
+
+    /**
+     * StructuredSelection.hashCode is not implemented, need to implement
+     * it here.
+     */
+    @Override
+    public int hashCode() {
+        int code = 31;
+        if (elements != null) {
+            for (int i = 0; i < elements.length; i++) {
+                code = code * 17 + hashCode(elements[i]);
+            }
+        }
+        return code;
+    }
 }
\ No newline at end of file