KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > universal > util > PreferenceArbiter


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.universal.util;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /*
17  * Accepts a set of Objects that represents each product's preference over some
18  * matter (e.g. where an item should appear in welcome) and provides a final ruling
19  * on which Object to use.
20  */

21 public class PreferenceArbiter {
22
23     private Map JavaDoc references;
24     private Object JavaDoc leader;
25     
26     public void consider(Object JavaDoc obj) {
27         if (obj != null) {
28             if (references == null) {
29                 references = new HashMap JavaDoc();
30             }
31             int[] count = (int[])references.get(obj);
32             if (count == null) {
33                 count = new int[] { 0 };
34                 references.put(obj, count);
35             }
36             ++count[0];
37             if (obj != leader) {
38                 if (leader == null) {
39                     leader = obj;
40                 }
41                 else if (count[0] > ((int[])references.get(leader))[0]) {
42                     leader = obj;
43                 }
44             }
45         }
46     }
47     
48     public Object JavaDoc getWinner() {
49         return leader;
50     }
51 }
52
Popular Tags