Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce7f71a85fa003ee36be754e3da50201b08341bc (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, Inc. 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:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.persistence.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.tcf.te.runtime.interfaces.properties.IPropertiesContainer;
import org.eclipse.tcf.te.runtime.persistence.PersistenceManager;
import org.eclipse.tcf.te.runtime.persistence.interfaces.IPersistenceDelegate;
import org.eclipse.tcf.te.runtime.properties.PropertiesContainer;

/**
 * Data helper for de/encoding.
 */
public class DataHelper {

	/**
	 * Encode a properties container to a string.
	 * @param data The properties container.
	 * @return String representing the properties container.
	 */
	public static final String encodePropertiesContainer(IPropertiesContainer data) {
		try {
			if (data != null && !data.isEmpty()) {
				IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(Map.class, String.class);
				return (String)delegate.write(data, String.class);
			}
		}
		catch (Exception e) {
		}
		return null;
	}

	/**
	 * Decode a string encoded properties container.
	 * @param encoded The string encoded properties container.
	 * @return Properties container.
	 */
	public static final IPropertiesContainer decodePropertiesContainer(String encoded) {
		if (encoded != null && encoded.trim().length() > 0) {
			try {
				IPersistenceDelegate delegate = PersistenceManager.getInstance().getDelegate(Map.class, String.class);
				return (IPropertiesContainer)delegate.read(IPropertiesContainer.class, encoded);
			}
			catch (Exception e) {
			}
		}
		return new PropertiesContainer();
	}

	/**
	 * Convert all keys of the map to lower case.
	 * @param list The list of maps to convert.
	 * @return List with new maps with lowercase keys.
	 */
	public static List<Map<String,Object>> keysToLowerCase(List<Map<String,Object>> list) {
		List<Map<String, Object>> paramListLowerCase = new ArrayList<Map<String,Object>>();
		for (Map<String, Object> map : list) {
			paramListLowerCase.add(keysToLowerCase(map));
		}
		return paramListLowerCase;
	}

	/**
	 * Convert all keys of the map to lower case.
	 * @param map The map to convert.
	 * @return New map with lowercase keys.
	 */
	@SuppressWarnings("unchecked")
	public static Map<String,Object> keysToLowerCase(Map<String,Object> map) {
		Map<String, Object> mapLowerCase = new HashMap<String, Object>();
		for (String key : map.keySet()) {
			Object value = map.get(key);
			if (value instanceof Map) {
				value = keysToLowerCase((Map<String,Object>)value);
			}
			else if (value instanceof List) {
				value = keysToLowerCase((List<Map<String,Object>>)value);
			}
			mapLowerCase.put(key.toLowerCase(), value);
		}
		return mapLowerCase;
	}
}

Back to the top