/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * JBoss, Home of Professional Open Source
 * Copyright 2010-2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @authors tag. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * 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,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package org.hibernate.ogm.grid;

import java.io.Serializable;
import java.util.Arrays;

/**
 * Represents the key used to link a property value and the id of it's owning entity
 *
 * @author Emmanuel Bernard
 */
public class AssociationKey implements Serializable {
	private final String table;
	private final String[] columns;
	//column value types do have to be serializable so AssociationKey is serializable
	//should it be a Serializable[] type? It seems to be more pain than anything else
	private final Object[] columnValues;

	public AssociationKey(String table, String[] columns, Object[] columnValues) {
		this.table = table;
		this.columns = columns;
		this.columnValues = columnValues;
	}

	@Override
	public boolean equals(Object o) {
		if ( this == o ) {
			return true;
		}
		if ( o == null || getClass() != o.getClass() ) {
			return false;
		}

		AssociationKey that = ( AssociationKey ) o;

		// Probably incorrect - comparing Object[] arrays with Arrays.equals
		if ( !Arrays.equals( columnValues, that.columnValues ) ) {
			return false;
		}
		if ( !Arrays.equals( columns, that.columns ) ) {
			return false;
		}
		if ( !table.equals( that.table ) ) {
			return false;
		}

		return true;
	}

	@Override
	public int hashCode() {
		int result = table.hashCode();
		result = 31 * result + Arrays.hashCode( columns );
		result = 31 * result + Arrays.hashCode( columnValues );
		return result;
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();
		sb.append( "AssociationKey" );
		sb.append( "{table='" ).append( table ).append( '\'' );
		sb.append( ", columns=" ).append( columns == null ? "null" : Arrays.asList( columns ).toString() );
		sb.append( ", columnValues=" )
				.append( columnValues == null ? "null" : Arrays.asList( columnValues ).toString() );
		sb.append( '}' );
		return sb.toString();
	}
}

@@ -1110,16 +1110,75 @@
 rrays;%0A%0A
+import org.hibernate.annotations.common.AssertionFailure;%0A%0A
 /**%0A * R
@@ -1402,24 +1402,28 @@
 ing%5B%5D column
+Name
 s;%0A%09//column
@@ -1670,16 +1670,20 @@
 %5D column
+Name
 s, Objec
@@ -1702,16 +1702,149 @@
 lues) %7B%0A
+%09%09if ( columnNames.length != columnValues.length ) %7B%0A%09%09%09throw new AssertionFailure( %22Column names do not match column values%22 );%0A%09%09%7D%0A
 %09%09this.t
@@ -1870,16 +1870,20 @@
 s.column
+Name
 s = colu
@@ -1884,16 +1884,20 @@
 = column
+Name
 s;%0A%09%09thi
@@ -2320,16 +2320,20 @@
 ( column
+Name
 s, that.
@@ -2338,16 +2338,20 @@
 t.column
+Name
 s ) ) %7B%0A
@@ -2568,16 +2568,20 @@
 ( column
+Name
 s );%0A%09%09r
@@ -2830,240 +2830,195 @@
 nd( 
-'%5C''
+%22'%22
  );%0A%09%09
-sb.append( %22, columns=%22 ).append( columns == null ? %22null%22 : Arrays.asList( columns ).toString() );%0A%09%09sb.append( %22, columnValues=%22 )%0A%09%09%09%09.append( columnValues == null ? %22null%22 : Arrays.asList( columnValues ).toString()
+for( int index = 0 ; index %3C columnNames.length ; index++) %7B%0A%09%09%09sb.append(%22%5Cn%5Ct%22).append( columnNames%5Bindex%5D ).append( %22 = '%22 ).append( columnValues%5Bindex%5D ).append( %22'%22
  );%0A
+%09%09%7D%0A%0A
 %09%09sb
@@ -3030,11 +3030,13 @@
 nd( 
-'%7D'
+%22%5Cn%7D%22
  );%0A

Improve variable names and toString on AssociationKey