tiny fixes here and there
[mir.git] / source / mircoders / localizer / MirCachingLocalizerDecorator.java
1 /*
2  * Copyright (C) 2001, 2002 The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with  any library licensed under the Apache Software License,
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23  * (or with modified versions of the above that use the same license as the above),
24  * and distribute linked combinations including the two.  You must obey the
25  * GNU General Public License in all respects for all of the code used other than
26  * the above mentioned libraries.  If you modify this file, you may extend this
27  * exception to your version of the file, but you are not obligated to do so.
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30
31 package mircoders.localizer;
32
33 import mir.entity.adapter.EntityAdapterModel;
34 import mir.generator.Generator;
35 import mir.generator.WriterEngine;
36
37 /**
38  * This was used to prevent localizers from being created
39  * all the time. It might be obsolete.
40  * 
41  */
42 public class MirCachingLocalizerDecorator implements MirLocalizer {
43   private MirLocalizer localizer;
44   private MirProducerLocalizer producerLocalizer;
45   private MirGeneratorLocalizer generatorLocalizer;
46   private MirOpenPostingLocalizer openPostingsLocalizer;
47   private MirProducerAssistantLocalizer producerAssistantLocalizer;
48   private MirDataModelLocalizer dataModelLocalizer;
49   private MirAdminInterfaceLocalizer adminInterfaceLocalizer;
50   private MirMediaLocalizer mediaLocalizer;
51
52   public MirCachingLocalizerDecorator(MirLocalizer aLocalizer) {
53     localizer = aLocalizer;
54   }
55
56   public MirProducerLocalizer producers() throws MirLocalizerFailure {
57     if (producerLocalizer==null) {
58       producerLocalizer = localizer.producers();
59     }
60
61     return producerLocalizer;
62   }
63
64   public MirGeneratorLocalizer generators() throws MirLocalizerFailure {
65     if (generatorLocalizer==null) {
66       generatorLocalizer = new MirCachingGeneratorLocalizer(localizer.generators());
67     }
68
69     return generatorLocalizer;
70   }
71
72   public MirOpenPostingLocalizer openPostings() throws MirLocalizerFailure {
73     if (openPostingsLocalizer==null) {
74       openPostingsLocalizer = localizer.openPostings();
75     }
76
77     return openPostingsLocalizer;
78   }
79
80   public MirProducerAssistantLocalizer producerAssistant() throws MirLocalizerFailure {
81     if (producerAssistantLocalizer==null) {
82       producerAssistantLocalizer = localizer.producerAssistant();
83     }
84
85     return producerAssistantLocalizer;
86   }
87
88   public MirDataModelLocalizer dataModel() throws MirLocalizerFailure {
89     if (dataModelLocalizer==null) {
90       dataModelLocalizer = new MirCachingDatamodelLocalizer(localizer.dataModel());
91     }
92
93     return dataModelLocalizer;
94   }
95
96   public MirAdminInterfaceLocalizer adminInterface() throws MirLocalizerFailure {
97     if (adminInterfaceLocalizer==null) {
98       adminInterfaceLocalizer = localizer.adminInterface();
99     }
100
101     return adminInterfaceLocalizer;
102   }
103
104   public MirMediaLocalizer media() throws MirLocalizerFailure {
105     if (mediaLocalizer==null) {
106       mediaLocalizer = localizer.media();
107     }
108
109     return mediaLocalizer;
110   }
111
112   private static class MirCachingDatamodelLocalizer implements MirDataModelLocalizer {
113     private MirDataModelLocalizer master;
114     private EntityAdapterModel adapterModel;
115
116     public MirCachingDatamodelLocalizer(MirDataModelLocalizer aMaster) {
117       master = aMaster;
118       adapterModel = null;
119     }
120
121     public EntityAdapterModel adapterModel() throws MirLocalizerExc, MirLocalizerFailure {
122       if (adapterModel==null) {
123         adapterModel = master.adapterModel();
124       }
125
126       return adapterModel;
127     }
128
129   }
130
131   private static class MirCachingGeneratorLocalizer implements MirGeneratorLocalizer {
132     private MirGeneratorLocalizer master;
133     private WriterEngine writerEngine;
134     private Generator.Library producerGeneratorLibrary;
135     private Generator.Library adminGeneratorLibrary;
136     private Generator.Library openPostingGeneratorLibrary;
137
138     public MirCachingGeneratorLocalizer(MirGeneratorLocalizer aMaster) {
139       master = aMaster;
140     }
141
142     public WriterEngine makeWriterEngine() throws MirLocalizerExc, MirLocalizerFailure {
143       if (writerEngine==null) {
144         writerEngine = master.makeWriterEngine();
145       }
146
147       return writerEngine;
148     }
149
150     public Generator.Library makeProducerGeneratorLibrary() throws MirLocalizerExc, MirLocalizerFailure {
151       if (producerGeneratorLibrary==null) {
152         producerGeneratorLibrary = master.makeProducerGeneratorLibrary();
153       }
154
155       return producerGeneratorLibrary;
156     }
157
158     public Generator.Library makeAdminGeneratorLibrary() throws MirLocalizerExc, MirLocalizerFailure {
159       if (adminGeneratorLibrary==null) {
160         adminGeneratorLibrary = master.makeAdminGeneratorLibrary();
161       }
162
163       return adminGeneratorLibrary;
164     }
165
166     public Generator.Library makeOpenPostingGeneratorLibrary() throws MirLocalizerExc, MirLocalizerFailure {
167       if (openPostingGeneratorLibrary==null) {
168         openPostingGeneratorLibrary = master.makeOpenPostingGeneratorLibrary();
169       }
170
171       return openPostingGeneratorLibrary;
172     }
173   }
174
175 }