001/* 002 * Copyright 2011 Chris Pheby 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.jadira.bindings.core.general.marshaller; 017 018import org.jadira.bindings.core.api.Converter; 019import org.jadira.bindings.core.api.ToMarshaller; 020 021/** 022 * Base class providing capability to perform marshalling of source object type 023 * to target. This class uses reflection. 024 * <p> 025 * The marshal method must either 026 * </p> 027 * <p> 028 * a) be instance scoped and defined as part of class S. It must accept no 029 * parameters and return a type of T. For example: 030 * </p> 031 * <p> 032 * {@code public String marshal()} 033 * </p> 034 * <p> 035 * b) be statically scoped. It must accept a single parameter of type S and 036 * return a type of T. For example: 037 * </p> 038 * <p> 039 * {@code public static String marshal(BoundType param)} 040 * </p> 041 * @param <S> Source type for the conversion 042 * @param <T> Source type 043 */ 044public class ConverterToMarshaller<S, T> implements ToMarshaller<S, T> { 045 046 private final Converter<S,T> converter; 047 048 /** 049 * Create a new instance for the given converter 050 * @param converter The converter to be wrapped 051 */ 052 public ConverterToMarshaller(Converter<S,T> converter) { 053 054 if (converter == null) { 055 throw new IllegalStateException("converter must not be null"); 056 } 057 this.converter = converter; 058 } 059 060 /** 061 * {@inheritDoc} 062 */ 063 /* @Override */ 064 public T marshal(S object) { 065 066 return converter.convert(object); 067 } 068 069 /** 070 * {@inheritDoc} 071 */ 072 /* @Override */ 073 public Class<S> getBoundClass() { 074 return converter.getInputClass(); 075 } 076 077 /** 078 * {@inheritDoc} 079 */ 080 /* @Override */ 081 public Class<T> getTargetClass() { 082 return converter.getOutputClass(); 083 } 084}