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