View Javadoc

1   //
2   //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3   //All rights reserved.
4   //
5   //Redistribution and use in source and binary forms, with or without modification, 
6   //are permitted provided that the following conditions are met:
7   //
8   //* Redistributions of source code must retain the above copyright notice, 
9   //this list of conditions and the following disclaimer.
10  //* Redistributions in binary form must reproduce the above copyright notice, 
11  //this list of conditions and the following disclaimer in the documentation 
12  //and/or other materials provided with the distribution.
13  //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
14  //nor the names of its contributors may be used to endorse or promote products 
15  //derived from this software without specific prior written permission.
16  //
17  //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
18  //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
19  //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
21  //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
22  //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23  //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
24  //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
25  //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
26  //POSSIBILITY OF SUCH DAMAGE.
27  //
28  
29  package org.objectledge.parameters;
30  
31  import java.util.ArrayList;
32  import java.util.Arrays;
33  import java.util.Date;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Set;
37  import java.util.SortedSet;
38  import java.util.TreeSet;
39  
40  /**
41   * A compound implementation of parameters.
42   *
43   * @author <a href="mailto:pablo@caltha.com">Pawel Potempski</a>
44   * @author <a href="mailto:rafal@caltha.com">Rafal Krzewski</a>
45   * @version $Id: CompoundParameters.java,v 1.8 2006/01/12 15:51:01 rafal Exp $
46   */
47  public class CompoundParameters implements Parameters
48  {
49      /*** The underylying containers. */
50      private List<Parameters> containers;
51  
52      /***
53       * Constructs a copound parameter container.
54       *
55       * <p>The contatiners with lesser indexes will have precedence over the
56       * conainer with greater indexes.</p>
57       *
58       * @param containers the containers.
59       */
60      public CompoundParameters(Parameters... containers)
61      {
62          this.containers = Arrays.asList(containers);
63      }
64  
65      /***
66       * Constructs a copound parameter container.
67       *
68       * <p>The contatiners with lesser indexes will have precenence over the
69       * conainer with greater indexes.</p>
70       *
71       * @param list the containers.
72       */
73      public CompoundParameters(List<Parameters> list)
74      {
75          containers = list;
76          Iterator i = list.iterator();
77          while(i.hasNext())
78          {
79              Object obj = i.next();
80              if(!(obj instanceof Parameters))
81              {
82                  throw new ClassCastException(obj.getClass().getName());
83              }
84          }
85      }
86  
87      
88      /***
89       * {@inheritDoc}
90       */
91      public boolean isDefined(String name)
92      {
93          Iterator i = containers.iterator();
94          while(i.hasNext())
95          {
96              Parameters c = (Parameters)i.next();
97              if(c.isDefined(name))
98              {
99                  return true;
100             }
101         }
102         return false;
103     }
104 
105     /***
106      * {@inheritDoc}
107      */
108     public String[] getParameterNames()
109     {
110         SortedSet<String> keys = new TreeSet<String>();
111         Iterator i = containers.iterator();
112         while(i.hasNext())
113         {
114             Parameters c = (Parameters)i.next();
115             keys.addAll(Arrays.asList(c.getParameterNames()));
116         }
117         String[] result = new String[keys.size()];
118         keys.toArray(result);
119         return result;
120     }
121 
122     /***
123      * {@inheritDoc}
124      */
125     public String get(String name)
126     {
127         Iterator i = containers.iterator();
128         while(i.hasNext())
129         {
130             Parameters c = (Parameters)i.next();
131             if(c.isDefined(name))
132             {
133                 return c.get(name);
134             }
135         }
136         throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
137     }
138     
139     /***
140      * {@inheritDoc}
141      */
142     public String get(String name, String defaultValue)
143     {
144         Iterator i = containers.iterator();
145         while(i.hasNext())
146         {
147             Parameters c = (Parameters)i.next();
148             if(c.isDefined(name))
149             {
150                 return c.get(name);
151             }
152         }
153         return defaultValue;
154     }
155 
156     /***
157      * {@inheritDoc}
158      */
159     public String[] getStrings(String name)
160     {
161         Iterator i = containers.iterator();
162         while(i.hasNext())
163         {
164             Parameters c = (Parameters)i.next();
165             if(c.isDefined(name))
166             {
167                 return c.getStrings(name);
168             }
169         }
170         return new String[0];
171     }
172 
173     /***
174      * {@inheritDoc}
175      */
176     public boolean getBoolean(String name)
177     {
178         Iterator i = containers.iterator();
179         while(i.hasNext())
180         {
181             Parameters c = (Parameters)i.next();
182             if(c.isDefined(name))
183             {
184                 return c.getBoolean(name);
185             }
186         }
187         throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
188     }
189     
190     /***
191      * {@inheritDoc}
192      */
193     public boolean getBoolean(String name, boolean defaultValue)
194     {
195         Iterator i = containers.iterator();
196         while(i.hasNext())
197         {
198             Parameters c = (Parameters)i.next();
199             if(c.isDefined(name))
200             {
201                 return c.getBoolean(name);
202             }
203         }
204         return defaultValue;
205     }
206 
207     /***
208      * {@inheritDoc}
209      */
210     public boolean[] getBooleans(String name)
211     {
212         Iterator i = containers.iterator();
213         while(i.hasNext())
214         {
215             Parameters c = (Parameters)i.next();
216             if(c.isDefined(name))
217             {
218                 return c.getBooleans(name);
219             }
220         }
221         return new boolean[0];
222     }
223 
224     /***
225      * {@inheritDoc}
226      */
227     public Date getDate(String name)
228     {
229         Iterator i = containers.iterator();
230         while(i.hasNext())
231         {
232             Parameters c = (Parameters)i.next();
233             if(c.isDefined(name))
234             {
235                 return c.getDate(name);
236             }
237         }
238         throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
239     }
240 
241     /***
242      * {@inheritDoc}
243      */
244     public Date getDate(String name, Date defaultValue)
245     {
246         Iterator i = containers.iterator();
247         while(i.hasNext())
248         {
249             Parameters c = (Parameters)i.next();
250             if(c.isDefined(name))
251             {
252                 return c.getDate(name, defaultValue);
253             }
254         }
255         return defaultValue;
256     }
257 
258     /***
259      * {@inheritDoc}
260      */
261     public Date[] getDates(String name)
262     {
263         Iterator i = containers.iterator();
264         while(i.hasNext())
265         {
266             Parameters c = (Parameters)i.next();
267             if(c.isDefined(name))
268             {
269                 return c.getDates(name);
270             }
271         }
272         return new Date[0];
273     }
274     
275     /***
276      * {@inheritDoc}
277      */
278     public int getInt(String name)
279     {
280         Iterator i = containers.iterator();
281         while(i.hasNext())
282         {
283             Parameters c = (Parameters)i.next();
284             if(c.isDefined(name))
285             {
286                 return c.getInt(name);
287             }
288         }
289         throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
290     }
291     
292     /***
293      * {@inheritDoc}
294      */
295     public int getInt(String name, int defaultValue)
296     {
297         Iterator i = containers.iterator();
298         while(i.hasNext())
299         {
300             Parameters c = (Parameters)i.next();
301             if(c.isDefined(name))
302             {
303                 return c.getInt(name, defaultValue);
304             }
305         }
306         return defaultValue;
307     }
308 
309     /***
310      * {@inheritDoc}
311      */
312     public int[] getInts(String name)
313     {
314         Iterator i = containers.iterator();
315         while(i.hasNext())
316         {
317             Parameters c = (Parameters)i.next();
318             if(c.isDefined(name))
319             {
320                 return c.getInts(name);
321             }
322         }
323         return new int[0];
324     }
325 
326     /***
327      * {@inheritDoc}
328      */
329     public long getLong(String name)
330     {
331         Iterator i = containers.iterator();
332         while(i.hasNext())
333         {
334             Parameters c = (Parameters)i.next();
335             if(c.isDefined(name))
336             {
337                 return c.getLong(name);
338             }
339         }
340         throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
341     }
342     
343     /***
344      * {@inheritDoc}
345      */
346     public long getLong(String name, long defaultValue)
347     {
348         Iterator i = containers.iterator();
349         while(i.hasNext())
350         {
351             Parameters c = (Parameters)i.next();
352             if(c.isDefined(name))
353             {
354                 return c.getLong(name, defaultValue);
355             }
356         }
357         return defaultValue;
358     }
359 
360     /***
361      * {@inheritDoc}
362      */
363     public long[] getLongs(String name)
364     {
365         Iterator i = containers.iterator();
366         while(i.hasNext())
367         {
368             Parameters c = (Parameters)i.next();
369             if(c.isDefined(name))
370             {
371                 return c.getLongs(name);
372             }
373         }
374         return new long[0];
375     }
376 
377     /***
378      * {@inheritDoc}
379      */
380     public float getFloat(String name)
381     {
382         Iterator i = containers.iterator();
383         while(i.hasNext())
384         {
385             Parameters c = (Parameters)i.next();
386             if(c.isDefined(name))
387             {
388                 return c.getFloat(name);
389             }
390         }
391         throw new UndefinedParameterException("Parameter '" + name + "'is undefined");
392     }
393     
394     /***
395      * {@inheritDoc}
396      */
397     public float getFloat(String name, float defaultValue)
398     {
399         Iterator i = containers.iterator();
400         while(i.hasNext())
401         {
402             Parameters c = (Parameters)i.next();
403             if(c.isDefined(name))
404             {
405                 return c.getFloat(name, defaultValue);
406             }
407         }
408         return defaultValue;
409     }
410 
411     /***
412      * {@inheritDoc}
413      */
414     public float[] getFloats(String name)
415     {
416         Iterator i = containers.iterator();
417         while(i.hasNext())
418         {
419             Parameters c = (Parameters)i.next();
420             if(c.isDefined(name))
421             {
422                 return c.getFloats(name);
423             }
424         }
425         return new float[0];
426     }
427 
428     /***
429      * {@inheritDoc}
430      */
431     public Parameters getChild(String prefix)
432     {
433         List<Parameters> list = new ArrayList<Parameters>();
434         Iterator i = containers.iterator();
435         while(i.hasNext())
436         {
437             Parameters c = (Parameters)i.next();
438             list.add(c.getChild(prefix));
439         }
440         return new CompoundParameters(list);
441     }
442     
443     //// ---------------------------------
444 
445     /***
446      * {@inheritDoc}
447      */
448     public void remove()
449     {
450         throw new UnsupportedOperationException();
451     }
452 
453     /***
454      * {@inheritDoc}
455      */
456     public void remove(String name)
457     {
458         throw new UnsupportedOperationException();
459     }
460 
461     /***
462      * {@inheritDoc}
463      */
464     public void remove(String name, String value)
465     {
466         throw new UnsupportedOperationException();
467     }
468 
469     /***
470      * {@inheritDoc}
471      */
472     public void remove(String name, Date value)
473     {
474         throw new UnsupportedOperationException();
475     }
476 
477     /***
478      * {@inheritDoc}
479      */
480     public void remove(String name, float value)
481     {
482         throw new UnsupportedOperationException();
483     }
484 
485     /***
486      * {@inheritDoc}
487      */
488     public void remove(String name, int value)
489     {
490         throw new UnsupportedOperationException();
491     }
492 
493     /***
494      * {@inheritDoc}
495      */
496     public void remove(String name, long value)
497     {
498         throw new UnsupportedOperationException();
499     }
500 
501     /***
502      * Remove all parameters with a name contained in given set.
503      *
504      * @param keys the set of keys.
505      */
506     public void remove(Set keys)
507     {
508         throw new UnsupportedOperationException();
509     }
510 
511     /***
512      * Remove all except those with a keys specified in the set.
513      *
514      * @param keys the set of names.
515      */
516     public void removeExcept(Set keys)
517     {
518         throw new UnsupportedOperationException();
519     }
520 
521     /***
522      * {@inheritDoc}
523      */
524     public void set(String name, String value)
525     {
526         throw new UnsupportedOperationException();
527     }
528 
529     /***
530      * {@inheritDoc}
531      */
532     public void set(String name, String[] values)
533     {
534         throw new UnsupportedOperationException();
535     }
536 
537     /***
538      * {@inheritDoc}
539      */
540     public void set(String name, Date value)
541     {
542         throw new UnsupportedOperationException();
543     }
544 
545     /***
546      * {@inheritDoc}
547      */
548     public void set(String name, Date[] values)
549     {
550         throw new UnsupportedOperationException();
551     }
552 
553     /***
554      * {@inheritDoc}
555      */
556     public void set(String name, boolean value)
557     {
558         throw new UnsupportedOperationException();
559     }
560 
561     /***
562      * {@inheritDoc}
563      */
564     public void set(String name, boolean[] values)
565     {
566         throw new UnsupportedOperationException();
567     }
568 
569     /***
570      * {@inheritDoc}
571      */
572     public void set(String name, float value)
573     {
574         throw new UnsupportedOperationException();
575     }
576 
577     /***
578      * {@inheritDoc}
579      */
580     public void set(String name, float[] values)
581     {
582         throw new UnsupportedOperationException();
583     }
584 
585     /***
586      * {@inheritDoc}
587      */
588     public void set(String name, int value)
589     {
590         throw new UnsupportedOperationException();
591     }
592 
593     /***
594      * {@inheritDoc}
595      */
596     public void set(String name, int[] values)
597     {
598         throw new UnsupportedOperationException();
599     }
600 
601     /***
602      * {@inheritDoc}
603      */
604     public void set(String name, long value)
605     {
606         throw new UnsupportedOperationException();
607     }
608 
609     /***
610      * {@inheritDoc}
611      */
612     public void set(String name, long[] values)
613     {
614         throw new UnsupportedOperationException();
615     }
616 
617     /***
618      * {@inheritDoc}
619      */
620 	public void set(Parameters parameters)
621 	{
622 		throw new UnsupportedOperationException();
623 	}
624 	
625     /***
626      * {@inheritDoc}
627      */
628     public void add(String name, String value)
629     {
630         throw new UnsupportedOperationException();
631     }
632 
633     /***
634      * {@inheritDoc}
635      */
636     public void add(String name, String[] values)
637     {
638         throw new UnsupportedOperationException();
639     }
640 
641     /***
642      * {@inheritDoc}
643      */
644     public void add(String name, Date value)
645     {
646         throw new UnsupportedOperationException();
647     }
648 
649     /***
650      * {@inheritDoc}
651      */
652     public void add(String name, Date[] values)
653     {
654         throw new UnsupportedOperationException();
655     }
656     
657     /***
658      * {@inheritDoc}
659      */
660     public void add(String name, boolean value)
661     {
662         throw new UnsupportedOperationException();
663     }
664 
665     /***
666      * {@inheritDoc}
667      */
668     public void add(String name, boolean[] values)
669     {
670         throw new UnsupportedOperationException();
671     }
672 
673     /***
674      * {@inheritDoc}
675      */
676     public void add(String name, float value)
677     {
678         throw new UnsupportedOperationException();
679     }
680 
681     /***
682      * {@inheritDoc}
683      */
684     public void add(String name, float[] values)
685     {
686         throw new UnsupportedOperationException();
687     }
688 
689     /***
690      * {@inheritDoc}
691      */
692     public void add(String name, int value)
693     {
694         throw new UnsupportedOperationException();
695     }
696 
697     /***
698      * {@inheritDoc}
699      */
700     public void add(String name, int[] values)
701     {
702         throw new UnsupportedOperationException();
703     }
704 
705     /***
706      * {@inheritDoc}
707      */
708     public void add(String name, long value)
709     {
710         throw new UnsupportedOperationException();
711     }
712 
713     /***
714      * {@inheritDoc}
715      */
716     public void add(String name, long[] values)
717     {
718         throw new UnsupportedOperationException();
719     }
720 
721     /***
722      * {@inheritDoc}
723      */
724     public void add(Parameters parameters, boolean overwrite)
725     {
726         throw new UnsupportedOperationException();
727     }
728 }