]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/SingletonCollection.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / SingletonCollection.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 package org.simantics.utils.datastructures;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Iterator;\r
16 \r
17 public class SingletonCollection<T> extends ImmutableSet<T> {\r
18 \r
19     T content;        \r
20     \r
21     public SingletonCollection(T content) {\r
22         this.content = content;\r
23     }\r
24 \r
25     @Override\r
26     public boolean contains(Object o) {\r
27         return content.equals(o);\r
28     }\r
29 \r
30     @Override\r
31     public boolean containsAll(Collection<?> c) {\r
32         for(Object o : c)\r
33             if(!content.equals(o))\r
34                 return false;\r
35         return true;\r
36     }\r
37 \r
38     @Override\r
39     public boolean isEmpty() {\r
40         return content == null;\r
41     }\r
42     \r
43     static class SingletonIterator<T> extends ImmutableIterator<T> {\r
44 \r
45         T content;\r
46         \r
47         public SingletonIterator(T content) {\r
48             this.content = content;\r
49         }\r
50 \r
51         @Override\r
52         public boolean hasNext() {\r
53             return(content != null);\r
54         }\r
55 \r
56         @Override\r
57         public T next() {\r
58             T ret = content;\r
59             content = null;\r
60             return ret;\r
61         }\r
62         \r
63     }\r
64 \r
65     @Override\r
66     public Iterator<T> iterator() {\r
67         return new SingletonIterator<T>(content);\r
68     }\r
69 \r
70     @Override\r
71     public int size() {\r
72         if(content == null)\r
73             return 0;\r
74         else\r
75             return 1;\r
76     }\r
77 \r
78 }\r