]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/persistent/ImmutableStack.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / persistent / ImmutableStack.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.persistent;
13
14 public abstract class ImmutableStack<T> {
15         
16         private ImmutableStack() {              
17         }
18         
19         private static class SingleStackNode<T> extends ImmutableStack<T> {
20                 ImmutableStack<T> parent;
21                 T value;
22                 
23                 public SingleStackNode(ImmutableStack<T> parent, T value) {             
24                         this.parent = parent;
25                         this.value = value;
26                 }
27
28                 @Override
29                 public T get(int i) {
30                         return i==0 ? value : parent.get(i-1);
31                 }       
32         }
33         
34         private static class MultiStackNode<T> extends ImmutableStack<T> {
35                 ImmutableStack<T> parent;
36                 T[] values;
37                 
38                 public MultiStackNode(ImmutableStack<T> parent, T[] values) {           
39                         this.parent = parent;
40                         this.values = values;
41                 }
42
43                 @Override
44                 public T get(int i) {
45                         return i<values.length ? values[i] : parent.get(i-values.length);
46                 }       
47         }
48         
49         private static class EmptyStack<T> extends ImmutableStack<T> {
50
51                 @Override
52                 public T get(int i) {
53                         throw new IllegalArgumentException("No such element in stack.");
54                 }
55                 
56         }
57         
58         @SuppressWarnings({ "rawtypes" })
59         static final EmptyStack EMPTY = new EmptyStack();
60         
61         public ImmutableStack<T> push(T value) {
62                 return new SingleStackNode<T>(this, value);
63         }
64         
65         public ImmutableStack<T> push(T[] values) {
66                 if(values.length > 1)
67                         return new MultiStackNode<T>(this, values);
68                 else if(values.length == 1)
69                         return new SingleStackNode<T>(this, values[0]);
70                 else
71                         return this;
72         }
73         
74         public abstract T get(int i); 
75         
76         @SuppressWarnings("unchecked")
77         public static <T> ImmutableStack<T> empty() {
78                 return (ImmutableStack<T>)EMPTY;
79         }
80         
81         public static <T> ImmutableStack<T> of(T value) {
82                 return new SingleStackNode<T>(null, value);
83         }
84         
85         @SuppressWarnings("unchecked")
86         public static <T> ImmutableStack<T> of(T[] values) {
87                 if(values.length > 1)
88                         return new MultiStackNode<T>((ImmutableStack<T>)empty(), values);
89                 else if(values.length == 1)
90                         return new SingleStackNode<T>((ImmutableStack<T>)empty(), values[0]);
91                 else
92                         return empty();
93         }
94 }