1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.rosetta;
\r
27 * A Ladder is a Stack like Storage Class, but where you can ascend and descend while
\r
28 * the elements exists.
\r
30 * Like an extension ladder, you can make taller as you go
\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
42 init_size = DEFAULT_INIT_SIZE;
\r
43 struts=new Object[init_size];
\r
46 public Ladder(int initSize) {
\r
48 init_size = initSize;
\r
49 struts=new Object[init_size];
\r
52 public void bottom() {
\r
57 rung = struts.length-1;
\r
58 while(rung>0 && struts[rung]==null)--rung;
\r
61 public int howHigh() {
\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
74 public int height() {
\r
75 return struts.length;
\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
84 public void ascend() {
\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
93 public void descend() {
\r
97 @SuppressWarnings("unchecked")
\r
99 return (T)struts[rung];
\r
102 public void push(T t) {
\r
106 @SuppressWarnings("unchecked")
\r
108 T t = (T)struts[rung];
\r