View Javadoc
1   package org.apache.fulcrum.security.entity.impl;
2   
3   
4   /*
5    * Licensed to the Apache Software Foundation (ASF) under one
6    * or more contributor license agreements.  See the NOTICE file
7    * distributed with this work for additional information
8    * regarding copyright ownership.  The ASF licenses this file
9    * to you under the Apache License, Version 2.0 (the
10   * "License"); you may not use this file except in compliance
11   * with the License.  You may obtain a copy of the License at
12   *
13   *   http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing,
16   * software distributed under the License is distributed on an
17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18   * KIND, either express or implied.  See the License for the
19   * specific language governing permissions and limitations
20   * under the License.
21   */
22  
23  import org.apache.commons.lang3.builder.HashCodeBuilder;
24  import org.apache.fulcrum.security.entity.SecurityEntity;
25  
26  /**
27   * Base class for all objects implementing SecurityEnitity. This class
28   * automatically lowercases the name. So the permission "EDIT" will equal "eDit"
29   * and "edit";
30   * 
31   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
32   * @version $Id$
33   */
34  public class SecurityEntityImpl implements SecurityEntity
35  {
36      /**
37  	 * Serial id
38  	 */
39  	private static final long serialVersionUID = 6949229336753158100L;
40  
41  	private String name;
42  
43      private Object id;
44  
45      /**
46       * @return object id
47       */
48      public Object getId()
49      {
50          return id;
51      }
52  
53      /**
54       * @param id the object id
55       */
56      public void setId(Object id)
57      {
58          this.id = id;
59      }
60  
61      /**
62       * @return object name
63       */
64      public String getName()
65      {
66          return name;
67      }
68  
69      /**
70       * Pass in the name for this entity. Also lowercases it.
71       * 
72       * @param name name of entity
73       * @throws IllegalArgumentException must provide a name
74       */
75      public void setName(String name) throws IllegalArgumentException
76      {
77      	if ( name == null )
78      	{
79              throw new IllegalArgumentException("Must provide a valid name for all SecurityEntities.");
80      	} else {
81          	this.name = name.toLowerCase();
82      	}
83      }
84  
85      @Override
86      public String toString()
87      {
88          return getClass().getName() + " (id:" + getId() + " name:" + getName() + ")";
89      }
90  
91      /**
92       * Check if this object is equal to another
93       * 
94       * @see java.lang.Object#equals(java.lang.Object)
95       */
96      @Override
97      public boolean equals(Object o)
98      {
99          boolean equals = true;
100         Object id = getId();
101 
102         if (o == null || id == null)
103         {
104             equals = false;
105         }
106         else if (!(o instanceof SecurityEntity))
107         {
108             equals = false;
109         }
110         else
111         {
112             equals = id.equals(((SecurityEntity) o).getId());
113         }
114         return equals;
115     }
116 
117     /**
118      * Calculate a hash code for this object
119      * 
120      * @see java.lang.Object#hashCode()
121      */
122     @Override
123     public int hashCode()
124     {
125         return new HashCodeBuilder(47, 11).append(getId()).append(getName()).toHashCode();
126     }
127 }