net.anwiba.commons.preferences.PreferenceUtilities Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of anwiba-commons-advanced Show documentation
Show all versions of anwiba-commons-advanced Show documentation
anwiba commons advanced library project
/*
* #%L
* anwiba commons advanced
* %%
* Copyright (C) 2007 - 2016 Andreas Bartels
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package net.anwiba.commons.preferences;
import net.anwiba.commons.utilities.parameter.IParameter;
import net.anwiba.commons.utilities.parameter.IParameters;
import net.anwiba.commons.utilities.parameter.Parameter;
import net.anwiba.commons.utilities.parameter.Parameters;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.prefs.Preferences;
public class PreferenceUtilities {
public static String[] createPath(final Preferences preferences) {
final List nodeNames = new ArrayList<>();
Preferences current = preferences;
do {
nodeNames.add(current.name());
} while ((current = current.parent()) != null);
Collections.reverse(nodeNames);
return nodeNames.toArray(new String[nodeNames.size()]);
}
public static void store(final IPreferenceNode preferenceNode) {
final String[] path = preferenceNode.getPath();
final IPreferences preferences = getPreferences(path);
final Iterable parameters = preferenceNode.getParameters().parameters();
for (final IParameter parameter : parameters) {
preferences.put(parameter.getName(), parameter.getValue());
}
preferences.flush();
}
public static IPreferences getPreferences(final String[] path) {
final UserPreferencesFactory factory = new UserPreferencesFactory();
return factory.create(path);
}
public static IParameters getParameters(final IPreferences preferences) {
final ArrayList parameters = new ArrayList<>();
for (final String key : preferences.keys()) {
parameters.add(new Parameter(key, preferences.get(key, null)));
}
return new Parameters(parameters);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy