• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 18154768944

01 Oct 2025 07:26AM UTC coverage: 81.882% (-0.005%) from 81.887%
18154768944

push

github

web-flow
Merge pull request #290 from TAKETODAY/dev/jspecify

jspecify

59788 of 78013 branches covered (76.64%)

Branch coverage included in aggregate %.

141239 of 167496 relevant lines covered (84.32%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
today-context/src/main/java/infra/ui/Model.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.ui;
19

20
import org.jspecify.annotations.Nullable;
21

22
import java.util.Collection;
23
import java.util.Map;
24

25
import infra.core.Conventions;
26
import infra.lang.Constant;
27
import infra.lang.Unmodifiable;
28
import infra.util.CollectionUtils;
29

30
/**
31
 * Interface that defines a holder for model attributes.
32
 *
33
 * <p>Primarily designed for adding attributes to the model.
34
 * <p>Allows for accessing the overall model as a {@code java.util.Map}.
35
 *
36
 * @author Juergen Hoeller
37
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
38
 * @since 2018-10-14 20:30
39
 */
40
public interface Model {
41

42
  /**
43
   * Contains a attribute with given name
44
   *
45
   * @param name Attribute name
46
   * @return if contains the attribute
47
   */
48
  default boolean containsAttribute(String name) {
49
    return getAttribute(name) == null;
×
50
  }
51

52
  /**
53
   * Add the attributes from map
54
   *
55
   * @param attributes The attributes
56
   */
57
  default void setAttributes(Map<String, Object> attributes) {
58
    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
×
59
      setAttribute(entry.getKey(), entry.getValue());
×
60
    }
×
61
  }
×
62

63
  /**
64
   * Returns the value of the named attribute as an <code>Object</code>, or
65
   * <code>null</code> if no attribute of the given name exists.
66
   *
67
   * @param name a <code>String</code> specifying the name of the attribute
68
   * @return an <code>Object</code> containing the value of the attribute, or
69
   * <code>null</code> if the attribute does not exist
70
   */
71
  @Nullable
72
  Object getAttribute(String name);
73

74
  /**
75
   * Stores an attribute in this request. Attributes are reset between requests..
76
   *
77
   * @param name a <code>String</code> specifying the name of the attribute
78
   * @param value the <code>Object</code> to be stored
79
   */
80
  void setAttribute(String name, @Nullable Object value);
81

82
  /**
83
   * Add the supplied attribute under the supplied name.
84
   *
85
   * @param attributeName the name of the model attribute (never {@code null})
86
   * @param attributeValue the model attribute value (can be {@code null})
87
   * @since 4.0
88
   */
89
  Model addAttribute(String attributeName, @Nullable Object attributeValue);
90

91
  /**
92
   * Removes an attribute from this request. This method is not generally needed
93
   * as attributes only persist as long as the request is being handled.
94
   *
95
   * @param name a <code>String</code> specifying the name of the attribute to
96
   * remove
97
   * @return the last value of the attribute, if any
98
   */
99
  Object removeAttribute(String name);
100

101
  /**
102
   * Convert this model to a {@link Map}
103
   */
104
  Map<String, Object> asMap();
105

106
  /**
107
   * Clear all attributes
108
   */
109
  void clear();
110

111
  /**
112
   * Returns {@code true} if this map contains no key-value mappings.
113
   *
114
   * @return {@code true} if this map contains no key-value mappings
115
   * @since 4.0
116
   */
117
  boolean isEmpty();
118

119
  /**
120
   * Return the names of all attributes.
121
   *
122
   * @since 4.0
123
   */
124
  default String[] getAttributeNames() {
125
    return CollectionUtils.toArray(attributeNames().iterator(), Constant.EMPTY_STRING_ARRAY);
×
126
  }
127

128
  /**
129
   * Return the names.
130
   *
131
   * @since 4.0
132
   */
133
  @Unmodifiable
134
  Iterable<String> attributeNames();
135

136
  /**
137
   * Add the supplied attribute to this {@code Map} using a
138
   * {@link Conventions#getVariableName generated name}.
139
   * <p><i>Note: Empty {@link java.util.Collection Collections} are not added to
140
   * the model when using this method because we cannot correctly determine
141
   * the true convention name. View code should check for {@code null} rather
142
   * than for empty collections as is already done by JSTL tags.</i>
143
   *
144
   * @param attributeValue the model attribute value
145
   * @since 4.0
146
   */
147
  Model addAttribute(Object attributeValue);
148

149
  /**
150
   * Copy all attributes in the supplied {@code Collection} into this
151
   * {@code Map}, using attribute name generation for each element.
152
   *
153
   * @see #addAttribute(Object)
154
   * @since 4.0
155
   */
156
  Model addAllAttributes(@Nullable Collection<?> attributeValues);
157

158
  /**
159
   * Copy all attributes in the supplied {@code Map} into this {@code Map}.
160
   *
161
   * @see #setAttribute(String, Object)
162
   * @since 4.0
163
   */
164
  Model addAllAttributes(@Nullable Map<String, ?> attributes);
165

166
  /**
167
   * Copy all attributes in the supplied {@code Map} into this {@code Map},
168
   * with existing objects of the same name taking precedence (i.e. not getting
169
   * replaced).
170
   *
171
   * @since 4.0
172
   */
173
  Model mergeAttributes(@Nullable Map<String, ?> attributes);
174

175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc