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

sirius.kernel.commons.ComparableTuple Maven / Gradle / Ivy

Go to download

Provides common core classes and the microkernel powering all Sirius applications

There is a newer version: 12.9.1
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.kernel.commons;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Provides a tuple of values where the key is used as comparator.
 * 

* Subclasses {@link Tuple} to implement Comparable based on the key, that is the first element of the tuple. * * @param defines the first type of the tuple. The supplied class must implement {@code Comparable} * @param defines the second type of the tuple * @see Tuple * @see Comparable */ public class ComparableTuple, S> extends Tuple implements Comparable> { protected ComparableTuple(F first, S second) { super(first, second); } /** * Creates a new tuple without any values. * * @param the type for the first value * @param the type for the second value * @return the newly created tuple */ @Nonnull public static , S> ComparableTuple createTuple() { return new ComparableTuple<>(null, null); } /** * Creates a new tuple by only specifying the first value of the tuple. *

* The second value will remain null. * * @param first defines the first value of the tuple * @param the type for the first value * @param the type for the second value * @return the newly created tuple */ @Nonnull public static , S> ComparableTuple create(@Nullable F first) { return new ComparableTuple<>(first, null); } /** * Creates a new tuple with the given values. * * @param first defines the first value of the tuple * @param second defines the second value of the tuple * @param the type for the first value * @param the type for the second value * @return the newly created tuple */ @Nonnull public static , S> ComparableTuple create(@Nullable F first, @Nullable S second) { return new ComparableTuple<>(first, second); } /** * Converts a map into a list of tuples. * * @param map the map to be converted * @param the key type of the map and therefore the type of the first component of the tuples * @param the value type of the map and therefore the type of the second component of the tuples * @return a list of tuples, containing one tuple per map entry where the first component is the key, * and the second component is the value of the map entry. */ public static , V> List> fromComparableMap(@Nonnull Map map) { List> result = new ArrayList<>(map.size()); for (Map.Entry e : map.entrySet()) { result.add(new ComparableTuple(e.getKey(), e.getValue())); } return result; } @Override public int compareTo(ComparableTuple o) { if (o == null) { return 1; } if (o.getFirst() == null) { if (getFirst() != null) { return 1; } else { return 0; } } if (getFirst() == null) { return -1; } return getFirst().compareTo(o.getFirst()); } @SuppressWarnings("unchecked") @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (obj == this) { return true; } if (!(obj instanceof ComparableTuple)) { return false; } return compareTo((ComparableTuple) obj) == 0; } @Override public int hashCode() { if (getFirst() == null) { return 0; } return getFirst().hashCode(); } }