Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,64 @@

public class Pair {
// TODO напишите реализацию
}
import java.util.function.BiConsumer;

public class Pair<T, U>
{
private final T first;
private final U second;

private Pair(T first, U second)
{
this.first = first;
this.second = second;
}

public U getSecond()
{
//тест падает на null
//throw new NoSuchElementException("No second value present");
return second;
}

public T getFirst()
{
//тест падает на null
//throw new NoSuchElementException("No first value present");
return first;
}

public void ifPresent(BiConsumer<? super T, ? super U> consumer)
{
if (first != null && second != null) {
consumer.accept(first, second);
}
}

@Override
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
if (obj == null || obj.getClass() != this.getClass())
{
return false;
}
Pair tmp = (Pair) obj;
return (first == tmp.getFirst() || (first != null && tmp.getSecond().equals(tmp.getFirst())))
&& (second == tmp.getSecond() || (second != null && second.equals(tmp.getSecond())));
}

@Override
public int hashCode()
{
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
}

public static <T, U> Pair<T, U> of(T first, U second)
{
return new Pair<>(first, second);
}
}