/* 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 org.camunda.spin;

import java.io.Writer;

import org.camunda.spin.json.SpinJsonNode;
import org.camunda.spin.spi.DataFormat;
import org.camunda.spin.xml.SpinXmlElement;

/**
 *
 * @author Sebastian Menski
 * @author Daniel Meyer
 */
public abstract class Spin<T extends Spin<?>> {

  /**
   *
   * @param input
   * @param format
   * @return
   *
   * @throws IllegalArgumentException in case an argument of illegal type is provided (such as 'null')
   */
  public static <T extends Spin<?>> T S(Object input, DataFormat<T> format) {
    return SpinFactory.INSTANCE.createSpin(input, format);
  }

  /**
  *
  * @param input
  * @param format
  * @return
  *
  * @throws IllegalArgumentException in case an argument of illegal type is provided (such as 'null')
  */
 public static <T extends Spin<?>> T S(Object input, String dataFormatName) {
   return SpinFactory.INSTANCE.createSpin(input, dataFormatName);
 }

  /**
   *
   * @param input
   * @return
   *
   * @throws IllegalArgumentException in case an argument of illegal type is provided (such as 'null')
   */
  public static <T extends Spin<?>> T S(Object input) {
    return SpinFactory.INSTANCE.createSpin(input);
  }

  /**
   *
   * @param input
   * @return
   *
   * @throws IllegalArgumentException in case an argument of illegal type is provided (such as 'null')
   */
  public static SpinXmlElement XML(Object input) {
    return SpinFactory.INSTANCE.createSpin(input, DataFormats.xml());
  }

  /**
  *
  * @param input
  * @return
  *
  * @throws IllegalArgumentException in case an argument of illegal type is provided (such as 'null')
  */
  public static SpinJsonNode JSON(Object input) {
    return SpinFactory.INSTANCE.createSpin(input, DataFormats.json());
  }

  /**
   * Provides the name of the dataformat used by this spin.
   *
   * @return the name of the dataformat used by this Spin.
   */
  public abstract String getDataFormatName();

  /**
   * Return the wrapped object. The return type of this method
   * depends on the concrete data format.
   *
   * @return the object wrapped by this wrapper.
   */
  public abstract Object unwrap();

  /**
   * Returns the wrapped object as string representation.
   *
   * @return the string representation
   */
  public abstract String toString();

  /**
   * Writes the wrapped object to a existing writer.
   *
   * @param writer the writer to write to
   * @return the Writer after the object was written
   */
  public abstract void writeToWriter(Writer writer);

  /**
   * Maps the wrapped object to an instance of a java class.
   *
   * @param type
   * @return
   */
  public abstract <C> C mapTo(Class<C> type);

  /**
   * Maps the wrapped object to a java object.
   * The object is determined based on the configuration string
   * which is data format specific.
   *
   * @param type
   * @return
   */
  public abstract <C> C mapTo(String type);
}

@@ -858,58 +858,205 @@
    *
-%0A   * @param input%0A   * @param format%0A   * @return
+ Creates a spin wrapper for a data input of a given data format.%0A   *%0A   * @param input the input to wrap%0A   * @param format the data format of the input%0A   * @return the spin wrapper for the input
 %0A   
@@ -1316,37 +1316,127 @@
   /**%0A  
-*%0A  * @param input
+ * Creates a spin wrapper for a data input of a given data format.%0A   *%0A   * @param input the input to wrap
 %0A  
+ 
 * @param
@@ -1440,31 +1440,107 @@
 ram 
-format%0A  * @return
+dataFormatName the data format name of the input%0A   * @return the spin wrapper for the input
 %0A  
+ 
 *%0A
+ 
   * 
@@ -1633,24 +1633,25 @@
  as 'null')%0A
+ 
   */%0A public
@@ -1805,39 +1805,186 @@
    *
-%0A   * @param input%0A   * @return
+ Creates a spin wrapper for a data input. The data format of the%0A   * input is auto detected.%0A   *%0A   * @param input the input to wrap%0A   * @return the spin wrapper for the input
 %0A   
@@ -2220,39 +2220,190 @@
    *
-%0A   * @param input%0A   * @return
+ Creates a spin wrapper for a data input. The data format of the%0A   * input is assumed to be XML.%0A   *%0A   * @param input the input to wrap%0A   * @return the spin wrapper for the input
 %0A   
@@ -2651,43 +2651,200 @@
 *%0A  
-*%0A  * @param input%0A  * @return
+ * Creates a spin wrapper for a data input. The data format of the%0A   * input is assumed to be JSON.%0A   *%0A   * @param input the input to wrap%0A   * @return the spin wrapper for the input
 %0A  
+ 
 *%0A
+ 
   * 
@@ -2941,16 +2941,17 @@
 'null')%0A
+ 
   */%0A  p
@@ -3725,61 +3725,8 @@
  to%0A
-   * @return the Writer after the object was written%0A
    *
@@ -3865,37 +3865,80 @@
  @param type
-%0A   * @return
+ the java class to map to%0A   * @return the mapped object
 %0A   */%0A  pub
@@ -4153,29 +4153,72 @@
 ram type
-%0A   * @return
+ the class name to map to%0A   * @return the mapped object
 %0A   */%0A 

add javadoc for spin class