001    package com.sptci.rwt;
002    
003    import java.io.Serializable;
004    import java.util.Collection;
005    import java.util.Collections;
006    import java.util.Map;
007    import java.util.TreeMap;
008    
009    /**
010     * A simple data object used to represent a category under which named
011     * queries are stored.
012     *
013     * <p>&copy; Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
014     * @author Rakesh Vidyadharan 2007-10-10
015     * @version $Id: Category.java 4123 2008-05-25 21:49:01Z rakesh $
016     */
017    public class Category implements Serializable
018    {
019      /** The name of the category. */
020      private String name;
021    
022      /** The named queries associated with this category. */
023      private Map<String,Query> queries = new TreeMap<String,Query>();
024    
025      /** Default constructor. */
026      public Category() {}
027    
028      /**
029       * Create a new instance using the specified name.
030       *
031       * @param name The {@link #name} to use.
032       */
033      public Category( final String name )
034      {
035        setName( name );
036      }
037      
038      /**
039       * Returns {@link #name}.
040       *
041       * @return The value/reference of/to name.
042       */
043      public String getName()
044      {
045        return name;
046      }
047      
048      /**
049       * Set {@link #name}.
050       *
051       * @param name The value to set.
052       */
053      protected void setName( final String name )
054      {
055        this.name = name;
056      }
057    
058      /**
059       * Add the specified query as a saved instance.
060       *
061       * @param query The query to add to the saved state.
062       * @throws IllegalArgumentException If the specified name already exists.
063       */
064      protected void addQuery( final Query query ) throws IllegalArgumentException
065      {
066        if ( queries.containsKey( query.getKey() ) )
067        {
068          throw new IllegalArgumentException( "A query with name: " +
069              query.getKey() + " already exists!" );
070        }
071    
072        queries.put( query.getKey(), query );
073      }
074    
075      /**
076       * Remove the query specified from persistent state.
077       *
078       * @see #deleteQuery( String )
079       * @param query The query that is to be removed.
080       */
081      protected void deleteQuery( final Query query )
082      {
083        deleteQuery( query.getKey() );
084      }
085    
086      /**
087       * Remove the query identified by the specified unique name from
088       * persistent state.
089       *
090       * @param name The unique name assigned to the query.
091       */
092      protected void deleteQuery( final String name )
093      {
094        queries.remove( name );
095      }
096    
097      /**
098       * Return the query associated with the specified name.
099       *
100       * @param name The unique name used to identify the saved query.
101       * @return The saved query instance or <code>null</code> if no such
102       *   saved query exists.
103       */
104      public Query getQuery( final String name )
105      {
106        return queries.get( name );
107      }
108      
109      /**
110       * Returns a collection of {@link Query} objects that are stored under
111       * this category.
112       *
113       * @return The collection of query instances.
114       */
115      public Collection<Query> getQueries()
116      {
117        return Collections.unmodifiableCollection( queries.values() );
118      }
119      
120      /**
121       * Set {@link #queries}.
122       *
123       * @param queries The value to set.
124       */
125      protected void setQueries( final Map<String,Query> queries )
126      {
127        this.queries.clear();
128        this.queries.putAll( queries ); 
129      }
130    
131      /**
132       * Returns the unique names that have been used to save queries.
133       *
134       * @return The collection of unique names assigned to queries.
135       */
136      public Collection<String> getNames()
137      {
138        return Collections.unmodifiableCollection( queries.keySet() );
139      }
140    }