Package speculoos.utils

Source Code of speculoos.utils.StrictMap

/*---------------------------------------------------------------------------
* Speculoos, LDAP to objet mapping.
* Copyright (C) 2006  Norsys and oQube
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
* --------------------------------------------------------------------------*/
package speculoos.utils;

import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.map.AbstractMapDecorator;

/**
* Decorates a map to forbid duplicate key insertions via put method.
* <p>
* This decorator disallows duplicate insertion of key sin the map until removal
* has been done. That is, if method {@link java.util.Map#put(Object, Object)} is called
* with twice in a row with the same key (as stated by
* {@link java.lang.Object#equals(java.lang.Object)} method, an
* {@link java.lang.IllegalStateException} is thrown. Between successive
* invocations of put with the same key, method
* {@link java.util.Map#remove(java.lang.Object)} must be called.
* </p>
* <p>
* Example code with failure :
* <code>
* Map str = new StrictMap(new HashMap());
* str.put("foo","foo");
* str.put("bar","bar");
* // will throw exception
* str.put("foo","bar");
* </code>
* The following code is OK:
* <code>
* Map str = new StrictMap(new HashMap());
* str.put("foo","foo");
* str.remove("foo");
* // OK
* str.put("foo","bar");
* </code>
* <p>
*
* @author nono
* @version $Id: StrictMap.java 259 2006-05-23 10:34:50Z /C=FR/ST=Nord/L=Lille/O=Norsys SA/OU=UE/CN=Arnaud Bailly/emailAddress=abailly@norsys.fr $
*/
public class StrictMap extends AbstractMapDecorator {

  public StrictMap(Map arg0) {
    super(arg0);
  }

  /*
   * (non-Javadoc)
   *
   * @see org.apache.commons.collections.map.AbstractMapDecorator#put(java.lang.Object,
   *      java.lang.Object)
   */
  public Object put(Object arg0, Object arg1) {
    if (getMap().containsKey(arg0))
      throw new IllegalStateException("Duplicate key insertion: " + arg0);
    return super.put(arg0,arg1);
  }

  /*
   * (non-Javadoc)
   *
   * @see org.apache.commons.collections.map.AbstractMapDecorator#putAll(java.util.Map)
   */
  public void putAll(Map arg0) {
    Set keys = getMap().keySet();
    keys.retainAll(arg0.keySet());
    if (!keys.isEmpty())
      throw new IllegalStateException("Duplicate key insertion: " + keys);
    super.putAll(arg0);
  }

}
TOP

Related Classes of speculoos.utils.StrictMap

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.