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