Skip to main content
summaryrefslogtreecommitdiffstats
blob: 0203c6da0bb10e6764b72c6f77002c299e5e009f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
 *******************************************************************************/

package org.eclipse.linuxtools.systemtap.graphingapi.core.filters;

import java.util.ArrayList;

import org.eclipse.linuxtools.systemtap.graphingapi.core.structures.NumberType;
import org.eclipse.linuxtools.systemtap.structures.Copier;
import org.eclipse.linuxtools.systemtap.structures.IndexedObject;
import org.eclipse.linuxtools.systemtap.structures.Sort;



public class SortFilter implements IDataSetFilter {
	public SortFilter(int column, int ordering) {
		this.column = column;
		this.style = (ordering==ASCENDING ? ASCENDING : DESCENDING);
	}

	/**
	 * Apply the RangeFilter to the passed dataset.
	 *
	 * @param data The dataset to filter.
	 *
	 * @return The filtered dataset.
	 */
	@Override
	public ArrayList<Object>[] filter(ArrayList<Object>[] data) {
		if(column < 0 || column >= data.length)
			return null;

		ArrayList<Object>[] newData = Copier.copy(data);
		IndexedObject[] items = new IndexedObject[newData[0].size()];

		try {
			for(int i=0; i<newData[column].size(); i++)
				items[i] = new IndexedObject(i, NumberType.cleanObj2Num(newData[column].get(i)));
		} catch(NumberFormatException nfe) {
			for(int i=0; i<newData[column].size(); i++)
				items[i] = new IndexedObject(i, newData[column].get(i));
		}

		Sort.quicksort(items, 0, items.length-1);

		for(int j, i=0; i<newData.length; i++) {
			for(j=0; j<items.length; j++) {
				if(DESCENDING == style)
					newData[i].add(newData[i].get(items[items.length-j-1].index));
				else
					newData[i].add(newData[i].get(items[j].index));
			}
			for(j=0; j<items.length; j++)
				newData[i].remove(0);
		}
		return newData;
	}

	@Override
	public String getID() {
		return ID;
	}

	private int column;
	private int style;

	public static final int ASCENDING = 0;
	public static final int DESCENDING = 1;
	public static final String ID = "org.eclipse.linuxtools.systemtap.graphingapi.core.filters.SortFilter"; //$NON-NLS-1$
}

Back to the top