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

net.intelie.pipes.PropertyGroup Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
package net.intelie.pipes;

import net.intelie.pipes.filters.ObjectSink;
import net.intelie.pipes.types.Type;
import net.intelie.pipes.util.Escapes;
import net.intelie.pipes.util.Iterables;
import net.intelie.pipes.util.Preconditions;

import java.io.Serializable;
import java.util.*;

public class PropertyGroup implements Serializable, PropertySink {
    private static final long serialVersionUID = 1L;

    private final List properties;
    private final Type type;
    private final List propertyNames;

    public PropertyGroup(PropertySource src, Type type, String... properties) throws PipeException {
        this(src, type, Arrays.asList(properties));
    }

    public PropertyGroup(PropertySource src, Type type, List properties) throws PipeException {
        this(makeProperties(src, type, properties));
    }

    public PropertyGroup(Property property) {
        this(Collections.singletonList(property));
    }

    private PropertyGroup(List properties) {
        Preconditions.checkArgument(properties.size() > 0, "must have some property");
        this.properties = properties;
        this.type = properties.get(properties.size() - 1).type();
        this.propertyNames = new ArrayList<>(properties.size());
        for (Property property : properties) {
            propertyNames.add(property.name());
        }
    }

    private static List makeProperties(PropertySource src, Type type, List properties) throws PipeException {
        List list = new ArrayList<>(properties.size());
        for (int i = 0; i < properties.size(); i++) {
            String propertyName = properties.get(i);
            Property property = src.property(propertyName);
            if (i < properties.size() - 1) {
                property = property.hint(Type.OBJECT);
            } else if (type != null || !Type.STRING.equals(property.type()) && !Type.NUMBER.equals(property.type())) {
                property = property.hint(type);
            }

            list.add(property);
            src = src.newSource(src.metadata().withType(property.type()));
        }

        return list;
    }

    public Type type() {
        return type;
    }

    public List propertyNames() {
        return Collections.unmodifiableList(propertyNames);
    }

    public String firstProperty() {
        return properties.get(0).name();
    }

    public boolean isProperty(String name) {
        return properties.size() == 1 && Objects.equals(firstProperty(), name);
    }

    @Override
    public String toString() {
        String s = displayName();
        Type t = type();
        if (Type.STRING.equals(t) || Type.NUMBER.equals(t))
            s = t.makeString(s);
        return s;
    }

    public String displayName() {
        List parts = new ArrayList<>(properties.size());
        for (Property property : properties) {
            parts.add(Escapes.formatIdentifier(property.name()));
        }
        return Iterables.join("->", parts);
    }

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

        PropertyGroup that = (PropertyGroup) o;

        Iterator it1 = this.properties.iterator();
        Iterator it2 = that.properties.iterator();

        while (it1.hasNext() && it2.hasNext())
            if (!Objects.equals(it1.next().name(), it2.next().name()))
                return false;
        return it1.hasNext() == it2.hasNext() && Objects.equals(this.type, that.type);
    }

    @Override
    public int hashCode() {
        int hash = type.hashCode();

        for (Property property : properties) {
            hash *= 31;
            hash += Objects.hash(property.name());
        }

        return hash;
    }

    public void evalRaw(Scope scope, Object obj, ObjectSink sink) {
        for (int i = 0; i < properties.size() - 1; i++) {
            Property property = properties.get(i);
            Object obj2 = property.eval(scope, obj);
            scope = new Scope(scope, obj);
            obj = obj2;
        }
        properties.get(properties.size() - 1).evalRaw(scope, obj, sink);
    }

    @Override
    public PropertyVisitor visit(Scope scope, PropertyVisitor visitor) {
        for (Property property : properties) {
            PropertyVisitor visitor2 = property.visit(scope, visitor);
            scope = new Scope(scope, visitor);
            visitor = visitor2;
        }
        return visitor;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy