Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Hibernate OGM, Domain model persistence for NoSQL datastores
   *
   * License: GNU Lesser General Public License (LGPL), version 2.1 or later
   * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
   */
  package org.hibernate.ogm.model.spi;
  
  import static org.hibernate.ogm.model.spi.AssociationOperationType.PUT;
 import static org.hibernate.ogm.model.spi.AssociationOperationType.REMOVE;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
Represents an association (think of it as a set of rows, each representing a specific link).

An association accepts a AssociationSnapshot which is a read-only state of the association when read from the database or freshly created.

An association collects changes applied to it. These changes are represented by a list of AssociationOperation. It is intended that org.hibernate.ogm.dialect.spi.GridDialects retrieve these actions and apply them to the datastore. The list of changes is computed against the snapshot.

Note that the Tuples representing association rows always also contain the columns of their org.hibernate.ogm.model.key.spi.RowKey.

Author(s):
Emmanuel Bernard <emmanuel@hibernate.org>
Gunnar Morling
 
 public class Association {
 	private final AssociationSnapshot snapshot;
 	private boolean cleared;

Creates a new association, based on an empty association snapshot.
 
 	public Association() {
 	}
 
 	public Association(AssociationSnapshot snapshot) {
 		this. = snapshot;
 	}

Returns the association row with the given key.

Parameters:
key the key of the row to return.
Returns:
the association row with the given key or null if no row with that key is contained in this association
 
 	public Tuple get(RowKey key) {
 		AssociationOperation result = .getkey );
 		if ( result == null ) {
 			return  ? null : .getkey );
 		}
 		else if ( result.getType() ==  ) {
 			return null;
 		}
 		return result.getValue();
 	}

Adds the given row to this association, using the given row key. The row must not be null, use the remove(org.hibernate.ogm.model.key.spi.RowKey) operation instead.

Parameters:
key the key to store the row under
value the association row to store
 
 	public void put(RowKey keyTuple value) {
 		// instead of setting it to null, core must use remove
 		Contracts.assertNotNullvalue"association.put value" );
 		.putkeynew AssociationOperationkeyvalue )  );
 	}

Removes the row with the specified key from this association.

Parameters:
key the key of the association row to remove
 
 	public void remove(RowKey key) {
 		.putkeynew AssociationOperationkeynull ) );
 	}

Return the list of actions on the tuple. Operations are inherently deduplicated, i.e. there will be at most one operation for a specific row key.

Note that the global CLEAR operation is put at the top of the list.

Returns:
the operations to execute on the association, the global CLEAR operation is put at the top of the list
		if () {
			result.addnew AssociationOperationnullnull. ) );
		}
		result.addAll.values() );
		return result;
	}

Returns the snapshot upon which this association is based, i.e. its original state when loaded from the datastore or newly created.

Returns:
the snapshot upon which this association is based
		return ;
	}

Whether this association contains no rows.

Returns:
true if this association contains no rows, false otherwise
	public boolean isEmpty() {
		int snapshotSize =  ? 0 : .size();
		//nothing in both
		if ( snapshotSize == 0 && .isEmpty() ) {
			return true;
		}
		//snapshot bigger than changeset
		if ( snapshotSize > .size() ) {
			return false;
		}
		return size() == 0;
	}

Returns the number of rows within this association.

Returns:
the number of rows within this association
	public int size() {
		int size =  ? 0 : .size();
			switch ( op.getValue().getType() ) {
				case :
					if (  || !.containsKeyop.getKey() ) ) {
						size++;
					}
					break;
				case :
					if ( ! && .containsKeyop.getKey() ) ) {
						size--;
					}
					break;
			}
		}
		return size;
	}

Returns all keys of all rows contained within this association.

Returns:
all keys of all rows contained within this association
	public Iterable<RowKeygetKeys() {
		if (  ) {
			return Collections.emptyList();
		}
		else if ( .isEmpty() ) {
		}
		else {
			// It may be a bit too large in case of removals, but that's fine for now
			Set<RowKeykeys = new HashSet<RowKey>( .size() + .size() );
			for ( RowKey rowKey : .getRowKeys() ) {
				keys.addrowKey );
			}
				switch ( op.getValue().getType() ) {
					case :
						keys.addop.getKey() );
						break;
					case :
						keys.removeop.getKey() );
						break;
				}
			}
			return keys;
		}
	}

Removes all rows from this association.
	public void clear() {
		 = true;
	}
	public String toString() {
		StringBuilder sb = new StringBuilder"Association[" ).append( StringHelper.lineSeparator() );
		Iterator<RowKeyrowKeys = getKeys().iterator();
		while ( rowKeys.hasNext() ) {
			RowKey rowKey = rowKeys.next();
			sb.append"  " ).appendrowKey ).append"=" ).appendgetrowKey ) );
			if ( rowKeys.hasNext() ) {
				sb.append"," ).append( StringHelper.lineSeparator() );
			}
		}
		sb.append( StringHelper.lineSeparator() ).append"]" );
		return sb.toString();
	}
New to GrepCode? Check out our FAQ X