View Javadoc

1   /*
2    * BeanPropertyKeyProvider.java
3    *
4    * Created on February 4, 2007, 12:53 PM
5    */
6   
7   package net.sf.tacos.model.impl;
8   
9   import java.io.Serializable;
10  import net.sf.tacos.model.IKeyProvider;
11  
12  import org.apache.commons.beanutils.BeanUtils;
13  
14  /**
15   * Key provider that uses a property of the object as a key.
16   * 
17   * @author andyhot
18   */
19  public class BeanPropertyKeyProvider implements IKeyProvider {
20      
21      private String property;
22      
23      public BeanPropertyKeyProvider(String property) {
24          this.property = property;
25      }
26      
27      public Serializable getKey(Object obj) {
28          try {
29              return BeanUtils.getProperty(obj, property);
30          } catch (Exception e) {
31              throw new RuntimeException("Error getting property", e);
32          }        
33      }
34  
35  }