1 package org.sapia.archie.impl; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 import java.util.Properties ; 6 7 8 16 public class Offer implements java.io.Serializable { 17 18 private Properties _attributes; 19 private Object _obj; 20 private String _uniqueId; 21 private long _lastSelectTime = System.currentTimeMillis(); 22 private long _selectCount; 23 private static long _counter = 0; 24 25 public Offer(Properties attributes, Object obj){ 26 _attributes = attributes; 27 _obj = obj; 28 _uniqueId = uniqueId(); 29 } 30 31 public Properties getAttributes(){ 32 return _attributes; 33 } 34 35 public String 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 props){ 54 Iterator entries = props.entrySet().iterator(); 55 String value; 56 while(entries.hasNext()){ 57 Map.Entry entry = (Map.Entry )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 getObject(){ 80 return _obj; 81 } 82 83 public String toString() { 84 StringBuffer aBuffer = new StringBuffer (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 uniqueId(){ 96 return "" + (_counter++); 97 } 98 } 99 | Popular Tags |