View Javadoc
1   /*
2    * GenericResourceSet.java
3    *
4    * Created on December 21, 2004, 1:21 PM
5    */
6   
7   package gov.noaa.eds.xapi.generic;
8   
9   import java.util.Vector;
10  import org.xmldb.api.base.ErrorCodes;
11  import org.xmldb.api.base.Resource;
12  import org.xmldb.api.base.ResourceIterator;
13  import org.xmldb.api.base.ResourceSet;
14  import org.xmldb.api.base.XMLDBException;
15  
16  /***
17   * A basic Set of resources.
18   * @version $Id: GenericResourceSet.java,v 1.2 2004/12/23 22:26:01 mrxtravis Exp $
19   * @author tns
20   */
21  public class GenericResourceSet extends Vector implements ResourceSet {
22      
23      /*** Creates a new instance of GenericResourceSet */
24      public GenericResourceSet() {
25      }
26      
27      /***Add a resource to this set
28       *@param resource The resource to add
29       */
30      public void addResource(Resource resource) throws XMLDBException {
31          this.add(resource);
32      }
33      
34      /***Removes the resource at the specified index
35       *@param index The index of the resource to remove.
36       */
37      public void removeResource(long index) throws XMLDBException {
38          int intIndex = convertLong(index);
39          this.remove(intIndex);
40      }
41      
42      /***Returns the size of this set.
43       *@return The size
44       */
45      public long getSize() throws XMLDBException {
46          return (long) this.size();
47      }
48      
49      /***Vectors use ints and xmldb api requires long so this converts
50       *the long to an int, throwing an exception if the long is out of the
51       *integer range.
52       */
53      private int convertLong(long l){
54          if (l > Integer.MAX_VALUE){
55              throw new IllegalStateException("specified parameter can not be " +
56                      "larger than the largest int value" + Integer.MAX_VALUE +
57                      " but it was " + l);
58          }
59          return (int) l;
60      }
61      
62      /***Returns the resource at the specified index.  Throws an XMLDBException 
63       *if there is the index is inappropriate.
64       *@param index The index of the resource to return
65       *@return The resource at the specified index.
66       */
67      public Resource getResource(long index) throws XMLDBException {
68          int intIndex = convertLong(index);
69          try {
70              return (Resource) this.get(intIndex);
71          } catch (IndexOutOfBoundsException e){
72              throw new XMLDBException(ErrorCodes.NO_SUCH_RESOURCE);
73          }
74      }
75      
76      /***Not implemented
77       *@todo Implement this method
78       */
79      public Resource getMembersAsResource() throws XMLDBException {
80          throw new XMLDBException(ErrorCodes.NOT_IMPLEMENTED);
81      }
82      
83      /***Returns an iterator for this set of resources
84       *@return The iterator.
85       */
86      public ResourceIterator getIterator() throws XMLDBException {
87          return new GenericResourceIterator(this.iterator());
88      }
89      
90      
91  }