]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ImmutableSList.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / ImmutableSList.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 import java.util.NoSuchElementException;\r
17 \r
18 \r
19 public class ImmutableSList<T> extends ImmutableCollection<T> {\r
20         \r
21         final public T                 head;\r
22         final public ImmutableSList<T> tail; // null, if this is the last element\r
23         \r
24         public ImmutableSList(T head, ImmutableSList<T> tail) {\r
25                 this.head = head;\r
26                 this.tail = tail;\r
27         }\r
28         \r
29         public ImmutableSList(T head) {\r
30                 this(head, null);\r
31         }\r
32         \r
33         @Override\r
34         public boolean contains(Object o) {\r
35                 ImmutableSList<T> cur = this;\r
36                 do {\r
37                         if(cur.head.equals(o))\r
38                                 return true;\r
39                         cur = cur.tail;\r
40                 } while(cur != null);\r
41                 return false;   \r
42         }\r
43         @Override\r
44         public boolean containsAll(Collection<?> c) {\r
45                 for(Object o : c)\r
46                         if(!contains(o))\r
47                                 return false;\r
48                 return true;\r
49         }\r
50         @Override\r
51         public boolean isEmpty() {\r
52                 return false;\r
53         }\r
54         \r
55         static private class Iter<T> implements Iterator<T> {\r
56                 public ImmutableSList<T> cur;\r
57                 \r
58                 public Iter(ImmutableSList<T> cur) {\r
59                         this.cur = cur;\r
60                 }\r
61 \r
62                 @Override\r
63                 public boolean hasNext() {\r
64                         return cur != null;\r
65                 }\r
66 \r
67                 @Override\r
68                 public T next() {\r
69                         if(cur == null)\r
70                                 throw new NoSuchElementException();\r
71                         T ret = cur.head;\r
72                         cur = cur.tail;\r
73                         return ret;\r
74                 }\r
75 \r
76                 @Override\r
77                 public void remove() {\r
78                         throw new UnsupportedOperationException();\r
79                 }\r
80                 \r
81         }\r
82         \r
83         @Override\r
84         public Iterator<T> iterator() {\r
85                 return new Iter<T>(this);\r
86         }\r
87         @Override\r
88         public int size() {\r
89                 ImmutableSList<T> cur = this;\r
90                 int count = 0;\r
91                 do {\r
92                         ++count;\r
93                         cur = cur.tail;\r
94                 } while(cur != null);\r
95                 return count;   \r
96         }\r
97         \r
98         \r
99 \r
100 }\r