001/*
002 * Copyright 2002-2017 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.beans;
018
019import org.springframework.lang.Nullable;
020import org.springframework.util.Assert;
021import org.springframework.util.ObjectUtils;
022
023/**
024 * Holder for a key-value style attribute that is part of a bean definition.
025 * Keeps track of the definition source in addition to the key-value pair.
026 *
027 * @author Juergen Hoeller
028 * @since 2.5
029 */
030public class BeanMetadataAttribute implements BeanMetadataElement {
031
032        private final String name;
033
034        @Nullable
035        private final Object value;
036
037        @Nullable
038        private Object source;
039
040
041        /**
042         * Create a new AttributeValue instance.
043         * @param name the name of the attribute (never {@code null})
044         * @param value the value of the attribute (possibly before type conversion)
045         */
046        public BeanMetadataAttribute(String name, @Nullable Object value) {
047                Assert.notNull(name, "Name must not be null");
048                this.name = name;
049                this.value = value;
050        }
051
052
053        /**
054         * Return the name of the attribute.
055         */
056        public String getName() {
057                return this.name;
058        }
059
060        /**
061         * Return the value of the attribute.
062         */
063        @Nullable
064        public Object getValue() {
065                return this.value;
066        }
067
068        /**
069         * Set the configuration source {@code Object} for this metadata element.
070         * <p>The exact type of the object will depend on the configuration mechanism used.
071         */
072        public void setSource(@Nullable Object source) {
073                this.source = source;
074        }
075
076        @Override
077        @Nullable
078        public Object getSource() {
079                return this.source;
080        }
081
082
083        @Override
084        public boolean equals(@Nullable Object other) {
085                if (this == other) {
086                        return true;
087                }
088                if (!(other instanceof BeanMetadataAttribute)) {
089                        return false;
090                }
091                BeanMetadataAttribute otherMa = (BeanMetadataAttribute) other;
092                return (this.name.equals(otherMa.name) &&
093                                ObjectUtils.nullSafeEquals(this.value, otherMa.value) &&
094                                ObjectUtils.nullSafeEquals(this.source, otherMa.source));
095        }
096
097        @Override
098        public int hashCode() {
099                return this.name.hashCode() * 29 + ObjectUtils.nullSafeHashCode(this.value);
100        }
101
102        @Override
103        public String toString() {
104                return "metadata attribute '" + this.name + "'";
105        }
106
107}