blob: bf22da958e68e0ec699cec66576c4ffc3a4dae7e [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2012, 2021 Original NatTable authors and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0.
#
# SPDX-License-Identifier: EPL-2.0
#
# Contributors:
# Original NatTable authors and others - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ecommons.waltable.util;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class PersistenceUtils {
/**
* Parse the persisted property and create a TreeMap<Long, String> from it.
* Works in conjunction with the {@link PersistenceUtils#mapAsString(Map)}.
*
* @param property from the properties file.
*/
public static Map<Long, String> parseString(final Object property) {
final TreeMap<Long, String> map= new TreeMap<>();
if (property != null) {
final String value= (String) property;
final String[] renamedColumns= value.split("\\|"); //$NON-NLS-1$
for (final String token : renamedColumns) {
final String[] split= token.split(":"); //$NON-NLS-1$
final String id= split[0];
final String label= split[1];
map.put(Long.valueOf(id), label);
}
}
return map;
}
/**
* Convert the Map to a String suitable for persisting in the Properties file.
* {@link PersistenceUtils#parseString(Object)} can be used to reconstruct this Map object from the String.
*/
public static String mapAsString(final Map<Long, String> map) {
final StringBuffer buffer= new StringBuffer();
for (final Entry<Long, String> entry : map.entrySet()) {
buffer.append(entry.getKey() + ":" + entry.getValue() + "|"); //$NON-NLS-1$ //$NON-NLS-2$
}
return buffer.toString();
}
}