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

org.simpleflatmapper.map.property.KeyProperty Maven / Gradle / Ivy

Go to download

Java library to map flat record - ResultSet, csv - to java object with minimum configuration and low footprint.

There is a newer version: 9.0.2
Show newest version
package org.simpleflatmapper.map.property;


import org.simpleflatmapper.map.impl.DiscriminatorPropertyFinder;
import org.simpleflatmapper.map.impl.JoinUtils;
import org.simpleflatmapper.reflect.meta.PropertyMeta;
import org.simpleflatmapper.reflect.meta.SubPropertyMeta;
import org.simpleflatmapper.reflect.property.EligibleAsNonMappedProperty;
import org.simpleflatmapper.util.BiConsumer;
import org.simpleflatmapper.util.Predicate;

import java.lang.reflect.Type;

import static org.simpleflatmapper.util.Asserts.requireNonNull;

public class KeyProperty implements EligibleAsNonMappedProperty {

    private static final Predicate> DEFAULT_PREDICATE = new Predicate>() {
        @Override
        public boolean test(PropertyMeta propertyMeta) {
            // if the property is in a join ... not electable
//            if (JoinUtils.isArrayElement(propertyMeta)) {
//                return false;
//            }
            
            // for now assume discriminator property are fine are they?
            // what if we have conflicting result
            if (propertyMeta instanceof DiscriminatorPropertyFinder.DiscriminatorPropertyMeta) {
                DiscriminatorPropertyFinder.DiscriminatorPropertyMeta dpm = (DiscriminatorPropertyFinder.DiscriminatorPropertyMeta) propertyMeta;
                return dpm.forEachProperty(new BiConsumer>() {
                    boolean valid = false;
                    @Override
                    public void accept(Type type, PropertyMeta propertyMeta) {
                        if (!valid && test(propertyMeta)) {
                            valid = true;
                        }
                    }
                }).valid;
            }
            // where does the buck stop? on arrays
            if (propertyMeta.isSubProperty()) {
                SubPropertyMeta subPropertyMeta = (SubPropertyMeta) propertyMeta;
                if (JoinUtils.isArrayElement(subPropertyMeta.getOwnerProperty())) {
                    return false;
                } else {
                    PropertyMeta subProperty = subPropertyMeta.getSubProperty();
                    return !JoinUtils.isArrayElement(subProperty) && test(subProperty);
                }
            }
            return true; //!propertyMeta.isSubProperty();
        }
    };

    public static final KeyProperty DEFAULT = new KeyProperty(DEFAULT_PREDICATE);

    private final Predicate> appliesTo;

    public KeyProperty() {
        this(DEFAULT_PREDICATE);
    }

    public KeyProperty(Predicate> appliesTo) {
        this.appliesTo = requireNonNull("appliesTo", appliesTo);
    }

    public Predicate> getAppliesTo() {
        return appliesTo;
    }

    @Override
    public String toString() {
        return "Key{" + appliesTo + "}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        KeyProperty that = (KeyProperty) o;

        return appliesTo.equals(that.appliesTo);

    }

    @Override
    public int hashCode() {
        return appliesTo.hashCode();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy