/**
 * Licensed to jclouds, Inc. (jclouds) under one or more
 * contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  jclouds 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.jclouds.io.payloads;

import java.io.Serializable;

import org.jclouds.javax.annotation.Nullable;

import org.jclouds.io.ContentMetadata;
import org.jclouds.io.ContentMetadataBuilder;
import org.jclouds.io.MutableContentMetadata;

import com.google.common.collect.Multimap;

/**
 * @author Adrian Cole
 */
public class BaseMutableContentMetadata extends ContentMetadataBuilder implements MutableContentMetadata, Serializable {
   /** The serialVersionUID */
   private static final long serialVersionUID = 8364286391963469370L;

   @Override
   public void setPropertiesFromHttpHeaders(Multimap<String, String> headers) {
      fromHttpHeaders(headers);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public Long getContentLength() {
      return contentLength;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void setContentLength(@Nullable Long contentLength) {
      contentLength(contentLength);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public byte[] getContentMD5() {
      if (contentMD5 != null) {
         byte[] retval = new byte[contentMD5.length];
         System.arraycopy(this.contentMD5, 0, retval, 0, contentMD5.length);
         return retval;
      } else {
         return null;
      }
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void setContentMD5(byte[] md5) {
      contentMD5(md5);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String getContentType() {
      return contentType;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void setContentType(@Nullable String contentType) {
      contentType(contentType);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void setContentDisposition(@Nullable String contentDisposition) {
      contentDisposition(contentDisposition);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String getContentDisposition() {
      return this.contentDisposition;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void setContentLanguage(@Nullable String contentLanguage) {
      contentLanguage(contentLanguage);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String getContentLanguage() {
      return this.contentLanguage;
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public void setContentEncoding(@Nullable String contentEncoding) {
      contentEncoding(contentEncoding);
   }

   /**
    * {@inheritDoc}
    */
   @Override
   public String getContentEncoding() {
      return this.contentEncoding;
   }

   @Override
   public BaseMutableContentMetadata toBuilder() {
      return BaseMutableContentMetadata.fromContentMetadata(this);
   }

   public static BaseMutableContentMetadata fromContentMetadata(ContentMetadata in) {
      return (BaseMutableContentMetadata) new BaseMutableContentMetadata().contentType(in.getContentType())
               .contentLength(in.getContentLength()).contentMD5(in.getContentMD5()).contentDisposition(
                        in.getContentDisposition()).contentLanguage(in.getContentLanguage()).contentEncoding(
                        in.getContentEncoding());
   }
}