| 31 | |
| 32 | |
| 33 | @Override |
| 34 | public boolean add(E e) |
| 35 | { |
| 36 | if(isEmpty()) |
| 37 | { |
| 38 | super.add(e); |
| 39 | return true; |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | int ind = Collections.binarySearch(this, e); |
| 44 | if (ind >= 0)//it is already in the list, |
| 45 | { |
| 46 | if (size() == maxSize)//pop the last, put this |
| 47 | { |
| 48 | this.remove(maxSize - 1); |
| 49 | super.add(ind, e); |
| 50 | } |
| 51 | else//not full yet, can jsut add |
| 52 | { |
| 53 | if(ind > size()) |
| 54 | super.add(e); |
| 55 | else |
| 56 | super.add(ind, e); |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | ind = -(ind + 1);//Now it is the point where it should be inserted |
| 63 | if (size() < maxSize) |
| 64 | super.add(ind, e); |
| 65 | else if (ind < maxSize) |
| 66 | { |
| 67 | this.remove(maxSize - 1); |
| 68 | super.add(ind, e); |
| 69 | } |
| 70 | else |
| 71 | return false; |
| 72 | |
| 73 | return true; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | public E first() |