001/*
002 * Copyright 2002-2015 the original author or authors.
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 *      https://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 */
016
017package org.springframework.context;
018
019import org.springframework.core.ResolvableType;
020import org.springframework.core.ResolvableTypeProvider;
021import org.springframework.util.Assert;
022
023/**
024 * An {@link ApplicationEvent} that carries an arbitrary payload.
025 *
026 * <p>Mainly intended for internal use within the framework.
027 *
028 * @author Stephane Nicoll
029 * @since 4.2
030 * @param <T> the payload type of the event
031 */
032@SuppressWarnings("serial")
033public class PayloadApplicationEvent<T> extends ApplicationEvent implements ResolvableTypeProvider {
034
035        private final T payload;
036
037
038        /**
039         * Create a new PayloadApplicationEvent.
040         * @param source the object on which the event initially occurred (never {@code null})
041         * @param payload the payload object (never {@code null})
042         */
043        public PayloadApplicationEvent(Object source, T payload) {
044                super(source);
045                Assert.notNull(payload, "Payload must not be null");
046                this.payload = payload;
047        }
048
049
050        @Override
051        public ResolvableType getResolvableType() {
052                return ResolvableType.forClassWithGenerics(getClass(), ResolvableType.forInstance(getPayload()));
053        }
054
055        /**
056         * Return the payload of the event.
057         */
058        public T getPayload() {
059                return this.payload;
060        }
061
062}