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