You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Roger R Andrews (JIRA)" <ji...@apache.org> on 2015/11/05 16:27:27 UTC

[jira] [Commented] (COLLECTIONS-579) PassiveExpiringMap doesn't work when the key is a byte array

    [ https://issues.apache.org/jira/browse/COLLECTIONS-579?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14991809#comment-14991809 ] 

Roger R Andrews commented on COLLECTIONS-579:
---------------------------------------------

Yes, thank you for pointing this out. I don't know how it's never bitten me before. Sorry for the trouble. Here is the code I should have used first:

{code:title=ByteArray.java|borderStyle=solid}
public class ByteArray {
	private volatile int hashCode = 0;
	private byte[] bytes = null;
	private final Object lock = new Object();

	public ByteArray(byte[] bytes) {
		 this.bytes = bytes;
 	}

        /**
       * Redefine the backing array
       * @param bytes original array
       */
       public void set(byte[] bytes) {
	 synchronized (lock) {
  	 this.bytes = bytes;
	 hashCode = 0;
       }
	}
	
	/**
	 * @return original backing array
	 */
	public byte[] get(){
		return bytes;
	}

	@Override
	public int hashCode() {
   		synchronized (lock) {
			if (hashCode == 0) {
				int hash = 11;
				hashCode = 29 * hash + Arrays.hashCode(this.bytes);
			}
		}
		return hashCode;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		final ByteArray other = (ByteArray) obj;
		if (!Arrays.equals(this.bytes, other.bytes)) {
			return false;
		}
		return true;
	}
}
{code}

> PassiveExpiringMap doesn't work when the key is a byte array
> ------------------------------------------------------------
>
>                 Key: COLLECTIONS-579
>                 URL: https://issues.apache.org/jira/browse/COLLECTIONS-579
>             Project: Commons Collections
>          Issue Type: Bug
>          Components: Map
>    Affects Versions: 4.0
>         Environment: GNU/Linux Ubuntu 15.10 64 bits, OpenJDK 1.8.0_65
>            Reporter: Roger R Andrews
>
> When you put a (key,value) pair in a PassiveExpiringMap and the key is byte[] you can't retrieve it.
> Code to reproduce the problem:
> byte[] key = {0,0,0,1};
> PassiveExpiringMap<byte[],byte[]> map = new PassiveExpiringMap<byte[],byte[]> ();
> map.put(key,key);
> byte[] queryKey = {0,0,0,1};
> //this should be true
> map.containsKey(queryKey) == false



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)