]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/element/handler/impl/StaticObjectAdapter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / element / handler / impl / StaticObjectAdapter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.g2d.element.handler.impl;
13
14 import org.simantics.g2d.element.ElementClass;
15 import org.simantics.g2d.element.handler.Adapter;
16 import org.simantics.utils.ObjectUtils;
17
18 /**
19  * A generic single object adapter handler for use with {@link ElementClass}.
20  * 
21  * @author Tuukka Lehtonen
22  */
23 public class StaticObjectAdapter implements Adapter {
24
25     private static final long serialVersionUID = -8713272704786672656L;
26
27     private final Object object;
28
29     public StaticObjectAdapter(Object object) {
30         if (object == null)
31             throw new IllegalArgumentException("null object");
32         this.object = object;
33     }
34
35     @SuppressWarnings("unchecked")
36     @Override
37     public <T> T adapt(Class<T> toClass) {
38         if (toClass == null)
39             throw new IllegalArgumentException("null class");
40         if (toClass.isInstance(object))
41             return (T) object;
42         return null;
43     }
44
45     @Override
46     public int hashCode() {
47         return ObjectUtils.hashCode(object);
48     }
49
50     @Override
51     public boolean equals(Object obj) {
52         if (this == obj)
53             return true;
54         if (obj == null)
55             return false;
56         if (getClass() != obj.getClass())
57             return false;
58         StaticObjectAdapter other = (StaticObjectAdapter) obj;
59         return ObjectUtils.objectEquals(object, other.object);
60     }
61
62 }