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

net.morimekta.providence.graphql.gql.GQLInlineFragment Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
package net.morimekta.providence.graphql.gql;

import net.morimekta.providence.descriptor.PMessageDescriptor;
import net.morimekta.util.collect.UnmodifiableList;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import java.util.List;
import java.util.Objects;

/**
 * An inline fragment, defining type-conditional field
 * in-place.
 *
 * {@inheritDoc}
 */
@Immutable
public class GQLInlineFragment implements GQLFragment {
    private final PMessageDescriptor descriptor;
    private final List    selectionSet;

    /**
     * Inline fragment with field set.
     *
     * @param descriptor Descriptor of type defined in this fragment.
     * @param selectionSet Field selectionSet for the fragment.
     */
    public GQLInlineFragment(@Nonnull PMessageDescriptor descriptor,
                             @Nonnull List selectionSet) {
        this.descriptor = descriptor;
        this.selectionSet = UnmodifiableList.copyOf(selectionSet);
    }

    @Nonnull
    @Override
    public PMessageDescriptor getTypeCondition() {
        return descriptor;
    }

    @Nonnull
    @Override
    public List getSelectionSet() {
        return selectionSet;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("... on ")
               .append(descriptor.getName())
               .append(" {");

        boolean first = true;
        for (GQLSelection entry : selectionSet) {
            if (first) {
                first = false;
            } else {
                builder.append(", ");
            }
            builder.append(entry.toString());
        }
        builder.append("}");
        return builder.toString();
    }

    @Override
    public int hashCode() {
        return Objects.hash(getClass(),
                            descriptor,
                            selectionSet);
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof GQLInlineFragment)) return false;
        GQLInlineFragment other = (GQLInlineFragment) o;
        return Objects.equals(descriptor, other.descriptor) &&
               Objects.equals(selectionSet, other.selectionSet);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy