diff --git a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/ModelAndView.java b/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/ModelAndView.java deleted file mode 100644 index 3bec37bdde..0000000000 --- a/spring-web-reactive/src/main/java/org/springframework/web/reactive/result/view/ModelAndView.java +++ /dev/null @@ -1,202 +0,0 @@ -/* - * Copyright 2002-2016 the original author or authors. - * - * 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.springframework.web.reactive.result.view; - -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Optional; - -import org.springframework.core.Conventions; -import org.springframework.util.Assert; - -/** - * Immutable holder for both model and view in the web MVC framework. Note that these are entirely - * distinct. This class merely holds both to make it possible for a handler to return both model - * and view in a single return value. - * - *

Represents a model and view returned by a handler. The view can take the form of a String - * view name which will need to be resolved by a {@link ViewResolver}; alternatively a - * {@link View} can be specified directly. The model - * is a {@link Map}, allowing the use of multiple objects keyed by name. - * @param the type of view the model contains (either a {@link View} or a {@code String} - * denoting the view name). - * @author Arjen Poutsma - * @see ViewResolver - * @since 5.0 - */ -public class ModelAndView { - - /** - * View instance or view name String - */ - private final T view; - - /** - * Model Map - */ - private final Map model; - - - private ModelAndView(T view, Map model) { - Assert.notNull(view, "'view' must not be null"); - Assert.notNull(model, "'model' must not be null"); - this.view = view; - this.model = Collections.unmodifiableMap(model); - } - - /** - * Return a builder for a {@code ModelAndView} with a view name. - * @return the builder - */ - public static Builder viewName(String viewName) { - Assert.hasLength(viewName, "'viewName' must not be empty"); - return new BuilderImpl<>(viewName); - } - - /** - * Return a builder for a {@code ModelAndView} with a {@link View} instance. - * @return the builder - */ - public static Builder view(View view) { - Assert.notNull(view, "'view' must not be null"); - return new BuilderImpl<>(view); - } - - /** - * Return the optional view name to be resolved by the DispatcherHandler - * via a ViewResolver. - */ - public Optional viewName() { - return this.view instanceof String ? Optional.of((String) this.view) : Optional.empty(); - } - - /** - * Return the optional View object. - */ - public Optional view() { - return this.view instanceof View ? Optional.of((View) this.view) : Optional.empty(); - } - - /** - * Return whether we use a view reference, i.e. {@code true} - * if the view has been specified via a name to be resolved by a ViewResolver. - */ - public boolean isReference() { - return (this.view instanceof String); - } - - /** - * Return the unmodifiable model map. Never returns {@code null}. - */ - public Map model() { - return this.model; - } - - /** - * Return diagnostic information about this model and view. - */ - @Override - public String toString() { - StringBuilder sb = new StringBuilder("ModelAndView: "); - if (isReference()) { - sb.append("reference to view with name '").append(this.view).append("'"); - } - else { - sb.append("materialized View is [").append(this.view).append(']'); - } - sb.append("; model is ").append(this.model); - return sb.toString(); - } - - - /** - * A mutable builder for a {@link ModelAndView}. - * @param the type of view the model contains (either {@link View} or a view name). - */ - public interface Builder { - - /** - * Add the supplied model attribute under the supplied name. - * @param attributeName the name of the model attribute (never {@code null}) - * @param attributeValue the model attribute value (can be {@code null}) - */ - Builder modelAttribute(String attributeName, Object attributeValue); - - /** - * Add a model attribute using parameter name generation. - * @param attributeValue the object to add to the model (never {@code null}) - */ - Builder modelAttribute(Object attributeValue); - - /** - * Copy all attributes in the supplied {@code Map} into the model. - * @see #modelAttribute(String, Object) - */ - Builder modelAttributes(Map attributes); - - /** - * Builds the {@link ModelAndView}. - * @return the built model and view - */ - ModelAndView build(); - - } - - - private static class BuilderImpl implements Builder { - - private final T view; - - private final Map model = new LinkedHashMap<>(); - - public BuilderImpl(T view) { - this.view = view; - } - - @Override - public Builder modelAttribute(String attributeName, Object attributeValue) { - Assert.notNull(attributeName, "Model attribute name must not be null"); - this.model.put(attributeName, attributeValue); - return this; - } - - @Override - public Builder modelAttribute(Object attributeValue) { - Assert.notNull(attributeValue, "Model object must not be null"); - if (attributeValue instanceof Collection && ((Collection) attributeValue).isEmpty()) { - return this; - } - return modelAttribute(Conventions.getVariableName(attributeValue), attributeValue); - } - - @Override - public Builder modelAttributes(Map attributes) { - if (attributes != null) { - this.model.putAll(attributes); - } - return this; - } - - @Override - public ModelAndView build() { - return new ModelAndView(this.view, this.model); - } - } - -} diff --git a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ModelAndViewTests.java b/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ModelAndViewTests.java deleted file mode 100644 index c44f3a240e..0000000000 --- a/spring-web-reactive/src/test/java/org/springframework/web/reactive/result/view/ModelAndViewTests.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2002-2016 the original author or authors. - * - * 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.springframework.web.reactive.result.view; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; - -/** - * @author Arjen Poutsma - */ -public class ModelAndViewTests { - - @Test - public void view() { - View view = mock(View.class); - ModelAndView mav = ModelAndView.view(view).build(); - assertFalse(mav.isReference()); - assertTrue(mav.view().isPresent()); - assertFalse(mav.viewName().isPresent()); - assertEquals(view, mav.view().get()); - } - - @Test - public void viewName() { - String viewName = "foo"; - ModelAndView mav = ModelAndView.viewName(viewName).build(); - assertTrue(mav.isReference()); - assertTrue(mav.viewName().isPresent()); - assertFalse(mav.view().isPresent()); - assertEquals(viewName, mav.viewName().get()); - } - - @Test - public void modelAttribute() { - ModelAndView mav = ModelAndView.viewName("foo") - .modelAttribute("foo", "bar").build(); - assertEquals(1, mav.model().size()); - assertTrue(mav.model().containsKey("foo")); - assertEquals("bar", mav.model().get("foo")); - } - - @Test - public void modelAttributeNoName() { - ModelAndView mav = ModelAndView.viewName("foo") - .modelAttribute(this).build(); - assertEquals(1, mav.model().size()); - assertTrue(mav.model().containsKey("modelAndViewTests")); - assertEquals(this, mav.model().get("modelAndViewTests")); - } - - @Test - public void modelAttributes() { - Map model = new HashMap<>(); - model.put("foo", "bar"); - model.put("baz", "qux"); - ModelAndView mav = ModelAndView.viewName("foo") - .modelAttributes(model).build(); - assertEquals(2, mav.model().size()); - assertTrue(mav.model().containsKey("foo")); - assertEquals("bar", mav.model().get("foo")); - assertTrue(mav.model().containsKey("baz")); - assertEquals("qux", mav.model().get("baz")); - } -} \ No newline at end of file