View Javadoc

1   /* $Id: Person.java 1140212 2011-06-27 15:44:36Z simonetripodi $
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one or more
4    * contributor license agreements.  See the NOTICE file distributed with
5    * this work for additional information regarding copyright ownership.
6    * The ASF licenses this file to You under the Apache License, Version 2.0
7    * (the "License"); you may not use this file except in compliance with
8    * the License.  You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.digester3.annotations.addressbook;
19  
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.apache.commons.digester3.annotations.rules.BeanPropertySetter;
26  import org.apache.commons.digester3.annotations.rules.CallMethod;
27  import org.apache.commons.digester3.annotations.rules.CallParam;
28  import org.apache.commons.digester3.annotations.rules.ObjectCreate;
29  import org.apache.commons.digester3.annotations.rules.SetNext;
30  import org.apache.commons.digester3.annotations.rules.SetProperty;
31  
32  /**
33   * @since 2.1
34   */
35  @ObjectCreate( pattern = "address-book/person" )
36  public class Person
37  {
38  
39      private final Map<String, String> emails = new HashMap<String, String>();
40  
41      private final List<Address> addresses = new ArrayList<Address>();
42  
43      @SetProperty( pattern = "address-book/person" )
44      private int id;
45  
46      @SetProperty( pattern = "address-book/person" )
47      private String category;
48  
49      @BeanPropertySetter( pattern = "address-book/person/name" )
50      private String name;
51  
52      public int getId()
53      {
54          return id;
55      }
56  
57      public void setId( int id )
58      {
59          this.id = id;
60      }
61  
62      public String getCategory()
63      {
64          return category;
65      }
66  
67      public void setCategory( String category )
68      {
69          this.category = category;
70      }
71  
72      public String getName()
73      {
74          return name;
75      }
76  
77      public void setName( String name )
78      {
79          this.name = name;
80      }
81  
82      public Map<String, String> getEmails()
83      {
84          return emails;
85      }
86  
87      public List<Address> getAddresses()
88      {
89          return addresses;
90      }
91  
92      @CallMethod( pattern = "address-book/person/email" )
93      public void addEmail( @CallParam( pattern = "address-book/person/email", attributeName = "type" ) String type,
94                            @CallParam( pattern = "address-book/person/email" ) String address )
95      {
96          this.emails.put( type, address );
97      }
98  
99      @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 }