001/*
002 * Copyright 2002-2020 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.validation;
018
019import java.io.Serializable;
020import java.util.Map;
021
022import org.springframework.lang.NonNull;
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025
026/**
027 * Map-based implementation of the BindingResult interface,
028 * supporting registration and evaluation of binding errors on
029 * Map attributes.
030 *
031 * <p>Can be used as errors holder for custom binding onto a
032 * Map, for example when invoking a Validator for a Map object.
033 *
034 * @author Juergen Hoeller
035 * @since 2.0
036 * @see java.util.Map
037 */
038@SuppressWarnings("serial")
039public class MapBindingResult extends AbstractBindingResult implements Serializable {
040
041        private final Map<?, ?> target;
042
043
044        /**
045         * Create a new MapBindingResult instance.
046         * @param target the target Map to bind onto
047         * @param objectName the name of the target object
048         */
049        public MapBindingResult(Map<?, ?> target, String objectName) {
050                super(objectName);
051                Assert.notNull(target, "Target Map must not be null");
052                this.target = target;
053        }
054
055
056        /**
057         * Return the target Map to bind onto.
058         */
059        public final Map<?, ?> getTargetMap() {
060                return this.target;
061        }
062
063        @Override
064        @NonNull
065        public final Object getTarget() {
066                return this.target;
067        }
068
069        @Override
070        @Nullable
071        protected Object getActualFieldValue(String field) {
072                return this.target.get(field);
073        }
074
075}