001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.preferences;
011:
012: import java.util.Iterator;
013: import java.util.Set;
014:
015: /**
016: * @since 3.1
017: */
018: public class PropertyUtil {
019: private PropertyUtil() {
020: }
021:
022: public static boolean isEqual(IPropertyMap map1, IPropertyMap map2) {
023: Set map1Keys = map1.keySet();
024: Set map2Keys = map2.keySet();
025:
026: if (!map1Keys.equals(map2Keys)) {
027: return false;
028: }
029:
030: for (Iterator iter = map1Keys.iterator(); iter.hasNext();) {
031: String next = (String) iter.next();
032:
033: if (!map1.getValue(next, Object.class).equals(
034: map2.getValue(next, Object.class))) {
035: return false;
036: }
037: }
038:
039: return true;
040: }
041:
042: /**
043: * Copies all properties from the given source to the given destination
044: *
045: * @param destination
046: * @param source
047: * @since 3.1
048: */
049: public static void copy(IPropertyMap destination,
050: IPropertyMap source) {
051: Set keys = source.keySet();
052:
053: for (Iterator iter = keys.iterator(); iter.hasNext();) {
054: String key = (String) iter.next();
055:
056: destination.setValue(key, source
057: .getValue(key, Object.class));
058: }
059: }
060:
061: /**
062: * Computes the union of a set property maps. The result will contain all properties from
063: * all of the contributing maps. If the same property had a different value in two or
064: * more maps, its value in the union will be null. If the same property
065: *
066: * Note that the result is a standalone
067: * object and will not be updated to reflect subsequent changes in the source maps.
068: *
069: * @param sources
070: * @return
071: * @since 3.1
072: */
073: public static IPropertyMap union(IPropertyMap[] sources) {
074: PropertyMapUnion result = new PropertyMapUnion();
075:
076: for (int i = 0; i < sources.length; i++) {
077: IPropertyMap map = sources[i];
078:
079: result.addMap(map);
080: }
081:
082: return result;
083: }
084:
085: public static boolean get(IPropertyMap toRead, String propertyId,
086: boolean defaultValue) {
087: Boolean result = ((Boolean) toRead.getValue(propertyId,
088: Boolean.class));
089:
090: if (result == null) {
091: return defaultValue;
092: }
093:
094: return result.booleanValue();
095: }
096:
097: public static int get(IPropertyMap toRead, String propertyId,
098: int defaultValue) {
099: Integer result = ((Integer) toRead.getValue(propertyId,
100: Integer.class));
101:
102: if (result == null) {
103: return defaultValue;
104: }
105:
106: return result.intValue();
107: }
108: }
|