]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/DataContainer.java
Changing existing log4j logging to use slf4j
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / DataContainer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 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 /*\r
13  * Created on 16.12.2005\r
14  * \r
15  */\r
16 package org.simantics.utils;\r
17 \r
18 import java.lang.reflect.Array;\r
19 import java.util.Collection;\r
20 import java.util.Iterator;\r
21 \r
22 \r
23 /**\r
24  * DataContainer is used as return argument\r
25  * \r
26  * @author Toni Kalajainen\r
27  */\r
28 public final class DataContainer<T> implements Container<T>, Collection<T> {\r
29 \r
30     private T data;\r
31 \r
32     public static <T> DataContainer<T> make(T t) {\r
33         return new DataContainer<T>(t);\r
34     }\r
35 \r
36     public DataContainer() {\r
37     }\r
38 \r
39     public DataContainer(T initialData) {\r
40         this.data = initialData;\r
41     }\r
42 \r
43     public boolean isEmpty() {\r
44         return data == null;\r
45     }\r
46 \r
47     public void set(T value) {\r
48         data = value;\r
49     }\r
50 \r
51     public T get() {\r
52         return data;\r
53     }\r
54 \r
55     public boolean hasContent() {\r
56         return data != null;\r
57     }\r
58 \r
59     @Override\r
60     public boolean add(T arg0) {\r
61         throw new UnsupportedOperationException();\r
62     }\r
63 \r
64     @Override\r
65     public boolean addAll(Collection<? extends T> arg0) {\r
66         throw new UnsupportedOperationException();\r
67     }\r
68 \r
69     @Override\r
70     public void clear() {\r
71         throw new UnsupportedOperationException();\r
72     }\r
73 \r
74     @Override\r
75     public boolean contains(Object arg0) {\r
76         throw new UnsupportedOperationException();\r
77     }\r
78 \r
79     @Override\r
80     public boolean containsAll(Collection<?> arg0) {\r
81         throw new UnsupportedOperationException();\r
82     }\r
83 \r
84     @Override\r
85     public Iterator<T> iterator() {\r
86 \r
87         return new Iterator<T>() {\r
88 \r
89             private T value = data;\r
90 \r
91             @Override\r
92             public boolean hasNext() {\r
93                 return value != null;\r
94             }\r
95 \r
96             @Override\r
97             public T next() {\r
98                 T result = value;\r
99                 value = null;\r
100                 return result;\r
101             }\r
102 \r
103             @Override\r
104             public void remove() {\r
105                 throw new UnsupportedOperationException();\r
106             }\r
107         };\r
108 \r
109     }\r
110 \r
111     @Override\r
112     public boolean remove(Object arg0) {\r
113         throw new UnsupportedOperationException();\r
114     }\r
115 \r
116     @Override\r
117     public boolean removeAll(Collection<?> arg0) {\r
118         throw new UnsupportedOperationException();\r
119     }\r
120 \r
121     @Override\r
122     public boolean retainAll(Collection<?> arg0) {\r
123         throw new UnsupportedOperationException();\r
124     }\r
125 \r
126     @Override\r
127     public int size() {\r
128         return data != null ? 1 : 0;\r
129     }\r
130 \r
131     @Override\r
132     public Object[] toArray() {\r
133         return new Object[] { data };\r
134     }\r
135 \r
136     @SuppressWarnings("unchecked")\r
137     @Override\r
138     public <K> K[] toArray(K[] a) {\r
139         int size = size();\r
140         if (a.length < size)\r
141             a = (K[]) Array.newInstance(a.getClass().getComponentType(), 1);\r
142         if (size > 0)\r
143             a[0] = (K) data;\r
144         for (int i = size; i < a.length; ++i)\r
145             a[i] = null;\r
146         return a;\r
147     }\r
148 \r
149 }\r