fixes:
[mir.git] / source / mir / storage / store / StoreContainerType.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mir.storage.store;\r
33 \r
34 /**\r
35  * Title:         StoreContainerType\r
36  *\r
37  * Description:   StoreContainerTypes are uniqe Objects and are generated\r
38  *                via @see valueOf(Class stocClass, int stocType).\r
39  *                For every combination of stocClass and stocType there is\r
40  *                only one Object instantiated.\r
41  *\r
42  * Copyright:     Copyright (c) 2002\r
43  * Company:       indy\r
44  *\r
45  * @author        rk\r
46  * @version 1.0\r
47  */\r
48 \r
49 import java.util.HashMap;\r
50 import java.util.Map;\r
51 \r
52 import mir.misc.StringUtil;\r
53 \r
54 public class StoreContainerType {\r
55 \r
56   public final static int STOC_TYPE_UNKNOWN = -1;\r
57   public final static int STOC_TYPE_ENTITY = 0;\r
58   public final static int STOC_TYPE_ENTITYLIST = 1;\r
59   public final static int STOC_TYPE_MAX = STOC_TYPE_ENTITYLIST;\r
60 \r
61   private static Map[] uniqueTypes = new HashMap[STOC_TYPE_MAX + 1];\r
62   private static ObjectStore o_store = ObjectStore.getInstance();\r
63   private Class stocClass = null;\r
64   private int stocType = STOC_TYPE_UNKNOWN;\r
65 \r
66   static {\r
67     uniqueTypes[STOC_TYPE_ENTITY] = new HashMap();\r
68     uniqueTypes[STOC_TYPE_ENTITYLIST] = new HashMap();\r
69   }\r
70 \r
71   private StoreContainerType() {}\r
72 \r
73   private StoreContainerType(Class stocClass, int stocType) {\r
74     this.stocClass = stocClass;\r
75     this.stocType = stocType;\r
76   }\r
77 \r
78   public static StoreContainerType valueOf(Class stoc_class, int stoc_type) {\r
79     StoreContainerType returnStocType = null;\r
80     if (stoc_type >= 0 && stoc_type <= STOC_TYPE_MAX) {\r
81       Map current = uniqueTypes[stoc_type];\r
82       if (current.containsKey(stoc_class))\r
83         returnStocType = (StoreContainerType) current.get(stoc_class);\r
84       else {\r
85         returnStocType = new StoreContainerType(stoc_class, stoc_type);\r
86         current.put(stoc_class, returnStocType);\r
87       }\r
88     }\r
89     return returnStocType;\r
90   }\r
91 \r
92   public int getStocType() {\r
93     return stocType;\r
94   }\r
95 \r
96   public Class getStocClass() {\r
97     return stocClass;\r
98   }\r
99 \r
100   public String getConfPrefix() {\r
101     return StoreUtil.getPropNameFor(stocClass) + "." +\r
102         stringForStoreType(stocType);\r
103   }\r
104 \r
105   public int getDefaultSize() {\r
106     String confProperty = "StoreContainer." + stringForStoreType(stocType) +\r
107         ".DefaultSize";\r
108     return\r
109         StringUtil.parseInt(o_store.getConfProperty(confProperty), 10);\r
110   }\r
111 \r
112   public String toString() {\r
113     StringBuffer sb = new StringBuffer(this.stocClass.toString());\r
114     sb.append("@").append(stringForStoreType(stocType));\r
115     return sb.toString();\r
116   }\r
117 \r
118   private static String stringForStoreType(int stocType) {\r
119     switch (stocType) {\r
120       case STOC_TYPE_ENTITY:\r
121         return "Entity";\r
122       case STOC_TYPE_ENTITYLIST:\r
123         return "EntityList";\r
124       default:\r
125         return "UNKNOWN";\r
126     }\r
127   }\r
128 }