a2d3aeb13d42245faa478ee870b1ecf02356de38
[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=0;
24         public final static int    STOC_TYPE_ENTITY=1;
25         public final static int    STOC_TYPE_ENTITYLIST=2;
26
27         private static HashMap      uniqueTypes = new HashMap(); // StoreKey / StoreContainerType
28         private static Logfile      storeLog;
29         private Class               stocClass=null;
30         private int                 stocType=STOC_TYPE_UNKNOWN;
31
32         private StoreContainerType() {}
33
34         private StoreContainerType(Class stocClass, int stocType) {
35                 this.stocClass=stocClass;
36                 this.stocType=stocType;
37         }
38
39         public static StoreContainerType valueOf(Class stoc_class, int stoc_type) {
40                 /** @todo factory, only gives out types once to make them comparable via ==
41                         *   check if class is StorableObject */
42                 StoreContainerType returnStocType=null;
43
44                         // inner class for hashlookup
45                          class StoreKey {
46                                 int   storeType; Class storeClass;
47         public boolean equals(Object o) {
48           if ( o instanceof StoreKey &&
49                ((StoreKey)o).storeType==storeType &&
50                ((StoreKey)o).storeClass==storeClass )
51             return true;
52           return false;
53         }
54                         }
55
56                 StoreKey key = new StoreKey();
57                 key.storeClass = stoc_class; key.storeType = stoc_type;
58                 if ( uniqueTypes.containsKey(key) )
59                         returnStocType=(StoreContainerType)uniqueTypes.get(key);
60                 else {
61                         returnStocType=new StoreContainerType(stoc_class,stoc_type);
62                         uniqueTypes.put(key,returnStocType);
63                 }
64                 return returnStocType;
65         }
66
67         public String toString() {
68                 StringBuffer sb = new StringBuffer("StoreContainerType: ");
69                 sb.append(this.stocClass.toString()).append("@");
70                 sb.append(stringForStoreType(stocType));
71                 return sb.toString();
72         }
73
74         private static String stringForStoreType(int stocType) {
75                 switch(stocType) {
76                         case STOC_TYPE_ENTITY: return "ENTITY";
77                         case STOC_TYPE_ENTITYLIST: return "ENTITYLIST";
78                         default: return "UNKNOWN";
79                 }
80         }
81 }