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

org.conqat.lib.commons.collections.ImmutablePair Maven / Gradle / Ivy

There is a newer version: 2024.7.2
Show newest version
/*
 * Copyright (c) CQSE GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.conqat.lib.commons.collections;

import java.io.Serializable;

import org.conqat.lib.commons.test.IndexValueClass;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Simple readonly pair class.
 */
@IndexValueClass
public class ImmutablePair implements Cloneable, Comparable>, Serializable {

	/** Version used for serialization. */
	private static final long serialVersionUID = 1;

	protected static final String FIRST_PROPERTY = "first";

	protected static final String SECOND_PROPERTY = "second";

	/** The first element. */
	@JsonProperty(FIRST_PROPERTY)
	protected S first;

	/** The second element. */
	@JsonProperty(SECOND_PROPERTY)
	protected T second;

	/** Constructor. */
	@JsonCreator
	public ImmutablePair(@JsonProperty(FIRST_PROPERTY) S first, @JsonProperty(SECOND_PROPERTY) T second) {
		this.first = first;
		this.second = second;
	}

	/** Copy constructor. */
	public ImmutablePair(ImmutablePair p) {
		this.first = p.first;
		this.second = p.second;
	}

	/** Returns the first element of the pair. */
	public S getFirst() {
		return first;
	}

	/** Returns the second element of the pair. */
	public T getSecond() {
		return second;
	}

	/** {@inheritDoc} */
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof ImmutablePair)) {
			return false;
		}
		ImmutablePair p = (ImmutablePair) obj;
		return areEqual(first, p.first) && areEqual(second, p.second);
	}

	/** Returns true if either both are null or they are equal. */
	private static boolean areEqual(Object o1, Object o2) {
		if (o1 == null) {
			return o2 == null;
		}
		return o1.equals(o2);
	}

	/**
	 * {@inheritDoc}
	 * 

* The hash code is based on the hash code of the first and second members. */ @Override public int hashCode() { int firstCode = 1; if (first != null) { firstCode = first.hashCode(); } int secondCode = 1; if (second != null) { secondCode = second.hashCode(); } return firstCode + 1013 * secondCode; } /** {@inheritDoc} */ @Override public String toString() { return "(" + first + "," + second + ")"; } /** {@inheritDoc} */ @Override protected ImmutablePair clone() { return new ImmutablePair<>(this); } /** * {@inheritDoc} *

* Compare based on first element. Use second element only if first elements are equal. Null entries * are sorted to the top. */ @Override public int compareTo(ImmutablePair pair) { int cmp = objCompare(first, pair.first); if (cmp != 0) { return cmp; } return objCompare(second, pair.second); } /** * Performs comparison on two arbitrary objects of the same type. */ @SuppressWarnings("unchecked") private static int objCompare(O o1, O o2) { if (o1 == null) { if (o2 == null) { return 0; } return -1; } else if (o2 == null) { return 1; } if ((o1 instanceof Comparable) && (o2 instanceof Comparable)) { try { return ((Comparable) o1).compareTo(o2); } catch (ClassCastException e) { // somehow failed, so continue and treat as if not comparable } } // compare using hash code, so we get at least an approximation of an // ordering int h1 = o1.hashCode(); int h2 = o2.hashCode(); if (h1 == h2) { return 0; } if (h1 < h2) { return -1; } return 1; } }