add & use is working with little testsuite. information about hits/misses added.
[mir.git] / source / mir / storage / store / StoreIdentifier.java
1 package mir.storage.store;
2
3 /**
4  * Title:         Class StoreIdentifier
5  * Description:   StoreIdentifier has two functions.
6  *                A) StoreIdenfier holds a reference to a @see StorableObject
7  *                or B) StoreIdentifier is used too search for a @see StorableObject
8  *                in the @see StoreContainer that matches its
9  *
10  * Copyright:     Copyright (c) 2002
11  * Company:       indy
12  * @author        rk
13  * @version 1.0
14  */
15 import java.util.*;
16 import mir.misc.Logfile;
17
18 public class StoreIdentifier {
19
20         /** @todo check if invalidating already to avoid deadlocks
21          *  what about concurrency? */
22
23         private static Logfile      storeLog;
24
25         private StoreContainerType  stocType=null;
26         private StorableObject      reference=null;
27         private String              uniqueIdentifier=null; // id for Entity & sql for EntityList
28         private long                timesUsed;
29         private boolean             invalidating=false;
30
31         /** @todo initialize logfile  */
32
33         private StoreIdentifier() {}
34
35         public StoreIdentifier(StorableObject reference, int storeType, String uniqueIdentifier) {
36     this(reference.getClass(), uniqueIdentifier);
37                 this.reference=reference;
38         }
39
40   public StoreIdentifier(Class theClass, String uniqueIdentifier) {
41     this(theClass, StoreContainerType.STOC_TYPE_ENTITY,uniqueIdentifier);
42   }
43
44   public StoreIdentifier(Class theClass, int storeType, String uniqueIdentifier) {
45     this.uniqueIdentifier=uniqueIdentifier;
46                 this.stocType = StoreContainerType.valueOf(theClass, storeType);
47   }
48         /**
49          *  Method:       ivalidate
50          *  Description:
51          *
52          *  @return
53          */
54         public void invalidate() {
55                 // avoid deadlock due to propagation.
56                 if (!invalidating) {
57                         invalidating=true;
58                         Set set = reference.getNotifyOnReleaseSet();
59                         /** @todo here we should propagate the invalidation all members of Set
60                         *    @see StoreContainer. The set may contain objects of different type.*/
61                         release();
62                 }
63         }
64
65         public void release() {
66                 this.reference=null;
67                 this.uniqueIdentifier=null;
68                 this.stocType=null;
69         }
70
71         public StorableObject use() {
72                 timesUsed++;
73                 return reference;
74         }
75
76         /**
77          *  Method equals for comparison between two identifier
78          *
79          *  @return true if yes otherwise false
80          *
81          */
82         public boolean equals(Object sid) {
83     if ( !(sid instanceof StoreIdentifier) ) return false;
84     if ( ((StoreIdentifier)sid).getStoreContainerType()==stocType &&
85          ((StoreIdentifier)sid).getUniqueIdentifier().equals(uniqueIdentifier) )
86                                 return true;
87                 return false;
88         }
89
90         public StoreContainerType getStoreContainerType() { return stocType; }
91         public String getUniqueIdentifier() { return uniqueIdentifier; }
92         public boolean hasReference() { return (reference==null) ? false:true; }
93
94         public String toString() {
95     StringBuffer id = new StringBuffer(uniqueIdentifier);
96     id.append("@storetype: ").append(stocType.toString());
97     if (reference != null) id.append(" ("+timesUsed).append(") times used.");
98                 return id.toString();
99         }
100
101
102 }