--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 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
+/*\r
+ * Created on 16.12.2005\r
+ * \r
+ */\r
+package org.simantics.utils;\r
+\r
+import java.lang.reflect.Array;\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+\r
+\r
+/**\r
+ * DataContainer is used as return argument\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public final class DataContainer<T> implements Container<T>, Collection<T> {\r
+\r
+ private T data;\r
+\r
+ public static <T> DataContainer<T> make(T t) {\r
+ return new DataContainer<T>(t);\r
+ }\r
+\r
+ public DataContainer() {\r
+ }\r
+\r
+ public DataContainer(T initialData) {\r
+ this.data = initialData;\r
+ }\r
+\r
+ public boolean isEmpty() {\r
+ return data == null;\r
+ }\r
+\r
+ public void set(T value) {\r
+ data = value;\r
+ }\r
+\r
+ public T get() {\r
+ return data;\r
+ }\r
+\r
+ public boolean hasContent() {\r
+ return data != null;\r
+ }\r
+\r
+ @Override\r
+ public boolean add(T arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public boolean addAll(Collection<? extends T> arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public void clear() {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public boolean contains(Object arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public boolean containsAll(Collection<?> arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public Iterator<T> iterator() {\r
+\r
+ return new Iterator<T>() {\r
+\r
+ private T value = data;\r
+\r
+ @Override\r
+ public boolean hasNext() {\r
+ return value != null;\r
+ }\r
+\r
+ @Override\r
+ public T next() {\r
+ T result = value;\r
+ value = null;\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public void remove() {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+ };\r
+\r
+ }\r
+\r
+ @Override\r
+ public boolean remove(Object arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public boolean removeAll(Collection<?> arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public boolean retainAll(Collection<?> arg0) {\r
+ throw new UnsupportedOperationException();\r
+ }\r
+\r
+ @Override\r
+ public int size() {\r
+ return data != null ? 1 : 0;\r
+ }\r
+\r
+ @Override\r
+ public Object[] toArray() {\r
+ return new Object[] { data };\r
+ }\r
+\r
+ @SuppressWarnings("unchecked")\r
+ @Override\r
+ public <K> K[] toArray(K[] a) {\r
+ int size = size();\r
+ if (a.length < size)\r
+ a = (K[]) Array.newInstance(a.getClass().getComponentType(), 1);\r
+ if (size > 0)\r
+ a[0] = (K) data;\r
+ for (int i = size; i < a.length; ++i)\r
+ a[i] = null;\r
+ return a;\r
+ }\r
+\r
+}\r