001/**
002 * Copyright 2011 Bill Brown
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 * http://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 */
016package com.colorfulsoftware.rss;
017
018import java.io.Serializable;
019
020/**
021 * <p>
022 * This class contains the attribute definition for elements.
023 * </p>
024 * 
025 * @author Bill Brown
026 * 
027 */
028public class Attribute implements Serializable {
029
030        private static final long serialVersionUID = -3880416791234118400L;
031        private final String name;
032        private final String value;
033
034        // use the factory method in the RSSDoc.
035        Attribute(String name, String value) throws RSSpectException {
036                if (name == null || name.equals("")) {
037                        throw new RSSpectException("Attribute names SHOULD NOT be blank.");
038                }
039                this.name = name;
040                this.value = (value == null) ? "" : value;
041        }
042
043        // copy constructor
044        Attribute(Attribute attribute) {
045                this.name = attribute.name;
046                this.value = attribute.value;
047        }
048
049        /**
050         * 
051         * @return the name of this attribute
052         */
053        public String getName() {
054                return name;
055        }
056
057        /**
058         * 
059         * @return the value of this attribute
060         */
061        public String getValue() {
062                return value;
063        }
064
065        /**
066         * @return true if the attribute name and value are equal.
067         */
068        @Override
069        public boolean equals(Object obj) {
070                if (obj == this) {
071                        return true;
072                }
073                if (!(obj instanceof Attribute)) {
074                        return false;
075                }
076                return this.toString().equals(obj.toString());
077        }
078        
079        @Override public int hashCode() {
080                return toString().hashCode();
081        }
082
083        /**
084         * Shows the contents of the element's attribute in the form of '
085         * attrName="attrValue"'.
086         */
087        @Override
088        public String toString() {
089                return " " + name + "=\"" + value + "\"";
090        }
091}