Commit | Line | Data |
---|---|---|
12c155f5 | 1 | /******************************************************************************* |
2f2265e2 | 2 | * Copyright (c) 2011, 2012 Ericsson |
12c155f5 FC |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.util; |
12c155f5 FC |
14 | |
15 | import java.util.ArrayList; | |
16 | ||
17 | /** | |
2f2265e2 BH |
18 | * Implementation of a sorted array list. |
19 | * | |
20 | * @version 1.0 | |
21 | * @Francois Chouinard | |
12c155f5 FC |
22 | */ |
23 | ||
24 | public class TmfSortedArrayList<T> extends ArrayList<T> { | |
25 | private static final long serialVersionUID = 1L; | |
26 | ||
2f2265e2 BH |
27 | /** |
28 | * Inserts a new value in the list according to its sorted position. | |
29 | * | |
30 | * @param value A value to insert | |
31 | */ | |
12c155f5 FC |
32 | @SuppressWarnings("unchecked") |
33 | public void insertSorted(T value) { | |
34 | add(value); | |
35 | Comparable<T> cmp = (Comparable<T>) value; | |
36 | for (int pos = size() - 1; pos > 0 && cmp.compareTo(get(pos - 1)) < 0; pos--) { | |
37 | T tmp = get(pos); | |
38 | set(pos, get(pos - 1)); | |
39 | set(pos - 1, tmp); | |
40 | } | |
41 | } | |
42 | ||
43 | } |