/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.beam.sdk.transforms.join;

import static org.apache.beam.sdk.util.Structs.addList;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.beam.sdk.util.CloudObject;
import org.apache.beam.sdk.util.PropertyNames;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.sdk.values.TupleTagList;

/**
 * A schema for the results of a {@link CoGroupByKey}.  This maintains the full
 * set of {@link TupleTag}s for the results of a {@link CoGroupByKey} and
 * facilitates mapping between {@link TupleTag}s and
 * {@link RawUnionValue} tags (which are used as secondary keys in the
 * {@link CoGroupByKey}).
 */
public class CoGbkResultSchema implements Serializable {

  private final TupleTagList tupleTagList;

  @JsonCreator
  public static CoGbkResultSchema of(
      @JsonProperty(PropertyNames.TUPLE_TAGS) List<TupleTag<?>> tags) {
    TupleTagList tupleTags = TupleTagList.empty();
    for (TupleTag<?> tag : tags) {
      tupleTags = tupleTags.and(tag);
    }
    return new CoGbkResultSchema(tupleTags);
  }

  /**
   * Maps TupleTags to union tags.  This avoids needing to encode the tags
   * themselves.
   */
  private final HashMap<TupleTag<?>, Integer> tagMap = new HashMap<>();

  /**
   * Builds a schema from a tuple of {@code TupleTag<?>}s.
   */
  public CoGbkResultSchema(TupleTagList tupleTagList) {
    this.tupleTagList = tupleTagList;
    int index = -1;
    for (TupleTag<?> tag : tupleTagList.getAll()) {
      index++;
      tagMap.put(tag, index);
    }
  }

  /**
   * Returns the index for the given tuple tag, if the tag is present in this
   * schema, -1 if it isn't.
   */
  public int getIndex(TupleTag<?> tag) {
    Integer index = tagMap.get(tag);
    return index == null ? -1 : index;
  }

  /**
   * Returns the tuple tag at the given index.
   */
  public TupleTag<?> getTag(int index) {
    return tupleTagList.get(index);
  }

  /**
   * Returns the number of columns for this schema.
   */
  public int size() {
    return tupleTagList.getAll().size();
  }

  /**
   * Returns the TupleTagList tuple associated with this schema.
   */
  public TupleTagList getTupleTagList() {
    return tupleTagList;
  }

  public CloudObject asCloudObject() {
    CloudObject result = CloudObject.forClass(getClass());
    List<CloudObject> serializedTags = new ArrayList<>(tupleTagList.size());
    for (TupleTag<?> tag : tupleTagList.getAll()) {
      serializedTags.add(tag.asCloudObject());
    }
    addList(result, PropertyNames.TUPLE_TAGS, serializedTags);
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof CoGbkResultSchema)) {
      return false;
    }
    CoGbkResultSchema other = (CoGbkResultSchema) obj;
    return tupleTagList.getAll().equals(other.tupleTagList.getAll());
  }

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

  @Override
  public String toString() {
    return "CoGbkResultSchema: " + tupleTagList.getAll();
  }
}

@@ -849,172 +849,8 @@
 n;%0A%0A
-import static org.apache.beam.sdk.util.Structs.addList;%0A%0Aimport com.fasterxml.jackson.annotation.JsonCreator;%0Aimport com.fasterxml.jackson.annotation.JsonProperty;%0A
 impo
@@ -878,36 +878,8 @@
 le;%0A
-import java.util.ArrayList;%0A
 impo
@@ -927,100 +927,8 @@
 st;%0A
-import org.apache.beam.sdk.util.CloudObject;%0Aimport org.apache.beam.sdk.util.PropertyNames;%0A
 impo
@@ -1434,23 +1434,8 @@
 t;%0A%0A
-  @JsonCreator%0A
   pu
@@ -1471,55 +1471,8 @@
  of(
-%0A      @JsonProperty(PropertyNames.TUPLE_TAGS) 
 List
@@ -2810,375 +2810,8 @@
  %7D%0A%0A
-  public CloudObject asCloudObject() %7B%0A    CloudObject result = CloudObject.forClass(getClass());%0A    List%3CCloudObject%3E serializedTags = new ArrayList%3C%3E(tupleTagList.size());%0A    for (TupleTag%3C?%3E tag : tupleTagList.getAll()) %7B%0A      serializedTags.add(tag.asCloudObject());%0A    %7D%0A    addList(result, PropertyNames.TUPLE_TAGS, serializedTags);%0A    return result;%0A  %7D%0A%0A
   @O

Drop usage of Jackson/CloudObject from CoGbkResultSchema