001/* 002 * Copyright 2002-2019 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.ui; 018 019import java.util.Collection; 020import java.util.Map; 021 022import org.springframework.lang.Nullable; 023 024/** 025 * Java-5-specific interface that defines a holder for model attributes. 026 * Primarily designed for adding attributes to the model. 027 * Allows for accessing the overall model as a {@code java.util.Map}. 028 * 029 * @author Juergen Hoeller 030 * @since 2.5.1 031 */ 032public interface Model { 033 034 /** 035 * Add the supplied attribute under the supplied name. 036 * @param attributeName the name of the model attribute (never {@code null}) 037 * @param attributeValue the model attribute value (can be {@code null}) 038 */ 039 Model addAttribute(String attributeName, @Nullable Object attributeValue); 040 041 /** 042 * Add the supplied attribute to this {@code Map} using a 043 * {@link org.springframework.core.Conventions#getVariableName generated name}. 044 * <p><i>Note: Empty {@link java.util.Collection Collections} are not added to 045 * the model when using this method because we cannot correctly determine 046 * the true convention name. View code should check for {@code null} rather 047 * than for empty collections as is already done by JSTL tags.</i> 048 * @param attributeValue the model attribute value (never {@code null}) 049 */ 050 Model addAttribute(Object attributeValue); 051 052 /** 053 * Copy all attributes in the supplied {@code Collection} into this 054 * {@code Map}, using attribute name generation for each element. 055 * @see #addAttribute(Object) 056 */ 057 Model addAllAttributes(Collection<?> attributeValues); 058 059 /** 060 * Copy all attributes in the supplied {@code Map} into this {@code Map}. 061 * @see #addAttribute(String, Object) 062 */ 063 Model addAllAttributes(Map<String, ?> attributes); 064 065 /** 066 * Copy all attributes in the supplied {@code Map} into this {@code Map}, 067 * with existing objects of the same name taking precedence (i.e. not getting 068 * replaced). 069 */ 070 Model mergeAttributes(Map<String, ?> attributes); 071 072 /** 073 * Does this model contain an attribute of the given name? 074 * @param attributeName the name of the model attribute (never {@code null}) 075 * @return whether this model contains a corresponding attribute 076 */ 077 boolean containsAttribute(String attributeName); 078 079 /** 080 * Return the attribute value for the given name, if any. 081 * @param attributeName the name of the model attribute (never {@code null}) 082 * @return the corresponding attribute value, or {@code null} if none 083 * @since 5.2 084 */ 085 @Nullable 086 Object getAttribute(String attributeName); 087 088 /** 089 * Return the current set of model attributes as a Map. 090 */ 091 Map<String, Object> asMap(); 092 093}