2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdcrests.mapping;
24 * Base class for all mapping classes. Mapping classes will perform data mapping from source object
25 * to target object Base class provides following<br> <ol> <li>provides life cycle of
26 * mapping class , first mapSimpleProperties is called and then mapComplexProperties is
27 * called.</li> <li>methods mapSimpleProperties and mapComplexProperties with default
28 * implementation, these should be overridden by concrete mapping classes for writing mapping
34 public abstract class MappingBase<S, T> {
37 * Method is called for starting mapping from source object to target object method sets context
38 * in the thread locale and than calls mapSimpleProperties and mapComplexProperties
41 * @param source : source object for mapping
42 * @param clazz : target <code>Class</code> for mapping
43 * @return <code>T</code> - instance of type <code>T</code>
46 public final T applyMapping(final S source, Class<T> clazz) {
47 T target = (T) instantiateTarget(clazz);
48 if (source == null || target == null) {
51 preMapping(source, target);
52 doMapping(source, target);
53 postMapping(source, target);
61 * This method is called before the <code>doMapping</code> method.
64 public void preMapping(final S source, T target) {
68 * The actual method that does the mapping between the <code>source</code> to <code>target</code>
69 * objects. This method is being called automatically as part of the mapper class. This
70 * method must be override (it is abstract) by the mapper class.
72 * @param source - the source object.
73 * @param target - the target object.
76 public abstract void doMapping(final S source, T target);
79 * This method is called after the <code>doMapping</code> method.
82 public void postMapping(final S source, T target) {
86 * Creates the instance of the input class.
88 * @return <code>Object</code>
91 private Object instantiateTarget(final Class<?> clazz) {
94 object = clazz.newInstance();
96 } catch (InstantiationException | IllegalAccessException exception) {