first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mir / storage / store / StoreContainerType.java
1 package mir.storage.store;
2
3 /**
4  * Title:         StoreContainerType
5  *
6  * Description:   StoreContainerTypes are uniqe Objects and are generated
7  *                via @see valueOf(Class stocClass, int stocType).
8  *                For every combination of stocClass and stocType there is
9  *                only one Object instantiated.
10  *
11  * Copyright:     Copyright (c) 2002
12  * Company:       indy
13  *
14  * @author        rk
15  * @version 1.0
16  */
17
18 import java.util.HashMap;
19 import mir.misc.Logfile;
20
21 public class StoreContainerType {
22
23         public final static int     STOC_TYPE_UNKNOWN=-1;
24         public final static int     STOC_TYPE_ENTITY=0;
25         public final static int     STOC_TYPE_ENTITYLIST=1;
26   public final static int     STOC_TYPE_MAX=STOC_TYPE_ENTITYLIST;
27
28         private static HashMap[]    uniqueTypes=new HashMap[STOC_TYPE_MAX+1];
29         private static Logfile      storeLog;
30         private Class               stocClass=null;
31         private int                 stocType=STOC_TYPE_UNKNOWN;
32
33   static {
34     uniqueTypes[STOC_TYPE_ENTITY]= new HashMap();
35     uniqueTypes[STOC_TYPE_ENTITYLIST]=new HashMap();
36
37   }
38
39         private StoreContainerType() {}
40
41         private StoreContainerType(Class stocClass, int stocType) {
42                 this.stocClass=stocClass;
43                 this.stocType=stocType;
44         }
45
46         public static StoreContainerType valueOf(Class stoc_class, int stoc_type) {
47                 StoreContainerType returnStocType=null;
48     if (stoc_type>=0 && stoc_type <= STOC_TYPE_MAX) {
49       HashMap current = uniqueTypes[stoc_type];
50       if ( current.containsKey(stoc_class) )
51                           returnStocType=(StoreContainerType)current.get(stoc_class);
52                   else {
53                           returnStocType=new StoreContainerType(stoc_class,stoc_type);
54                           current.put(stoc_class,returnStocType);
55                   }
56     }
57                 return returnStocType;
58         }
59
60   public int getStocType() { return stocType; }
61   public Class getStocClass() { return stocClass; }
62
63         public String toString() {
64                 StringBuffer sb = new StringBuffer(this.stocClass.toString());
65                 sb.append("@").append(stringForStoreType(stocType));
66                 return sb.toString();
67         }
68
69         private static String stringForStoreType(int stocType) {
70                 switch(stocType) {
71                         case STOC_TYPE_ENTITY: return "ENTITY";
72                         case STOC_TYPE_ENTITYLIST: return "ENTITYLIST";
73                         default: return "UNKNOWN";
74                 }
75         }
76 }