/*
 * Copyright 2017, TeamDev Ltd. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.spine3.server.event.enrich;

import com.google.common.base.Function;

import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * This class performs enrichment conversion using the passed function.
 *
 * @param <S> the type of the field in the source event message
 * @param <T> the type of the field in the target enrichment message
 * @author Alexander Yevsyukov
 */
class FieldEnricher<S, T> extends EnrichmentFunction<S, T> {

    /** A function, which performs the enrichment. */
    private final Function<S, T> function;

    private FieldEnricher(Class<S> eventClass, Class<T> enrichmentClass, Function<S, T> function) {
        super(eventClass, enrichmentClass);
        this.function = checkNotNull(function);
    }

    /**
     * Creates a new instance.
     *
     * @param eventFieldClass      a class of the field in the event message
     * @param enrichmentFieldClass a class of the field in the enrichment message
     * @param translator           a conversion function
     * @return a new instance
     */
    static <S, T> FieldEnricher<S, T> newInstance(Class<S> eventFieldClass,
                                                                Class<T> enrichmentFieldClass,
                                                                Function<S, T> translator) {
        final FieldEnricher<S, T> result = new FieldEnricher<>(eventFieldClass, enrichmentFieldClass, translator);
        return result;
    }

    @Override
    void activate() {
        // Do nothing. Field enrichment relies only on the aggregated function.
    }

    @Override
    boolean isActive() {
        // It is always active, as no special actions are required to activate.
        return true;
    }

    @Override
    public Function<S, T> getFunction() {
        return function;
    }

    @Override
    @Nullable
    public T apply(@Nullable S message) {
        ensureActive();
        if (message == null) {
            return null;
        }
        final T result = function.apply(message);
        return result;
    }
}

@@ -2516,50 +2516,18 @@
     
-@Override%0A    void activate() %7B
+/**
 %0A     
-   //
+*
  Do 
@@ -2592,26 +2592,27 @@
 nction.%0A    
-%7D%0A
+ */
 %0A    @Overri
@@ -2622,45 +2622,85 @@
     
-boolean isA
+void a
 ctiv
+at
 e() %7B
+%7D%0A
 %0A    
-    // It is
+/**%0A     * The instances of %7B@code FieldEnricher%7D are
  alw
@@ -2710,16 +2710,23 @@
  active,
+%0A     *
  as no s
@@ -2757,18 +2757,23 @@
 red 
-to
+for the
  activat
 e.%0A 
@@ -2768,18 +2768,67 @@
  activat
-e.
+ion.%0A     */%0A    @Override%0A    boolean isActive() %7B
 %0A       

Convert some of the comments to JavaDocs.