Skip to main content
summaryrefslogtreecommitdiffstats
blob: 07f5dc568d5414881d698cc56e348ab9953e516f (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
/*******************************************************************************
 * Copyright (c) 2006 IBM Corporation and others.
 * 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 - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.intro.universal.util;

import java.util.HashMap;
import java.util.Map;

/*
 * Accepts a set of Objects that represents each product's preference over some
 * matter (e.g. where an item should appear in welcome) and provides a final ruling
 * on which Object to use.
 */
public class PreferenceArbiter {

	private Map<Object, int[]> references;
	private Object leader;

	public void consider(Object obj) {
		if (obj != null) {
			if (references == null) {
				references = new HashMap<>();
			}
			int[] count = references.get(obj);
			if (count == null) {
				count = new int[] { 0 };
				references.put(obj, count);
			}
			++count[0];
			if (obj != leader) {
				if (leader == null) {
					leader = obj;
				}
				else if (count[0] > (references.get(leader))[0]) {
					leader = obj;
				}
			}
		}
	}

	public Object getWinner() {
		return leader;
	}
}

Back to the top