View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.directory.server.core.avltree;
21  
22  
23  import org.apache.directory.server.i18n.I18n;
24  
25  
26  /**
27   * Stores either a single object or many of them in an AvlTree.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public class SingletonOrOrderedSet<V>
32  {
33      private V singleton;
34      private AvlTree<V> orderedSet;
35  
36  
37      /**
38       * Creates a new instance of SingletonOrOrderedSet with a singleton value.
39       *
40       * @param singleton the singleton value
41       */
42      public SingletonOrOrderedSet( V singleton )
43      {
44          if ( singleton == null )
45          {
46              throw new IllegalArgumentException( I18n.err( I18n.ERR_447 ) );
47          }
48  
49          this.singleton = singleton;
50      }
51  
52  
53      /**
54       * Creates a new instance of SingletonOrOrderedSet with a set of ordered 
55       * values.
56       *
57       * @param orderedSet the set of ordered values
58       */
59      public SingletonOrOrderedSet( AvlTree<V> orderedSet )
60      {
61          if ( orderedSet == null )
62          {
63              throw new IllegalArgumentException( I18n.err( I18n.ERR_448 ) );
64          }
65  
66          this.orderedSet = orderedSet;
67      }
68  
69  
70      /**
71       * Gets whether or not the stored value is a singleton.
72       *
73       * @return true if in singleton mode, false otherwise
74       */
75      public boolean isSingleton()
76      {
77          return singleton != null;
78      }
79  
80  
81      /**
82       * Gets whether or not the stored value is an ordered set.
83       * 
84       * @return true if in ordered set mode, false otherwise
85       */
86      public boolean isOrderedSet()
87      {
88          return orderedSet != null;
89      }
90  
91  
92      /**
93       * Gets the singleton value.
94       *
95       * @return the singleton value
96       * @exception RuntimeException if not in singleton mode
97       */
98      public V getSingleton()
99      {
100         if ( singleton != null )
101         {
102             return singleton;
103         }
104 
105         throw new RuntimeException( I18n.err( I18n.ERR_449 ) );
106     }
107 
108 
109     /**
110      * Sets the singleton if in singleton mode.
111      *
112      * @param singleton the singleton value to set
113      * @return old single value
114      */
115     public V setSingleton( V singleton )
116     {
117         if ( singleton == null )
118         {
119             throw new IllegalArgumentException( I18n.err( I18n.ERR_447 ) );
120         }
121 
122         if ( this.orderedSet != null )
123         {
124             throw new RuntimeException( I18n.err( I18n.ERR_450 ) );
125         }
126 
127         V retval = this.singleton;
128         this.singleton = singleton;
129         return retval;
130     }
131 
132 
133     /**
134      * Switches from orderedSet mode to singleton mode, while returning the 
135      * ordered set of values before removing them forever.
136      *
137      * @param singleton the singleton value
138      * @return the set of ordered values before nulling it out
139      * @exception RuntimeException if already in singleton mode
140      */
141     public AvlTree<V> switchToSingleton( V singleton )
142     {
143         if ( singleton == null )
144         {
145             throw new IllegalArgumentException( I18n.err( I18n.ERR_447 ) );
146         }
147 
148         if ( this.singleton != null )
149         {
150             throw new RuntimeException( I18n.err( I18n.ERR_451 ) );
151         }
152 
153         AvlTree<V> retval = this.orderedSet;
154         this.orderedSet = null;
155         this.singleton = singleton;
156         return retval;
157     }
158 
159 
160     /**
161      * Gets the ordered set.
162      * 
163      * @return the ordered set
164      * @exception RuntimeException if in singleton mode
165      */
166     public AvlTree<V> getOrderedSet()
167     {
168         if ( orderedSet != null )
169         {
170             return orderedSet;
171         }
172 
173         throw new RuntimeException( I18n.err( I18n.ERR_452 ) );
174     }
175 
176 
177     /**
178      * Sets the set of ordered values.
179      *
180      * @param orderedSet the set of ordered values to use
181      * @return the old set of ordered values
182      * @exception RuntimeException if in singleton mode
183      */
184     public AvlTree<V> setOrderedSet( AvlTree<V> orderedSet )
185     {
186         if ( orderedSet == null )
187         {
188             throw new IllegalArgumentException( I18n.err( I18n.ERR_448 ) );
189         }
190 
191         if ( this.singleton != null )
192         {
193             throw new RuntimeException( I18n.err( I18n.ERR_453 ) );
194         }
195 
196         AvlTree<V> retval = this.orderedSet;
197         this.orderedSet = orderedSet;
198         return retval;
199     }
200 
201 
202     /**
203      * Switches from orderedSet mode to singleton mode, while returning the 
204      * singleton value before removing it forever.
205      *
206      * @param orderedSet the AvlTree to use for orderedSet of values
207      * @return the singleton to return before nulling it out
208      * @throws RuntimeException if the mode is already in orderedSet mode.
209      */
210     public V switchToOrderedSet( AvlTree<V> orderedSet )
211     {
212         if ( orderedSet == null )
213         {
214             throw new IllegalArgumentException( I18n.err( I18n.ERR_448 ) );
215         }
216 
217         if ( this.orderedSet != null )
218         {
219             throw new RuntimeException( I18n.err( I18n.ERR_454 ) );
220         }
221 
222         V retval = this.singleton;
223         this.orderedSet = orderedSet;
224         this.singleton = null;
225         return retval;
226     }
227 }