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