/**
 * Copyright 2012 Twitter, Inc.
 *
 * Licensed 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 parquet.thrift;

import java.lang.reflect.Method;
import java.nio.ByteBuffer;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TField;
import org.apache.thrift.protocol.TList;
import org.apache.thrift.protocol.TMap;
import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TSet;
import org.apache.thrift.protocol.TStruct;

/**
 * Allows simple implementation of partial protocols
 *
 * @author Julien Le Dem
 *
 */
abstract class ParquetProtocol extends TProtocol {

  private final String message;

  ParquetProtocol() {
    super(null);
    this.message = "in " + getClassInfo();
  }

  private String getClassInfo() {
    final Class<? extends ParquetProtocol> clazz = getClass();
    final Method enclosingMethod = clazz.getEnclosingMethod();
    if (enclosingMethod != null) {
      return clazz.getName() + " in " + enclosingMethod.toGenericString();
    }
    return clazz.getName();
  }

  /**
   * @param name a meaningful debugging name for anonymous inner classes
   */
  ParquetProtocol(String name) {
    super(null);
    this.message = "when we expected " + name + " in " + getClassInfo();
  }

  private UnsupportedOperationException exception() {
    return new UnsupportedOperationException(new Exception().getStackTrace()[1].getMethodName() + " was called " + message);
  }

  /** WRITE */

  @Override
  public void writeMessageBegin(TMessage message) throws TException {
    throw new UnsupportedOperationException(this.message);
  }

  @Override
  public void writeMessageEnd() throws TException {
    throw exception();
  }

  @Override
  public void writeStructBegin(TStruct struct) throws TException {
    throw exception();
  }

  @Override
  public void writeStructEnd() throws TException {
    throw exception();
  }

  @Override
  public void writeFieldBegin(TField field) throws TException {
    throw exception();
  }

  @Override
  public void writeFieldEnd() throws TException {
    throw exception();
  }

  @Override
  public void writeFieldStop() throws TException {
    throw exception();
  }

  @Override
  public void writeMapBegin(TMap map) throws TException {
    throw exception();
  }

  @Override
  public void writeMapEnd() throws TException {
    throw exception();
  }

  @Override
  public void writeListBegin(TList list) throws TException {
    throw exception();
  }

  @Override
  public void writeListEnd() throws TException {
    throw exception();
  }

  @Override
  public void writeSetBegin(TSet set) throws TException {
    throw exception();
  }

  @Override
  public void writeSetEnd() throws TException {
    throw exception();
  }

  @Override
  public void writeBool(boolean b) throws TException {
    throw exception();
  }

  @Override
  public void writeByte(byte b) throws TException {
    throw exception();
  }

  @Override
  public void writeI16(short i16) throws TException {
    throw exception();
  }

  @Override
  public void writeI32(int i32) throws TException {
    throw exception();
  }

  @Override
  public void writeI64(long i64) throws TException {
    throw exception();
  }

  @Override
  public void writeDouble(double dub) throws TException {
    throw exception();
  }

  @Override
  public void writeString(String str) throws TException {
    throw exception();
  }

  @Override
  public void writeBinary(ByteBuffer buf) throws TException {
    throw exception();
  }

  /** READ */

  @Override
  public TMessage readMessageBegin() throws TException {
    throw exception();
  }

  @Override
  public void readMessageEnd() throws TException {
    throw exception();
  }

  @Override
  public TStruct readStructBegin() throws TException {
    throw exception();
  }

  @Override
  public void readStructEnd() throws TException {
    throw exception();
  }

  @Override
  public TField readFieldBegin() throws TException {
    throw exception();
  }

  @Override
  public void readFieldEnd() throws TException {
    throw exception();
  }

  @Override
  public TMap readMapBegin() throws TException {
    throw exception();
  }

  @Override
  public void readMapEnd() throws TException {
    throw exception();
  }

  @Override
  public TList readListBegin() throws TException {
    throw exception();
  }

  @Override
  public void readListEnd() throws TException {
    throw exception();
  }

  @Override
  public TSet readSetBegin() throws TException {
    throw exception();
  }

  @Override
  public void readSetEnd() throws TException {
    throw exception();
  }

  @Override
  public boolean readBool() throws TException {
    throw exception();
  }

  @Override
  public byte readByte() throws TException {
    throw exception();
  }

  @Override
  public short readI16() throws TException {
    throw exception();
  }

  @Override
  public int readI32() throws TException {
    throw exception();
  }

  @Override
  public long readI64() throws TException {
    throw exception();
  }

  @Override
  public double readDouble() throws TException {
    throw exception();
  }

  @Override
  public String readString() throws TException {
    throw exception();
  }

  @Override
  public ByteBuffer readBinary() throws TException {
    throw exception();
  }

}

@@ -1165,27 +1165,18 @@
 ate 
-final 
 String 
-messag
+nam
 e;%0A%0A
@@ -1218,51 +1218,8 @@
 l);%0A
-    this.message = %22in %22 + getClassInfo();%0A
   %7D%0A
@@ -1676,130 +1676,210 @@
 his.
-message = %22when we expected %22 + name + %22 in %22 + getClassInfo();%0A  %7D%0A%0A  private UnsupportedOperationException exception() %7B
+name = name;%0A  %7D%0A%0A  private UnsupportedOperationException exception() %7B%0A    String message = name == null ?%0A        %22in %22 + getClassInfo() :%0A        %22when we expected %22 + name + %22 in %22 + getClassInfo();
 %0A   
@@ -2117,54 +2117,18 @@
 row 
-new UnsupportedOperationException(this.message
+exception(
 );%0A 

remove debugging code from hot path