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