001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.directory.server.core.avltree; 021 022 023import org.apache.directory.server.i18n.I18n; 024 025 026/** 027 * Stores either a single object or many of them in an AvlTree. 028 * 029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 030 */ 031public class SingletonOrOrderedSet<V> 032{ 033 private V singleton; 034 private AvlTree<V> orderedSet; 035 036 037 /** 038 * Creates a new instance of SingletonOrOrderedSet with a singleton value. 039 * 040 * @param singleton the singleton value 041 */ 042 public SingletonOrOrderedSet( V singleton ) 043 { 044 if ( singleton == null ) 045 { 046 throw new IllegalArgumentException( I18n.err( I18n.ERR_447 ) ); 047 } 048 049 this.singleton = singleton; 050 } 051 052 053 /** 054 * Creates a new instance of SingletonOrOrderedSet with a set of ordered 055 * values. 056 * 057 * @param orderedSet the set of ordered values 058 */ 059 public SingletonOrOrderedSet( AvlTree<V> orderedSet ) 060 { 061 if ( orderedSet == null ) 062 { 063 throw new IllegalArgumentException( I18n.err( I18n.ERR_448 ) ); 064 } 065 066 this.orderedSet = orderedSet; 067 } 068 069 070 /** 071 * Gets whether or not the stored value is a singleton. 072 * 073 * @return true if in singleton mode, false otherwise 074 */ 075 public boolean isSingleton() 076 { 077 return singleton != null; 078 } 079 080 081 /** 082 * Gets whether or not the stored value is an ordered set. 083 * 084 * @return true if in ordered set mode, false otherwise 085 */ 086 public boolean isOrderedSet() 087 { 088 return orderedSet != null; 089 } 090 091 092 /** 093 * Gets the singleton value. 094 * 095 * @return the singleton value 096 * @exception RuntimeException if not in singleton mode 097 */ 098 public V getSingleton() 099 { 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}