Skip to content

Commit

Permalink
#163: improved type handling due to JavaFx Number problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Feb 17, 2016
1 parent 27032f1 commit 1a85b5c
Show file tree
Hide file tree
Showing 36 changed files with 242 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.bean.api;

import java.util.Collection;
import java.util.Set;

import javax.inject.Named;

import net.sf.mmm.util.exception.api.ObjectMismatchException;
Expand All @@ -27,6 +30,12 @@ public interface BeanAccess {
*/
Iterable<WritableProperty<?>> getProperties();

/**
* @return an {@link java.util.Collections#unmodifiableSet(Set) immutable} {@link Set} with the
* {@link WritableProperty#getName() names} of the {@link #getProperties() properties}.
*/
Set<String> getPropertyNames();

/**
* @return an {@link Iterable} with all defined {@link #getPropertyNameForAlias(String) aliases}.
*/
Expand Down Expand Up @@ -144,7 +153,7 @@ default <V, PROPERTY extends WritableProperty<V>> PROPERTY getOrCreateProperty(S
* @return the requested property.
*/
default <V, PROPERTY extends WritableProperty<V>> PROPERTY getOrCreateProperty(String name,
GenericType<V> valueType, Class<PROPERTY> propertyType) {
GenericType<? extends V> valueType, Class<PROPERTY> propertyType) {

WritableProperty<?> property = getProperty(name);
if (property != null) {
Expand Down Expand Up @@ -255,8 +264,8 @@ default <V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String
* @param propertyType the Class reflecting the {@link WritableProperty} to create.
* @return the newly created property.
*/
<V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name, GenericType<V> valueType,
Class<PROPERTY> propertyType);
<V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name,
GenericType<? extends V> valueType, Class<PROPERTY> propertyType);

/**
* This method updates a given {@link WritableProperty property} such that the provided {@link AbstractValidator
Expand All @@ -267,11 +276,26 @@ <V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name, G
* @param <PROPERTY> the generic type of the {@link WritableProperty property}.
* @param property the {@link WritableProperty property} to update. Has to be owned by the {@link Bean#access()
* owning} {@link Bean}.
* @param validators are the {@link AbstractValidator validators} to add. The implementation tries its best to be
* @param validator is the {@link AbstractValidator validator} to add. The implementation tries its best to be
* idempotent so adding the same validator again should have no effect.
*/
<V, PROPERTY extends WritableProperty<V>> void addPropertyValidator(WritableProperty<?> property,
@SuppressWarnings("unchecked") AbstractValidator<? super V>... validators);
AbstractValidator<? super V> validator);

/**
* This method updates a given {@link WritableProperty property} such that the provided {@link AbstractValidator
* validator} is added. Therefore the {@link Bean} has to be a {@link #isDynamic() dynamic} {@link #isPrototype()
* prototype} that is not {@link #isReadOnly() read-only}.
*
* @param <V> the generic type of the {@link WritableProperty#getValue() property value}.
* @param <PROPERTY> the generic type of the {@link WritableProperty property}.
* @param property the {@link WritableProperty property} to update. Has to be owned by the {@link Bean#access()
* owning} {@link Bean}.
* @param validators is the {@link Collection} with the {@link AbstractValidator validators} to add. The
* implementation tries its best to be idempotent so adding the same validator again should have no effect.
*/
<V, PROPERTY extends WritableProperty<V>> void addPropertyValidators(WritableProperty<?> property,
Collection<AbstractValidator<? super V>> validators);

/**
* @see BeanFactory#getReadOnlyBean(Bean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
*/
public interface EntityBean<ID> extends Bean {

/** {@link WritableProperty#getName() Property name} of {@link #Id()}. */
String PROPERTY_NAME_ID = "Id";

/** {@link WritableProperty#getName() Property name} of {@link #Version()}. */
String PROPERTY_NAME_VERSION = "Version";

/**
* @return the {@link WritableProperty property} containing the unique ID (primary key).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Collection;
import java.util.Iterator;
import java.util.Set;

import net.sf.mmm.util.bean.api.Bean;
import net.sf.mmm.util.bean.api.BeanAccess;
Expand Down Expand Up @@ -78,6 +79,12 @@ public String getPropertyNameForAlias(String alias) {
return this.prototype.getPropertyNameForAlias(alias);
}

@Override
public Set<String> getPropertyNames() {

return this.prototype.getPropertyNames();
}

@Override
protected WritableProperty<?> getProperty(BeanPrototypeProperty prototypeProperty, boolean required) {

Expand Down Expand Up @@ -105,8 +112,8 @@ public <V> WritableProperty<V> createProperty(String name, GenericType<V> type)

@SuppressWarnings("unchecked")
@Override
public <V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name, GenericType<V> valueType,
Class<PROPERTY> propertyType) {
public <V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name,
GenericType<? extends V> valueType, Class<PROPERTY> propertyType) {

if (isReadOnly()) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -156,9 +163,16 @@ public boolean isPrototype() {

@Override
public <V, PROPERTY extends WritableProperty<V>> void addPropertyValidator(WritableProperty<?> property,
@SuppressWarnings("unchecked") AbstractValidator<? super V>... validators) {
AbstractValidator<? super V> validator) {

throw new UnsupportedOperationException("No prototype!");
}

@Override
public <V, PROPERTY extends WritableProperty<V>> void addPropertyValidators(WritableProperty<?> property,
Collection<AbstractValidator<? super V>> validators) {

addPropertyValidator(property, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -47,6 +49,8 @@ public class BeanAccessPrototype<BEAN extends Bean> extends BeanAccessBase<BEAN>

private final Collection<String> aliases;

private final Set<String> propertyNames;

private final boolean dynamic;

private final BeanFactoryImpl beanFactory;
Expand All @@ -59,13 +63,7 @@ public class BeanAccessPrototype<BEAN extends Bean> extends BeanAccessBase<BEAN>
* @param beanFactory the owning {@link BeanFactoryImpl}.
*/
public BeanAccessPrototype(Class<BEAN> beanClass, String name, BeanFactoryImpl beanFactory) {
super(beanClass, name, beanFactory);
this.name2PropertyMap = new HashMap<>();
this.method2OperationMap = new HashMap<>();
this.aliasMap = new HashMap<>();
this.aliases = Collections.unmodifiableSet(this.aliasMap.keySet());
this.dynamic = false;
this.beanFactory = beanFactory;
this(beanClass, name, beanFactory, null, false);
}

/**
Expand All @@ -77,19 +75,34 @@ public BeanAccessPrototype(Class<BEAN> beanClass, String name, BeanFactoryImpl b
*/
public BeanAccessPrototype(BeanAccessPrototype<BEAN> master, boolean dynamic, String name) {

super(master.getBeanClass(), name, master.beanFactory);
this.name2PropertyMap = new HashMap<>(master.name2PropertyMap.size());
this.aliasMap = master.aliasMap;
this.aliases = master.aliases;
for (BeanPrototypeProperty prototypeProperty : master.name2PropertyMap.values()) {
AbstractProperty<?> property = prototypeProperty.getProperty();
BeanPrototypeProperty copy = new BeanPrototypeProperty(property.copy(getBean()),
prototypeProperty.getIndex());
this.name2PropertyMap.put(property.getName(), copy);
this(master.getBeanClass(), name, master.beanFactory, master, dynamic);
}

private BeanAccessPrototype(Class<BEAN> beanClass, String name, BeanFactoryImpl beanFactory,
BeanAccessPrototype<BEAN> master, boolean dynamic) {

super(beanClass, name, beanFactory);
if (master == null) {
this.name2PropertyMap = new HashMap<>();
this.aliasMap = new HashMap<>();
this.aliases = Collections.unmodifiableSet(this.aliasMap.keySet());
this.method2OperationMap = new HashMap<>();
} else {
this.name2PropertyMap = new HashMap<>(master.name2PropertyMap.size());
this.aliasMap = master.aliasMap;
this.aliases = master.aliases;
this.method2OperationMap = master.method2OperationMap;
// copy properties from master to new prototype
for (BeanPrototypeProperty prototypeProperty : master.name2PropertyMap.values()) {
AbstractProperty<?> property = prototypeProperty.getProperty();
BeanPrototypeProperty copy = new BeanPrototypeProperty(property.copy(getBean()),
prototypeProperty.getIndex());
this.name2PropertyMap.put(property.getName(), copy);
}
}
this.method2OperationMap = master.method2OperationMap;
this.propertyNames = Collections.unmodifiableSet(this.name2PropertyMap.keySet());
this.dynamic = dynamic;
this.beanFactory = master.beanFactory;
this.beanFactory = beanFactory;
}

@Override
Expand All @@ -98,6 +111,12 @@ protected BeanAccessPrototype<BEAN> getPrototype() {
return this;
}

@Override
public Set<String> getPropertyNames() {

return this.propertyNames;
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Iterator<WritableProperty<?>> iterator() {
Expand All @@ -107,8 +126,8 @@ public Iterator<WritableProperty<?>> iterator() {

@SuppressWarnings("unchecked")
@Override
public <V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name, GenericType<V> valueType,
Class<PROPERTY> propertyType) {
public <V, PROPERTY extends WritableProperty<V>> PROPERTY createProperty(String name,
GenericType<? extends V> valueType, Class<PROPERTY> propertyType) {

if (!this.dynamic) {
throw new ReadOnlyException(getBeanClass().getSimpleName(), "access.properties");
Expand Down Expand Up @@ -235,10 +254,17 @@ public boolean isPrototype() {
return true;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <V, PROPERTY extends WritableProperty<V>> void addPropertyValidator(WritableProperty<?> property,
AbstractValidator<? super V>... validators) {
AbstractValidator<? super V> validator) {

addPropertyValidators(property, Arrays.asList(validator));
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public <V, PROPERTY extends WritableProperty<V>> void addPropertyValidators(WritableProperty<?> property,
Collection<AbstractValidator<? super V>> validators) {

Objects.requireNonNull(property, "property");
Objects.requireNonNull(validators, "validators");
Expand All @@ -255,13 +281,14 @@ public <V, PROPERTY extends WritableProperty<V>> void addPropertyValidator(Writa
}
AbstractProperty<V> p = (AbstractProperty<V>) property;
AbstractValidator<? super V> currentValidator = p.getValidator();
Collection<AbstractValidator<? super V>> validatorsToAdd = new ArrayList<>(validators.length);
for (int i = 0; i < validators.length; i++) {
AbstractValidator<? super V> validator = validators[i];
Collection<AbstractValidator<? super V>> validatorsToAdd = new ArrayList<>(validators.size());
int i = 0;
for (AbstractValidator<? super V> validator : validators) {
if (validator == null) {
Objects.requireNonNull(validator, "validators[" + i + "]");
}
addValidator(currentValidator, validatorsToAdd, validator);
i++;
}
if (validatorsToAdd.isEmpty()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,10 @@ protected <V> AbstractProperty<V> createProperty(String name, GenericType<V> typ
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
protected <V, PROPERTY extends ReadableProperty<V>> AbstractProperty<V> createProperty(String name,
GenericType<V> valueType, Bean bean, Class<PROPERTY> propertyClass) {
GenericType<? extends V> valueType, Bean bean, Class<PROPERTY> propertyClass) {

AbstractProperty result;
Class<V> valueClass = null;
Class<? extends V> valueClass = null;
if (valueType != null) {
valueClass = valueType.getRetrievalClass();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* @author hohwille
* @since 8.0.0
*/
public abstract class AbstractContainerProperty<VALUE> extends AbstractGenericProperty<VALUE>
public abstract class AbstractContainerProperty<VALUE> extends AbstractValueProperty<VALUE>
implements ReadableContainerProperty<VALUE> {

private final GenericType<VALUE> type;
private final GenericType<? extends VALUE> type;

private SizeProperty sizeProperty;

Expand All @@ -30,7 +30,7 @@ public abstract class AbstractContainerProperty<VALUE> extends AbstractGenericPr
* @param type - see {@link #getType()}.
* @param bean - see {@link #getBean()}.
*/
public AbstractContainerProperty(String name, GenericType<VALUE> type, Bean bean) {
public AbstractContainerProperty(String name, GenericType<? extends VALUE> type, Bean bean) {
this(name, type, bean, null);
}

Expand All @@ -42,14 +42,14 @@ public AbstractContainerProperty(String name, GenericType<VALUE> type, Bean bean
* @param bean - see {@link #getBean()}.
* @param validator - see {@link #validate()}.
*/
public AbstractContainerProperty(String name, GenericType<VALUE> type, Bean bean,
public AbstractContainerProperty(String name, GenericType<? extends VALUE> type, Bean bean,
AbstractValidator<? super VALUE> validator) {
super(name, bean, validator);
this.type = type;
}

@Override
public GenericType<VALUE> getType() {
public GenericType<? extends VALUE> getType() {

return this.type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @since 8.0.0
*/
@SuppressWarnings("restriction")
public abstract class AbstractRegularProperty<VALUE> extends AbstractGenericProperty<VALUE> {
public abstract class AbstractRegularProperty<VALUE> extends AbstractValueProperty<VALUE> {

private ExpressionHelper<VALUE> helper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* @author hohwille
* @since 8.0.0
*/
public abstract class AbstractGenericProperty<VALUE> extends AbstractProperty<VALUE> {
public abstract class AbstractValueProperty<VALUE> extends AbstractProperty<VALUE> {

private ValidationFailure validationResult;

Expand All @@ -37,7 +37,7 @@ public abstract class AbstractGenericProperty<VALUE> extends AbstractProperty<VA
* @param name - see {@link #getName()}.
* @param bean - see {@link #getBean()}.
*/
public AbstractGenericProperty(String name, Bean bean) {
public AbstractValueProperty(String name, Bean bean) {
this(name, bean, null);
}

Expand All @@ -48,14 +48,14 @@ public AbstractGenericProperty(String name, Bean bean) {
* @param bean - see {@link #getBean()}.
* @param validator - see {@link #validate()}.
*/
public AbstractGenericProperty(String name, Bean bean, AbstractValidator<? super VALUE> validator) {
public AbstractValueProperty(String name, Bean bean, AbstractValidator<? super VALUE> validator) {
super(name, bean, validator);
}

@Override
protected AbstractGenericProperty<VALUE> copy() {
protected AbstractValueProperty<VALUE> copy() {

AbstractGenericProperty<VALUE> copy = (AbstractGenericProperty<VALUE>) super.copy();
AbstractValueProperty<VALUE> copy = (AbstractValueProperty<VALUE>) super.copy();
copy.binding = null;
copy.bindingListener = null;
copy.readOnlyProperty = null;
Expand Down
Loading

0 comments on commit 1a85b5c

Please sign in to comment.