All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dellroad.stuff.vaadin24.data.MapPropertySet Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2022 Archie L. Cobbs. All rights reserved.
 */

package org.dellroad.stuff.vaadin24.data;

import com.vaadin.flow.data.binder.PropertySet;
import com.vaadin.flow.shared.util.SharedUtil;

import java.util.Map;

/**
 * Straightforward implementation of {@link PropertySet} with property values stored in a {@link Map}.
 *
 * 

* This class is useful for building arbitrary property sets. * *

* Does not support sub-properties. */ @SuppressWarnings("serial") public class MapPropertySet extends SimplePropertySet> { @SuppressWarnings("unchecked") public MapPropertySet() { super((Class>)(Object)Map.class); } // Methods /** * Add a new property to this instance. * * @param name property name * @param type property type * @param caption property caption * @return newly created property definition * @throws IllegalArgumentException if any parameter is null * @throws IllegalArgumentException if {@code name} has already been added */ public Definition addPropertyDefinition(String name, Class type, String caption) { return this.addPropertyDefinition(name, type, caption, map -> this.get(map, type, name), (map, value) -> this.set(map, type, name, value)); } /** * Add a new property to this instance, deriving the caption automatically from the property name. * *

* Equivalent to: {@link #addPropertyDefinition(String, Class, String) addPropertyDefinition}{@code (name, type, * }{@link SharedUtil#camelCaseToHumanFriendly SharedUtil.camelCaseToHumanFriendly}{@code (name))}. * * @param name property name * @param type property type * @return newly created property definition * @throws IllegalArgumentException if either parameter is null * @throws IllegalArgumentException if {@code name} has already been added */ public Definition addPropertyDefinition(String name, Class type) { return this.addPropertyDefinition(name, type, SharedUtil.camelCaseToHumanFriendly(name)); } private V get(Map map, Class type, String name) { if (map == null) throw new IllegalArgumentException("null map"); try { return type.cast(map.get(name)); } catch (ClassCastException e) { throw new IllegalArgumentException("wrong type for \"" + name + "\" in map", e); } } private void set(Map map, Class type, String name, V value) { if (map == null) throw new IllegalArgumentException("null map"); try { map.put(name, type.cast(value)); } catch (ClassCastException e) { throw new IllegalArgumentException("wrong type for \"" + name + "\" in map", e); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy