ca768ae5a5dcad4b17c8c3bbce175f2379ec5b15
[jalview.git] / src / jalview / util / ShiftList.java
1 package jalview.util;
2
3 import java.util.*;
4
5 /**
6  * ShiftList
7  * Simple way of mapping a linear series to a new linear range with new points introduced.
8  * Use at your own risk!
9  * <p>Title: ShiftList</p>
10  *
11  * <p>Description: </p>
12  *
13  * <p>Copyright: Copyright (c) 2004</p>
14  *
15  * <p>Company: Dundee University</p>
16  *
17  * @author not attributable
18  * @version 1.0
19  */
20 public class ShiftList
21 {
22     Vector shifts;
23   public ShiftList()
24   {
25     shifts = new Vector();
26   }
27
28   /**
29    * addShift
30    * @param pos start position for shift (in original reference frame)
31    * @param shift length of shift
32    */
33   public void addShift(int pos, int shift)
34   {
35     int sidx = 0;
36     int[] rshift=null;
37     while (sidx<shifts.size() && (rshift=(int[]) shifts.get(sidx))[0]<pos)
38       sidx++;
39     if (sidx==shifts.size())
40       shifts.insertElementAt(new int[] { pos, shift}, 0);
41     else
42       rshift[1]+=shift;
43   }
44
45   /**
46    * shift
47    *
48    * @param pos int
49    * @return int shifted position
50    */
51   public int shift(int pos)
52   {
53     if (shifts.size()==0)
54       return pos;
55     int shifted=pos;
56     int sidx=0;
57     int rshift[];
58     while (sidx<shifts.size()
59            &&
60            (rshift=((int[]) shifts.get(sidx++)))[0]<=pos) {
61       shifted += rshift[1];
62     }
63     return shifted;
64   }
65
66   /**
67    * clear all shifts
68    */
69   public void clear()
70   {
71     shifts.removeAllElements();
72   }
73
74 }