View Javadoc

1   /*
2    * Copyright (c) 2004-2005, University Health Network.  All rights reserved. Distributed under the BSD 
3    * license (see http://opensource.org/licenses/bsd-license.php).
4    *  
5    * Created on 22-Dec-2004
6    */
7   package ca.uhn.cache.internal.impl;
8   
9   import java.util.ArrayList;
10  import java.util.HashMap;
11  import java.util.List;
12  import java.util.Map;
13  
14  import org.apache.commons.collections.ListUtils;
15  import org.apache.commons.collections.MapUtils;
16  
17  import ca.uhn.cache.IDimension;
18  import ca.uhn.cache.IQueryParam;
19  import ca.uhn.cache.internal.IParamSpaceConfig;
20  
21  /***
22   * Default implementation of IParamSpaceConfig. 
23   *  
24   * @author <a href="mailto:bryan.tripp@uhn.on.ca">Bryan Tripp</a>
25   * @version $Revision: 1.1 $ updated on $Date: 2005/01/24 22:54:05 $ by $Author: bryan_tripp $
26   */
27  public class ParamSpaceConfig implements IParamSpaceConfig {
28      
29      private List myDimensions;
30      private Map myChunkBoundaries;
31      private Map mySaturationPoints;
32  
33      /***
34       * Constructs ParamSpaceConfig.
35       * 
36       * @param theDimensions
37       *            Dimensions that define a IParamSpace.
38       *            
39       * @param theAllChunkBoundaries (many per dimension)
40       *            Chunk boundaries for all dimensions.  The dimension to which each corresponds is determined 
41       *            by getDimension().  The values are IQueryParam[], to be returned by getChunkBoundaries(). If 
42       *            there are no entries for a certain IDimension then isChunked() will return false.  
43       *            
44       * @param theAllSaturationPoints (at most one per dimension)
45       *            IQueryParam that define the distance 1 (for ordered dimensions
46       *            only). See IParamSpaceConfig.getSaturationPoint().
47       */
48      public ParamSpaceConfig( 
49              IDimension[] theDimensions, 
50              IQueryParam[] theAllChunkBoundaries,
51              IQueryParam[] theAllSaturationPoints ) {
52          
53          assert theDimensions!=null;
54          assert theAllChunkBoundaries!=null; 
55          assert theAllSaturationPoints!=null;
56  
57          initDimensions( theDimensions );
58          initAllChunkBoundaries( theAllChunkBoundaries );
59          initAllSaturationPoints( theAllSaturationPoints );
60          
61      }
62  
63      private void initAllSaturationPoints( IQueryParam[] theAllSaturationPoints ) {
64          mySaturationPoints = MapUtils.typedMap( new HashMap(), IDimension.class, IQueryParam.class );
65          
66          for (int i = 0; i < theAllSaturationPoints.length; i++) {
67              IQueryParam satPoint = theAllSaturationPoints[i];
68              
69              IDimension dim = satPoint.getDimension();
70              assert myDimensions.contains( dim ) : 
71                     "The saturation point " + satPoint + " belongs to an unknown dimension";
72              
73              assert mySaturationPoints.get( dim ) == null :
74                     "More than one saturation point has been defined for the dimension " + dim;
75              
76              mySaturationPoints.put( dim, satPoint );
77          }
78      }
79  
80      /***
81       * @param theAllChunkBoundaries
82       */
83      private void initAllChunkBoundaries( IQueryParam[] theAllChunkBoundaries ) {
84          myChunkBoundaries = MapUtils.typedMap( new HashMap(), IDimension.class, List.class );
85          
86          for (int i = 0; i < theAllChunkBoundaries.length; i++) {
87              IQueryParam queryParam = theAllChunkBoundaries[i];
88              
89              IDimension dim = queryParam.getDimension();
90              assert myDimensions.contains( dim );
91              
92              List boundaries = (List) myChunkBoundaries.get( dim );
93              if ( boundaries == null ) {
94                  boundaries = ListUtils.typedList( new ArrayList(), IQueryParam.class );
95              }
96              boundaries.add( queryParam );
97              
98              myChunkBoundaries.put( dim, boundaries );
99          }
100     }
101 
102     /***
103      * @param theDimensions
104      */
105     private void initDimensions( IDimension[] theDimensions ) {
106         myDimensions = ListUtils.typedList( new ArrayList(), IDimension.class );
107         
108         for (int i = 0; i < theDimensions.length; i++) {
109             IDimension dimension = theDimensions[i];
110             myDimensions.add( dimension );
111         }
112         
113     }
114 
115     /***
116      * {@inheritDoc}
117      */
118     public IDimension[] getDimensions() {
119         return (IDimension[]) myDimensions.toArray( new IDimension[ myDimensions.size() ] );
120     }
121 
122     /***
123      * {@inheritDoc}
124      */
125     public IQueryParam[] getChunkBoundaries( IDimension theDimension ) {
126         assert isKnownDimension(theDimension) : "The dimension " + theDimension + " is NOT known";
127         
128         IQueryParam[] retVal;
129         List chunkBoundaries = (List) myChunkBoundaries.get( theDimension );
130         
131         if ( chunkBoundaries == null ) {
132             retVal = new IQueryParam[] {};  
133         }
134         else {
135             retVal = (IQueryParam[]) chunkBoundaries.toArray( new IQueryParam[ chunkBoundaries.size() ] );    
136         }
137         
138         return retVal;
139     }
140 
141     private boolean isKnownDimension( IDimension theDimension ) {
142         return myDimensions.contains( theDimension );
143     }
144 
145     /***
146      * {@inheritDoc}
147      */
148     public boolean isChunked( IDimension theDimension ) {
149         assert isKnownDimension(theDimension) : "The dimension " + theDimension + " is NOT known";
150         return myChunkBoundaries.containsKey(theDimension);
151     }
152 
153     /***
154      * {@inheritDoc}
155      */
156     public IQueryParam getSaturationPoint( IDimension theDimension ) {
157         return (IQueryParam) mySaturationPoints.get( theDimension );
158     }
159 
160 }
161