All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.envers.internal.tools.Tools Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha3
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.envers.internal.tools;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.envers.tools.Pair;
import org.hibernate.internal.util.compare.EqualsHelper;

/**
 * @author Adam Warski (adam at warski dot org)
 * @author HernпїЅn Chanfreau
 * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com)
 * @author Chris Cranford
 */
public abstract class Tools {
	public static  Map newHashMap() {
		return new HashMap<>();
	}

	public static  Set newHashSet() {
		return new HashSet<>();
	}

	public static  Map newLinkedHashMap() {
		return new LinkedHashMap<>();
	}

	/**
	 * @deprecated (since 5.2), use {@link EqualsHelper#areEqual(Object, Object)}.
	 */
	@Deprecated
	public static boolean objectsEqual(Object obj1, Object obj2) {
		// HHH-10734
		// Delegates to core's EqualsHelper to support array and non-array types
		return EqualsHelper.areEqual( obj1, obj2 );
	}

	public static  List iteratorToList(Iterator iter) {
		final List ret = new ArrayList<>();
		while ( iter.hasNext() ) {
			ret.add( iter.next() );
		}

		return ret;
	}

	public static  List collectionToList(Collection collection) {
		if ( collection instanceof List ) {
			return (List) collection;
		}
		else {
			List list = new ArrayList<>();
			list.addAll( collection );
			return list;
		}
	}

	public static boolean iteratorsContentEqual(Iterator iter1, Iterator iter2) {
		while ( iter1.hasNext() && iter2.hasNext() ) {
			if ( !iter1.next().equals( iter2.next() ) ) {
				return false;
			}
		}

		//noinspection RedundantIfStatement
		if ( iter1.hasNext() || iter2.hasNext() ) {
			return false;
		}

		return true;
	}

	/**
	 * Transforms a list of arbitrary elements to a list of index-element pairs.
	 *
	 * @param list List to transform.
	 *
	 * @return A list of pairs: ((0, element_at_index_0), (1, element_at_index_1), ...)
	 */
	public static  List> listToIndexElementPairList(List list) {
		final List> ret = new ArrayList<>();
		final Iterator listIter = list.iterator();
		for ( int i = 0; i < list.size(); i++ ) {
			ret.add( Pair.make( i, listIter.next() ) );
		}

		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy