GQLInlineFragment.java

  1. package net.morimekta.providence.graphql.gql;

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

  4. import javax.annotation.Nonnull;
  5. import javax.annotation.concurrent.Immutable;
  6. import java.util.List;
  7. import java.util.Objects;

  8. /**
  9.  * An inline fragment, defining type-conditional field
  10.  * in-place.
  11.  *
  12.  * {@inheritDoc}
  13.  */
  14. @Immutable
  15. public class GQLInlineFragment implements GQLFragment {
  16.     private final PMessageDescriptor<?> descriptor;
  17.     private final List<GQLSelection>    selectionSet;

  18.     /**
  19.      * Inline fragment with field set.
  20.      *
  21.      * @param descriptor Descriptor of type defined in this fragment.
  22.      * @param selectionSet Field selectionSet for the fragment.
  23.      */
  24.     public GQLInlineFragment(@Nonnull PMessageDescriptor<?> descriptor,
  25.                              @Nonnull List<GQLSelection> selectionSet) {
  26.         this.descriptor = descriptor;
  27.         this.selectionSet = UnmodifiableList.copyOf(selectionSet);
  28.     }

  29.     @Nonnull
  30.     @Override
  31.     public PMessageDescriptor<?> getTypeCondition() {
  32.         return descriptor;
  33.     }

  34.     @Nonnull
  35.     @Override
  36.     public List<GQLSelection> getSelectionSet() {
  37.         return selectionSet;
  38.     }

  39.     @Override
  40.     public String toString() {
  41.         StringBuilder builder = new StringBuilder();
  42.         builder.append("... on ")
  43.                .append(descriptor.getName())
  44.                .append(" {");

  45.         boolean first = true;
  46.         for (GQLSelection entry : selectionSet) {
  47.             if (first) {
  48.                 first = false;
  49.             } else {
  50.                 builder.append(", ");
  51.             }
  52.             builder.append(entry.toString());
  53.         }
  54.         builder.append("}");
  55.         return builder.toString();
  56.     }

  57.     @Override
  58.     public int hashCode() {
  59.         return Objects.hash(getClass(),
  60.                             descriptor,
  61.                             selectionSet);
  62.     }

  63.     @Override
  64.     public boolean equals(Object o) {
  65.         if (o == this) return true;
  66.         if (!(o instanceof GQLInlineFragment)) return false;
  67.         GQLInlineFragment other = (GQLInlineFragment) o;
  68.         return Objects.equals(descriptor, other.descriptor) &&
  69.                Objects.equals(selectionSet, other.selectionSet);
  70.     }
  71. }