/*
 * Copyright (c) 2010-2017. Axon Framework
 *
 * 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.axonframework.queryhandling;

import org.axonframework.common.CollectionUtils;
import org.axonframework.messaging.GenericMessage;
import org.axonframework.messaging.Message;
import org.axonframework.messaging.MessageDecorator;
import org.axonframework.messaging.MetaData;

import java.util.Map;

/**
 * QueryResponseMessage implementation that takes all properties as constructor parameters.
 *
 * @param <T> The type of return value contained in this response
 * @author Allard Buijze
 * @since 3.2
 */
public class GenericQueryResponseMessage<T> extends MessageDecorator<T> implements QueryResponseMessage<T> {

    private static final long serialVersionUID = -735698768536456937L;

    /**
     * Initialize the response message with given {@code result}.
     *
     * @param result The result reported by the Query Handler
     */
    public GenericQueryResponseMessage(T result) {
        this(result, MetaData.emptyInstance());
    }

    /**
     * Initialize the response message with given {@code results}.
     *
     * @param results  The results reported by the Query Handler
     * @param metaData The meta data to contain in the message
     */
    public GenericQueryResponseMessage(T results, Map<String, ?> metaData) {
        super(new GenericMessage<>(results, metaData));
    }

    /**
     * Copy-constructor that takes the payload, meta data and message identifier of the given {@code delegate} for this
     * message.
     * <p>
     * Unlike the other constructors, this constructor will not attempt to retrieve any correlation data from the Unit
     * of Work.
     *
     * @param delegate The message to retrieve message details from
     */
    public GenericQueryResponseMessage(Message<T> delegate) {
        super(delegate);
    }

    /**
     * Creates a QueryResponseMessage for the given {@code result}. If result already implements QueryResponseMessage,
     * it is returned directly. Otherwise a new QueryResponseMessage is created with the result as payload. An attempt
     * is made to detect collection-like structures (see {@link CollectionUtils#asCollection(Object)}) in the given
     * {@code result}, converting it to a Collection is possible.
     *
     * @param result The result of a Query, to be wrapped in a QueryResponseMessage
     * @param <R>    The type of response expected
     * @return a QueryResponseMessage for the given {@code result}, or the result itself, if already a QueryResponseMessage.
     */
    @SuppressWarnings("unchecked")
    public static <R> QueryResponseMessage<R> asResponseMessage(Object result) {
        if (result instanceof QueryResponseMessage) {
            return (QueryResponseMessage<R>) result;
        } else {
            return new GenericQueryResponseMessage(result);
        }
    }

    @Override
    public QueryResponseMessage<T> withMetaData(Map<String, ?> metaData) {
        return new GenericQueryResponseMessage<>(getDelegate().withMetaData(metaData));
    }

    @Override
    public QueryResponseMessage<T> andMetaData(Map<String, ?> additionalMetaData) {
        return new GenericQueryResponseMessage<>(getDelegate().andMetaData(additionalMetaData));
    }
}

@@ -1020,17 +1020,17 @@
 @param %3C
-T
+R
 %3E The ty
@@ -1159,17 +1159,17 @@
 Message%3C
-T
+R
 %3E extend
@@ -1187,17 +1187,17 @@
 corator%3C
-T
+R
 %3E implem
@@ -1222,17 +1222,17 @@
 Message%3C
-T
+R
 %3E %7B%0A%0A   
@@ -1486,25 +1486,25 @@
 onseMessage(
-T
+R
  result) %7B%0A 
@@ -1628,17 +1628,16 @@
 e result
-s
 %7D.%0A     
@@ -1658,17 +1658,17 @@
 m result
-s
+ 
   The re
@@ -1671,17 +1671,16 @@
 e result
-s
  reporte
@@ -1816,17 +1816,16 @@
 age(
-T
+R
  result
-s
 , Ma
@@ -1890,17 +1890,16 @@
 %3E(result
-s
 , metaDa
@@ -2331,17 +2331,17 @@
 Message%3C
-T
+R
 %3E delega
@@ -3050,16 +3050,23 @@
 lready a
+%0A     *
  QueryRe
@@ -3451,17 +3451,17 @@
 Message%3C
-T
+R
 %3E withMe
@@ -3635,17 +3635,17 @@
 Message%3C
-T
+R
 %3E andMet

Replace all references of T to R of 'response'