AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / Ladder.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.rosetta;
23
24
25 /**
26  * A Ladder is a Stack like Storage Class, but where you can ascend and descend while
27  * the elements exists.
28  * 
29  * Like an extension ladder, you can make taller as you go
30  * 
31  * @author Jonathan
32  *
33  */
34 public class Ladder<T> {
35         public static final int DEFAULT_INIT_SIZE=8;
36         private final int init_size;
37         private int rung; // as in ladder
38         private Object[] struts;
39
40         public Ladder() {
41                 rung=0;
42                 init_size = DEFAULT_INIT_SIZE;
43                 struts=new Object[init_size];
44         }
45
46         public Ladder(int initSize) {
47                 rung=0;
48                 init_size = initSize;
49                 struts=new Object[init_size];
50         }
51
52         public void bottom() {
53                 rung = 0;
54         }
55         
56         public void top() {
57                 rung = struts.length-1;
58                 while(rung>0 && struts[rung]==null)--rung;
59         }
60         
61         public int howHigh() {
62                 return rung;
63         }
64         
65         public void jumpTo(int rung) {
66                 if(rung>=struts.length) {
67                         Object[] temp = new Object[init_size*((rung/init_size)+1)];
68                         System.arraycopy(struts, 0, temp, 0, struts.length);
69                         struts = temp;
70                 }
71                 this.rung = rung;
72         }
73         
74         public int height() {
75                 return struts.length;
76         }
77         
78         public void cutTo(int rungs) {
79                 Object[] temp = new Object[rungs];
80                 System.arraycopy(struts, 0, temp, 0, Math.min(rungs, struts.length));
81                 struts = temp;
82         }
83         
84         public void ascend() {
85                 ++rung;
86                 if(rung>=struts.length) {
87                         Object[] temp = new Object[struts.length+init_size];
88                         System.arraycopy(struts, 0, temp, 0, struts.length);
89                         struts = temp;
90                 }
91         }
92         
93         public void descend() {
94                 --rung;
95         }
96         
97         @SuppressWarnings("unchecked")
98         public T peek() {
99                 return (T)struts[rung];
100         }
101         
102         public void push(T t) {
103                 struts[rung]=t;
104         }
105         
106         @SuppressWarnings("unchecked")
107         public T pop() {
108                 T t = (T)struts[rung];
109                 struts[rung]=null;
110                 return t;
111         }
112
113 }