/* ================================================================== * SettingsUtil.java - 26/06/2015 2:46:00 pm * * Copyright 2007-2015 SolarNetwork.net Dev Team * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 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 Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * 02111-1307 USA * ================================================================== */ package net.solarnetwork.node.settings.support; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; import net.solarnetwork.node.settings.GroupSettingSpecifier; import net.solarnetwork.node.settings.SettingSpecifier; /** * Helper utilities for settings. * * @author matt * @version 1.0 */ public final class SettingsUtil { private SettingsUtil() { // Do not construct me. } /** * API to map a list element into a set of {@link SettingSpecifier} objects. * * @param <T> * The collection type. */ public interface KeyedListCallback<T> { /** * Map a single list element value into one or more * {@link SettingSpecifier} objects. * * @param value * The list element value. * @param index * The list element index. * @param key * An indexed key prefix to use for the grouped settings. * @return The settings. */ public Collection<SettingSpecifier> mapListSettingKey(T value, int index, String key); } /** * Get a dynamic list {@link GroupSettingSpecifier}. * * @param collection * The collection to turn into settings. * @param mapper * A helper to map individual elements into settings. * @return The resulting {@link GroupSettingSpecifier}. */ public static <T> BasicGroupSettingSpecifier dynamicListSettingSpecifier(String key, Collection<T> collection, KeyedListCallback<T> mapper) { List<SettingSpecifier> listStringGroupSettings; if ( collection == null ) { listStringGroupSettings = Collections.emptyList(); } else { final int len = collection.size(); listStringGroupSettings = new ArrayList<SettingSpecifier>(len); int i = 0; for ( T value : collection ) { Collection<SettingSpecifier> res = mapper.mapListSettingKey(value, i, key + "[" + i + "]"); i++; if ( res != null ) { listStringGroupSettings.addAll(res); } } } return new BasicGroupSettingSpecifier(key, listStringGroupSettings, true); } }