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

com.flextrade.builder.model.FieldFinder Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
package com.flextrade.builder.model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;

public class FieldFinder {

    private final HashMap getters = new HashMap<>();
    private final HashMap setters = new HashMap<>();

    private final Stream fields;

    public FieldFinder(Class clazz) {
        fields = createFields(clazz);
    }

    private Stream createFields(Class clazz) {
        Arrays.asList(clazz.getMethods()).stream().map(MethodWrapper::new).forEach(this::findGettersAndSetters);

        Set fieldNames = new HashSet<>(getters.keySet());
        fieldNames.retainAll(setters.keySet());

        return fieldNames.stream().map(fieldName -> new Property(getters.get(fieldName), setters.get(fieldName)));
    }

    private void findGettersAndSetters(MethodWrapper methodWrapper) {
        if(methodWrapper.isGetter())
            getters.put(methodWrapper.getFieldName(), methodWrapper);
        else if(methodWrapper.isSetter())
            setters.put(methodWrapper.getFieldName(), methodWrapper);
    }

    public Stream getFields() {
        return fields;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy