KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > archie > impl > Offer


1 package org.sapia.archie.impl;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5 import java.util.Properties JavaDoc;
6
7
8 /**
9  * @author Yanick Duchesne
10  * <dl>
11  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
12  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
13  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
14  * </dl>
15  */

16 public class Offer implements java.io.Serializable JavaDoc{
17   
18   private Properties JavaDoc _attributes;
19   private Object JavaDoc _obj;
20   private String JavaDoc _uniqueId;
21   private long _lastSelectTime = System.currentTimeMillis();
22   private long _selectCount;
23   private static long _counter = 0;
24   
25   public Offer(Properties JavaDoc attributes, Object JavaDoc obj){
26     _attributes = attributes;
27     _obj = obj;
28     _uniqueId = uniqueId();
29   }
30   
31   public Properties JavaDoc getAttributes(){
32     return _attributes;
33   }
34   
35   public String JavaDoc getId(){
36     return _uniqueId;
37   }
38   
39   public long getLastSelectTime(){
40     return _lastSelectTime;
41   }
42   
43   public long getSelectCount(){
44     return _selectCount;
45   }
46   
47   Offer select(){
48     _lastSelectTime = System.currentTimeMillis();
49     ++_selectCount;
50     return this;
51   }
52   
53   public boolean matches(Properties JavaDoc props){
54     Iterator JavaDoc entries = props.entrySet().iterator();
55     String JavaDoc value;
56     while(entries.hasNext()){
57         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)entries.next();
58         value = _attributes.getProperty(entry.getKey().toString());
59         if(value == null && entry.getValue() == null){
60           continue;
61         }
62         else if(value == null && entry.getValue() != null){
63           return false;
64         }
65         else if(value != null && entry.getValue() == null){
66           return false;
67         }
68         else if(value != null && entry.getValue() != null &&
69                 entry.getValue().toString().equals(value)){
70           continue;
71         }
72         else{
73           return false;
74         }
75     }
76     return true;
77   }
78   
79   public Object JavaDoc getObject(){
80     return _obj;
81   }
82   
83   public String JavaDoc toString() {
84     StringBuffer JavaDoc aBuffer = new StringBuffer JavaDoc(super.toString());
85     aBuffer.append("[id=").append(_uniqueId).
86             append(" object=").append(_obj).
87             append(" selectCount=").append(_selectCount).
88             append(" lastSelectTime=").append(_lastSelectTime).
89             append(" attributes=").append(_attributes).
90               append("]");
91     
92     return aBuffer.toString();
93   }
94   
95   static synchronized String JavaDoc uniqueId(){
96     return "" + (_counter++);
97   }
98 }
99
Popular Tags