release / invalidate
[mir.git] / source / mir / storage / store / StoreContainer.java
1 package mir.storage.store;
2
3 /**
4  * Title:         StoreContainer
5  *
6  * Description:   This is the bucket object for one type of StorableObjects,
7  *                mainy a linked list of StoreIdenfiers. On use or creation
8  *                an object stored in StoreIdentifier is put to head of the
9  *                list. if maximum size of the list is reached, the
10  *                StoreIdentifier at the end of the list is released.
11  *
12  * Copyright:     Copyright (c) 2002
13  * Company:       indy
14  * @author        //rk
15  * @version 1.0
16  */
17
18 import java.util.*;
19 import mir.misc.Logfile;
20
21 public class StoreContainer {
22
23   private final static int    DEFAULT_SIZE=10;
24   private static Logfile      storeLog;
25   private static int          uniqueCounter=10000;
26
27   private LinkedList          container;
28   private StoreContainerType  stocType;
29   private int                 maxSize=DEFAULT_SIZE;
30   private int                 uniqueId;
31
32   private StoreContainer() {};
33
34   public StoreContainer(StoreContainerType stoc_type) {
35     this.uniqueId=++uniqueCounter;
36     this.stocType=stoc_type;
37     this.container=new LinkedList();
38   }
39
40   public StoreContainer(StoreContainerType stoc_type, int maxSize) {
41     this();
42     this.maxSize=maxSize;
43   }
44
45   public StorableObject use(StoreIdentifier sid) {
46     // find sid in LinkedList or die
47     // move sid to head of linked list
48     // return reference on object
49     return null;
50   }
51
52   public boolean has(StoreIdentifier sid) {
53     return true; // yes yes
54   }
55
56   public void add(StoreIdentifier sid) {
57     // add to head of linkedlist, if size is exeded throw away tail until
58     // size ok.
59   }
60
61   /**
62    *  Method:       invalidate(StorableObject sto)
63    *  Description:  finds @see StorableObject, propagates invalidation to
64    *                @see StoreIdentifier and removes StoreIdentifier from
65    *                list.
66    */
67   public void invalidate(StorableObject sto) {
68     if (sto!=null) {
69       StoreIdentifier sid = sto.getStoreIdentifier();
70       if (sid!=null) {
71         if ( container.contains(sid) ) {
72           sid.invalidate();
73           container.remove(sid);
74         }
75       }
76     }
77   }
78
79   /**
80    *  Method:       setSize
81    *  Description:  readjusts StoreContainer size to value.
82    *
83    */
84   public void setSize(int size) {
85     if (size <0) return;
86     if ( size<maxSize && size > container.size() ) {
87       // shrink
88       while (size > container.size() ) {
89         StoreIdentifier sid = (StoreIdentifier)container.getLast();
90         sid.release();
91         container.remove(sid);
92       }
93     }
94     this.maxSize=size;
95   }
96
97   /**
98    *  Method:       toString()
99    *  Description:  gives out statistical Information, viewable via
100    *                @see ServletStoreInfo.
101    *
102    *  @return       String
103    */
104   public String toString() {
105     StringBuffer sb = new StringBuffer("StoreContainer id: ");
106     sb.append(uniqueId).append(" for ");
107     sb.append(stocType.toString()).append("\nCurrent size: ");
108     sb.append(container.size()).append("\nMaximum size:");
109     sb.append(maxSize).append("\n");
110     /** @todo list members ? */
111     return sb.toString();
112   }
113
114 }