01: /*******************************************************************************
02: * Copyright (c) 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.internal.intro.universal.util;
11:
12: import java.util.HashMap;
13: import java.util.Map;
14:
15: /*
16: * Accepts a set of Objects that represents each product's preference over some
17: * matter (e.g. where an item should appear in welcome) and provides a final ruling
18: * on which Object to use.
19: */
20: public class PreferenceArbiter {
21:
22: private Map references;
23: private Object leader;
24:
25: public void consider(Object obj) {
26: if (obj != null) {
27: if (references == null) {
28: references = new HashMap();
29: }
30: int[] count = (int[]) references.get(obj);
31: if (count == null) {
32: count = new int[] { 0 };
33: references.put(obj, count);
34: }
35: ++count[0];
36: if (obj != leader) {
37: if (leader == null) {
38: leader = obj;
39: } else if (count[0] > ((int[]) references.get(leader))[0]) {
40: leader = obj;
41: }
42: }
43: }
44: }
45:
46: public Object getWinner() {
47: return leader;
48: }
49: }
|