KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > preferences > PropertyUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.preferences;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15
16 /**
17  * @since 3.1
18  */

19 public class PropertyUtil {
20     private PropertyUtil() {
21     }
22     
23     public static boolean isEqual(IPropertyMap map1, IPropertyMap map2) {
24         Set JavaDoc map1Keys = map1.keySet();
25         Set JavaDoc map2Keys = map2.keySet();
26         
27         if (!map1Keys.equals(map2Keys)) {
28             return false;
29         }
30         
31         for (Iterator JavaDoc iter = map1Keys.iterator(); iter.hasNext();) {
32             String JavaDoc next = (String JavaDoc) iter.next();
33             
34             if (!map1.getValue(next, Object JavaDoc.class).equals(map2.getValue(next, Object JavaDoc.class))) {
35                 return false;
36             }
37         }
38         
39         return true;
40     }
41     
42     /**
43      * Copies all properties from the given source to the given destination
44      *
45      * @param destination
46      * @param source
47      * @since 3.1
48      */

49     public static void copy(IPropertyMap destination, IPropertyMap source) {
50        Set JavaDoc keys = source.keySet();
51        
52        for (Iterator JavaDoc iter = keys.iterator(); iter.hasNext();) {
53            String JavaDoc key = (String JavaDoc) iter.next();
54         
55            destination.setValue(key, source.getValue(key, Object JavaDoc.class));
56        }
57     }
58     
59     /**
60      * Computes the union of a set property maps. The result will contain all properties from
61      * all of the contributing maps. If the same property had a different value in two or
62      * more maps, its value in the union will be null. If the same property
63      *
64      * Note that the result is a standalone
65      * object and will not be updated to reflect subsequent changes in the source maps.
66      *
67      * @param sources
68      * @return
69      * @since 3.1
70      */

71     public static IPropertyMap union(IPropertyMap[] sources) {
72         PropertyMapUnion result = new PropertyMapUnion();
73         
74         for (int i = 0; i < sources.length; i++) {
75             IPropertyMap map = sources[i];
76             
77             result.addMap(map);
78         }
79         
80         return result;
81     }
82     
83     public static boolean get(IPropertyMap toRead, String JavaDoc propertyId, boolean defaultValue) {
84         Boolean JavaDoc result = ((Boolean JavaDoc)toRead.getValue(propertyId, Boolean JavaDoc.class));
85         
86         if (result == null) {
87             return defaultValue;
88         }
89         
90         return result.booleanValue();
91     }
92     
93     public static int get(IPropertyMap toRead, String JavaDoc propertyId, int defaultValue) {
94         Integer JavaDoc result = ((Integer JavaDoc)toRead.getValue(propertyId, Integer JavaDoc.class));
95         
96         if (result == null) {
97             return defaultValue;
98         }
99         
100         return result.intValue();
101     }
102 }
103
Popular Tags