/*
 * Copyright (c) 2012-2013 Spotify AB
 *
 * 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 com.spotify.netty4.handler.codec.zmtp;

import java.nio.charset.Charset;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.DefaultByteBufHolder;
import io.netty.buffer.Unpooled;

import static io.netty.util.CharsetUtil.UTF_8;


public class ZMTPFrame extends DefaultByteBufHolder {

  public static final ZMTPFrame EMPTY_FRAME = new ZMTPFrame(Unpooled.EMPTY_BUFFER);

  ZMTPFrame(final ByteBuf content) {
    super(content);
  }

  /**
   * @return Is the current frame empty
   */
  public boolean hasContent() {
    return content().isReadable();
  }

  /**
   * Returns the length of the data
   */
  public int size() {
    return content().readableBytes();
  }

  /**
   * Create a frame from a string, UTF-8 encoded.
   */
  static public ZMTPFrame from(final String data) {
    return from(data, UTF_8);
  }

  /**
   * Create a new frame from a string
   *
   * @param data    String
   * @param charset Used to get the bytes
   * @return a ZMTP frame containing the byte encoded string
   */
  public static ZMTPFrame from(final String data, final Charset charset) {
    if (data.length() == 0) {
      return EMPTY_FRAME;
    } else {
      return from(Unpooled.wrappedBuffer(charset.encode(data)));
    }
  }

  /**
   * Create a new frame from a byte array.
   */
  public static ZMTPFrame from(final byte[] data) {
    if (data == null || data.length == 0) {
      return EMPTY_FRAME;
    } else {
      return new ZMTPFrame(Unpooled.wrappedBuffer(data));
    }
  }

  /**
   * Create a new frame from a buffer. Claims ownership of the buffer.
   */
  public static ZMTPFrame from(final ByteBuf buf) {
    if (!buf.isReadable()) {
      return EMPTY_FRAME;
    } else {
      return new ZMTPFrame(buf);
    }
  }

  @Override
  public boolean equals(final Object o) {
    if (this == o) { return true; }
    if (o == null || getClass() != o.getClass()) { return false; }

    final ZMTPFrame zmtpFrame = (ZMTPFrame) o;

    if (content() != null ? !content().equals(zmtpFrame.content()) : zmtpFrame.content() != null) { return false; }

    return true;
  }

  @Override
  public int hashCode() {
    return content() != null ? content().hashCode() : 0;
  }

  @Override
  public String toString() {
    return "ZMTPFrame{\"" +
           ZMTPUtils.toString(content()) +
           "\"}";
  }
}

@@ -734,59 +734,53 @@
 fer.
-DefaultByteBufHolder;%0Aimport io.netty.buffer.Unpool
+Unpooled;%0Aimport io.netty.util.ReferenceCount
 ed;%0A
@@ -856,36 +856,35 @@
 ame 
-extends DefaultByteBufHolder
+implements ReferenceCounted
  %7B%0A%0A
@@ -968,16 +968,50 @@
 FFER);%0A%0A
+  private final ByteBuf content;%0A%0A
   ZMTPFr
@@ -1047,22 +1047,30 @@
     
-super(
+this.content = 
 content
-)
 ;%0A  
@@ -1174,18 +1174,16 @@
  content
-()
 .isReada
@@ -1186,32 +1186,127 @@
 eadable();%0A  %7D%0A%0A
+  /**%0A   * Get the frame contents.%0A   */%0A  public ByteBuf content() %7B%0A    return content;%0A  %7D%0A%0A
   /**%0A   * Retur
@@ -1377,18 +1377,16 @@
  content
-()
 .readabl
@@ -1468,19 +1468,19 @@
 /%0A  
-static publ
+public stat
 ic Z
@@ -2456,24 +2456,468 @@
 %0A    %7D%0A  %7D%0A%0A
+  @Override%0A  public int refCnt() %7B%0A    return content.refCnt();%0A  %7D%0A%0A  @Override%0A  public ReferenceCounted retain() %7B%0A    return content.retain();%0A  %7D%0A%0A  @Override%0A  public ReferenceCounted retain(final int increment) %7B%0A    return content.retain(increment);%0A  %7D%0A%0A  @Override%0A  public boolean release() %7B%0A    return content.release();%0A  %7D%0A%0A  @Override%0A  public boolean release(final int decrement) %7B%0A    return content.release(decrement);%0A  %7D%0A%0A
   @Override%0A
@@ -3121,26 +3121,24 @@
  if (content
-()
  != null ? !
@@ -3144,18 +3144,16 @@
 !content
-()
 .equals(
@@ -3169,18 +3169,16 @@
 .content
-()
 ) : zmtp
@@ -3186,26 +3186,24 @@
 rame.content
-()
  != null) %7B 
@@ -3297,18 +3297,16 @@
  content
-()
  != null
@@ -3315,18 +3315,16 @@
  content
-()
 .hashCod
@@ -3443,18 +3443,16 @@
 (content
-()
 ) +%0A    

implement ReferenceCounted directly