001/* $Id: Person.java 1140212 2011-06-27 15:44:36Z simonetripodi $ 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.commons.digester3.annotations.addressbook; 019 020import java.util.ArrayList; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.commons.digester3.annotations.rules.BeanPropertySetter; 026import org.apache.commons.digester3.annotations.rules.CallMethod; 027import org.apache.commons.digester3.annotations.rules.CallParam; 028import org.apache.commons.digester3.annotations.rules.ObjectCreate; 029import org.apache.commons.digester3.annotations.rules.SetNext; 030import org.apache.commons.digester3.annotations.rules.SetProperty; 031 032/** 033 * @since 2.1 034 */ 035@ObjectCreate( pattern = "address-book/person" ) 036public class Person 037{ 038 039 private final Map<String, String> emails = new HashMap<String, String>(); 040 041 private final List<Address> addresses = new ArrayList<Address>(); 042 043 @SetProperty( pattern = "address-book/person" ) 044 private int id; 045 046 @SetProperty( pattern = "address-book/person" ) 047 private String category; 048 049 @BeanPropertySetter( pattern = "address-book/person/name" ) 050 private String name; 051 052 public int getId() 053 { 054 return id; 055 } 056 057 public void setId( int id ) 058 { 059 this.id = id; 060 } 061 062 public String getCategory() 063 { 064 return category; 065 } 066 067 public void setCategory( String category ) 068 { 069 this.category = category; 070 } 071 072 public String getName() 073 { 074 return name; 075 } 076 077 public void setName( String name ) 078 { 079 this.name = name; 080 } 081 082 public Map<String, String> getEmails() 083 { 084 return emails; 085 } 086 087 public List<Address> getAddresses() 088 { 089 return addresses; 090 } 091 092 @CallMethod( pattern = "address-book/person/email" ) 093 public void addEmail( @CallParam( pattern = "address-book/person/email", attributeName = "type" ) String type, 094 @CallParam( pattern = "address-book/person/email" ) String address ) 095 { 096 this.emails.put( type, address ); 097 } 098 099 @SetNext( fireOnBegin = true ) 100 public void addAddress( Address addr ) 101 { 102 this.addresses.add( addr ); 103 } 104 105 @Override 106 public boolean equals( Object obj ) 107 { 108 if ( this == obj ) 109 return true; 110 if ( obj == null ) 111 return false; 112 if ( getClass() != obj.getClass() ) 113 return false; 114 Person other = (Person) obj; 115 if ( addresses == null ) 116 { 117 if ( other.addresses != null ) 118 return false; 119 } 120 else if ( !addresses.equals( other.addresses ) ) 121 return false; 122 if ( category == null ) 123 { 124 if ( other.category != null ) 125 return false; 126 } 127 else if ( !category.equals( other.category ) ) 128 return false; 129 if ( emails == null ) 130 { 131 if ( other.emails != null ) 132 return false; 133 } 134 else if ( !emails.equals( other.emails ) ) 135 return false; 136 if ( id != other.id ) 137 return false; 138 if ( name == null ) 139 { 140 if ( other.name != null ) 141 return false; 142 } 143 else if ( !name.equals( other.name ) ) 144 return false; 145 return true; 146 } 147 148 @Override 149 public String toString() 150 { 151 return "Person [addresses=" + addresses + ", category=" + category + ", emails=" + emails + ", id=" + id 152 + ", name=" + name + "]"; 153 } 154 155}