]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/inputs/Datum.java
6a554f4ac0f328f6f0d707236449fd0011eb377d
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / inputs / Datum.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.swt.inputs;\r
13 \r
14 import org.eclipse.core.runtime.IAdaptable;\r
15 \r
16 /**\r
17  * An equals-comparable and adaptable container for a single datum. The datum\r
18  * may be <code>null</code>. Adaptability is provided through {@link IAdaptable}\r
19  * .\r
20  * \r
21  * @author Tuukka Lehtonen\r
22  * \r
23  * @param <T> the type of the datum\r
24  */\r
25 public class Datum<T> implements IAdaptable {\r
26 \r
27     protected final T datum;\r
28 \r
29     public Datum(T datum) {\r
30         this.datum = datum;\r
31     }\r
32 \r
33     public T getDatum() {\r
34         return datum;\r
35     }\r
36 \r
37     @SuppressWarnings({ "unchecked", "rawtypes" })\r
38     @Override\r
39     public Object getAdapter(Class adapter) {\r
40         if (datum != null) {\r
41             if (adapter.isAssignableFrom(datum.getClass()))\r
42                 return datum;\r
43             if (datum instanceof IAdaptable)\r
44                 return ((IAdaptable) datum).getAdapter(adapter);\r
45         }\r
46         return null;\r
47     }\r
48 \r
49     @Override\r
50     public int hashCode() {\r
51         return datum != null ? datum.hashCode() : 0;\r
52     }\r
53 \r
54     @Override\r
55     public boolean equals(Object obj) {\r
56         if (this == obj)\r
57             return true;\r
58         if (obj == null)\r
59             return false;\r
60         if (getClass() != obj.getClass())\r
61             return false;\r
62         Datum<?> other = (Datum<?>) obj;\r
63         if (datum == null)\r
64             return datum == other.datum;\r
65         return datum.equals(other.datum);\r
66     }\r
67 \r
68 }