/*
 * 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.coders;

import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.List;
import org.apache.beam.sdk.values.TypeDescriptor;

/**
 * An abstract base class to implement a {@link Coder} that defines equality, hashing, and printing
 * via the class name and recursively using {@link #getComponents}.
 *
 * <p>A {@link StructuredCoder} should be defined purely in terms of its component coders, and
 * contain no additional configuration.
 *
 * <p>To extend {@link StructuredCoder}, override the following methods as appropriate:
 *
 * <ul>
 *   <li>{@link #getComponents}: the default implementation returns {@link #getCoderArguments}.
 *   <li>{@link #getEncodedElementByteSize} and {@link #isRegisterByteSizeObserverCheap}: the
 *       default implementation encodes values to bytes and counts the bytes, which is considered
 *       expensive. The default element byte size observer uses the value returned by
 *       {@link #getEncodedElementByteSize}.
 * </ul>
 */
public abstract class StructuredCoder<T> extends Coder<T> {
  protected StructuredCoder() {}

  /**
   * Returns the list of {@link Coder Coders} that are components of this {@link Coder}.
   *
   * <p>The default components will be equal to the value returned by {@link #getCoderArguments()}.
   */
  public List<? extends Coder<?>> getComponents() {
    List<? extends Coder<?>> coderArguments = getCoderArguments();
    if (coderArguments == null) {
      return Collections.emptyList();
    } else {
      return coderArguments;
    }
  }

  /**
   * {@inheritDoc}
   *
   * @return {@code true} if the two {@link StructuredCoder} instances have the
   * same class and equal components.
   */
  @Override
  public boolean equals(Object o) {
    if (o == null || this.getClass() != o.getClass()) {
      return false;
    }
    StructuredCoder<?> that = (StructuredCoder<?>) o;
    return this.getComponents().equals(that.getComponents());
  }

  @Override
  public int hashCode() {
    return getClass().hashCode() * 31 + getComponents().hashCode();
  }

  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder();
    String s = getClass().getName();
    builder.append(s.substring(s.lastIndexOf('.') + 1));

    List<? extends Coder<?>> componentCoders = getComponents();
    if (!componentCoders.isEmpty()) {
      builder.append('(');
      boolean first = true;
      for (Coder<?> componentCoder : componentCoders) {
        if (first) {
          first = false;
        } else {
          builder.append(',');
        }
        builder.append(componentCoder.toString());
      }
      builder.append(')');
    }
    return builder.toString();
  }

  /**
   * {@inheritDoc}
   *
   * @return {@code false} for {@link StructuredCoder} unless overridden.
   */
  @Override
  public boolean consistentWithEquals() {
    return false;
  }

  @Override
  public Object structuralValue(T value) {
    if (value != null && consistentWithEquals()) {
      return value;
    } else {
      try {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        encode(value, os, Context.OUTER);
        return new StructuralByteArray(os.toByteArray());
      } catch (Exception exn) {
        throw new IllegalArgumentException(
            "Unable to encode element '" + value + "' with coder '" + this + "'.", exn);
      }
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public TypeDescriptor<T> getEncodedTypeDescriptor() {
    return (TypeDescriptor<T>)
        TypeDescriptor.of(getClass()).resolveType(new TypeDescriptor<T>() {}.getType());
  }
}

@@ -840,46 +840,8 @@
 s;%0A%0A
-import java.io.ByteArrayOutputStream;%0A
 impo
@@ -892,58 +892,8 @@
 ist;
-%0Aimport org.apache.beam.sdk.values.TypeDescriptor;
 %0A%0A/*
@@ -3428,917 +3428,6 @@
  %7D%0A%0A
-  /**%0A   * %7B@inheritDoc%7D%0A   *%0A   * @return %7B@code false%7D for %7B@link StructuredCoder%7D unless overridden.%0A   */%0A  @Override%0A  public boolean consistentWithEquals() %7B%0A    return false;%0A  %7D%0A%0A  @Override%0A  public Object structuralValue(T value) %7B%0A    if (value != null && consistentWithEquals()) %7B%0A      return value;%0A    %7D else %7B%0A      try %7B%0A        ByteArrayOutputStream os = new ByteArrayOutputStream();%0A        encode(value, os, Context.OUTER);%0A        return new StructuralByteArray(os.toByteArray());%0A      %7D catch (Exception exn) %7B%0A        throw new IllegalArgumentException(%0A            %22Unable to encode element '%22 + value + %22' with coder '%22 + this + %22'.%22, exn);%0A      %7D%0A    %7D%0A  %7D%0A%0A  @SuppressWarnings(%22unchecked%22)%0A  @Override%0A  public TypeDescriptor%3CT%3E getEncodedTypeDescriptor() %7B%0A    return (TypeDescriptor%3CT%3E)%0A        TypeDescriptor.of(getClass()).resolveType(new TypeDescriptor%3CT%3E() %7B%7D.getType());%0A  %7D%0A
 %7D%0A

Remove duplicated methods in StructuredCoder