You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/11/02 20:45:48 UTC

cvs commit: jakarta-commons/collections/src/java/org/apache/commons/collections/pairs TiedMapEntry.java

scolebourne    2003/11/02 11:45:48

  Modified:    collections/src/test/org/apache/commons/collections/pairs
                        TestAll.java
  Added:       collections/src/test/org/apache/commons/collections/pairs
                        TestTiedMapEntry.java
               collections/src/java/org/apache/commons/collections/pairs
                        TiedMapEntry.java
  Log:
  Add TiedMapEntry
  
  Revision  Changes    Path
  1.3       +3 -2      jakarta-commons/collections/src/test/org/apache/commons/collections/pairs/TestAll.java
  
  Index: TestAll.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/pairs/TestAll.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestAll.java	2 Nov 2003 17:06:59 -0000	1.2
  +++ TestAll.java	2 Nov 2003 19:45:48 -0000	1.3
  @@ -85,6 +85,7 @@
           
           suite.addTest(TestDefaultKeyValue.suite());
           suite.addTest(TestDefaultMapEntry.suite());
  +        suite.addTest(TestTiedMapEntry.suite());
           suite.addTest(TestUnmodifiableMapEntry.suite());
           return suite;
       }
  
  
  
  1.1                  jakarta-commons/collections/src/test/org/apache/commons/collections/pairs/TestTiedMapEntry.java
  
  Index: TestTiedMapEntry.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/pairs/TestTiedMapEntry.java,v 1.1 2003/11/02 19:45:48 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.pairs;
  
  import java.util.HashMap;
  import java.util.Map;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * Test the TiedMapEntry class.
   * 
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/02 19:45:48 $
   * 
   * @author Stephen Colebourne
   */
  public class TestTiedMapEntry extends AbstractTestMapEntry {
  
      public TestTiedMapEntry(String testName) {
          super(testName);
  
      }
  
      public static void main(String[] args) {
          junit.textui.TestRunner.run(TestTiedMapEntry.class);
      }
  
      public static Test suite() {
          return new TestSuite(TestTiedMapEntry.class);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Gets the instance to test
       */
      public Map.Entry makeMapEntry(Object key, Object value) {
          Map map = new HashMap();
          map.put(key, value);
          return new TiedMapEntry(map, key);
      }
  
      //-----------------------------------------------------------------------
      /**
       * Tests the constructors.
       */
      public void testConstructors() {
          // ignore
      }
  
      /**
       * Tests the constructors.
       */
      public void testSetValue() {
          Map map = new HashMap();
          map.put("A", "a");
          map.put("B", "b");
          map.put("C", "c");
          Map.Entry entry = new TiedMapEntry(map, "A");
          assertSame("A", entry.getKey());
          assertSame("a", entry.getValue());
          assertSame("a", entry.setValue("x"));
          assertSame("A", entry.getKey());
          assertSame("x", entry.getValue());
          
          entry = new TiedMapEntry(map, "B");
          assertSame("B", entry.getKey());
          assertSame("b", entry.getValue());
          assertSame("b", entry.setValue("y"));
          assertSame("B", entry.getKey());
          assertSame("y", entry.getValue());
          
          entry = new TiedMapEntry(map, "C");
          assertSame("C", entry.getKey());
          assertSame("c", entry.getValue());
          assertSame("c", entry.setValue("z"));
          assertSame("C", entry.getKey());
          assertSame("z", entry.getValue());
      }
  
  }
  
  
  
  1.1                  jakarta-commons/collections/src/java/org/apache/commons/collections/pairs/TiedMapEntry.java
  
  Index: TiedMapEntry.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/pairs/TiedMapEntry.java,v 1.1 2003/11/02 19:45:48 scolebourne Exp $
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001-2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  package org.apache.commons.collections.pairs;
  
  import java.util.Map;
  
  /**
   * A Map Entry tied to a map underneath.
   * <p>
   * This can be used to enable a map entry to make changes on the underlying
   * map, however this will probably mess up any iterators.
   *
   * @since Commons Collections 3.0
   * @version $Revision: 1.1 $ $Date: 2003/11/02 19:45:48 $
   * 
   * @author Stephen Colebourne
   */
  public class TiedMapEntry implements Map.Entry, KeyValue {
  
      /** The map underlying the entry/iterator */    
      private final Map map;
      /** The key */
      private final Object key;
      
      /**
       * Constructs a new entry with the given Map and key.
       *
       * @param map  the map
       * @param key  the key
       */
      public TiedMapEntry(Map map, Object key) {
          super();
          this.map = map;
          this.key = key;
      }
  
      // Map.Entry interface
      //-------------------------------------------------------------------------
      /**
       * Gets the key of this entry
       * 
       * @return the key
       */
      public Object getKey() {
          return key;
      }
  
      /**
       * Gets the value of this entry direct from the map.
       * 
       * @return the value
       */
      public Object getValue() {
          return map.get(key);
      }
  
      /**
       * Sets the value associated with the key direct onto the map.
       * 
       * @param value  the new value
       * @return the old value
       * @throws IllegalArgumentException if the value is set to this map entry
       */
      public Object setValue(Object value) {
          if (value == this) {
              throw new IllegalArgumentException("Cannot set value to this map entry");
          }
          return map.put(key, value);
      }
  
      /**
       * Compares this Map Entry with another Map Entry.
       * <p>
       * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
       * 
       * @param obj  the object to compare to
       * @return true if equal key and value
       */
      public boolean equals(Object obj) {
          if (obj == this) {
              return true;
          }
          if (obj instanceof Map.Entry == false) {
              return false;
          }
          Map.Entry other = (Map.Entry) obj;
          Object value = getValue();
          return
              (key == null ? other.getKey() == null : key.equals(other.getKey())) &&
              (value == null ? other.getValue() == null : value.equals(other.getValue()));
      }
       
      /**
       * Gets a hashCode compatible with the equals method.
       * <p>
       * Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
       * 
       * @return a suitable hashcode
       */
      public int hashCode() {
          Object value = getValue();
          return (getKey() == null ? 0 : getKey().hashCode()) ^
                 (value == null ? 0 : value.hashCode()); 
      }
      
      /**
       * Gets a string version of the entry.
       * 
       * @return entry as a string
       */
      public String toString() {
          return getKey() + "=" + getValue();
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org