Skip to content

Commit

Permalink
Add default overrides for getFirst and getLast in MutableList and Mut…
Browse files Browse the repository at this point in the history
…ableSortedSet to Fixes #1460.

Signed-off-by: Donald Raab <Donald.Raab@bnymellon.com>
  • Loading branch information
donraab committed Jun 13, 2023
1 parent 52076bb commit afe2a98
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Goldman Sachs and others.
* Copyright (c) 2023 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -60,6 +60,26 @@
public interface MutableList<T>
extends MutableCollection<T>, List<T>, Cloneable, ListIterable<T>
{
/**
* This default override exists because java.util.List added a default getFirst() method in Java 21.
* @since 12.0
*/
@Override
default T getFirst()
{
return this.isEmpty() ? null : this.get(0);
}

/**
* This default override exists because java.util.List added a default getLast() method in Java 21.
* @since 12.0
*/
@Override
default T getLast()
{
return this.isEmpty() ? null : this.get(this.size() - 1);
}

@Override
default MutableList<T> with(T element)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Goldman Sachs and others.
* Copyright (c) 2023 Goldman Sachs and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand Down Expand Up @@ -50,6 +50,28 @@
public interface MutableSortedSet<T>
extends MutableSetIterable<T>, SortedSetIterable<T>, SortedSet<T>, Cloneable
{
/**
* This default override exists because java.util.SortedSet added a default getFirst() method in Java 21.
*
* @since 12.0
*/
@Override
default T getFirst()
{
return this.first();
}

/**
* This default override exists because java.util.SortedSet added a default getLast() method in Java 21.
*
* @since 12.0
*/
@Override
default T getLast()
{
return this.last();
}

@Override
default MutableSortedSet<T> with(T element)
{
Expand Down Expand Up @@ -160,7 +182,9 @@ default <V> MutableList<V> collectWithIndex(ObjectIntToObjectFunction<? super T,
* @since 9.2
*/
@Override
default <P, V> MutableList<V> flatCollectWith(Function2<? super T, ? super P, ? extends Iterable<V>> function, P parameter)
default <P, V> MutableList<V> flatCollectWith(
Function2<? super T, ? super P, ? extends Iterable<V>> function,
P parameter)
{
return this.flatCollect(each -> function.apply(each, parameter));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Two Sigma.
* Copyright (c) 2023 Two Sigma and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
Expand All @@ -21,16 +21,27 @@ public class UnmodifiableMutableOrderedMapSerializationTest
@Test
public void serializedForm()
{
Verify.assertSerializedForm(
1L,
"rO0ABXNyAE5vcmcuZWNsaXBzZS5jb2xsZWN0aW9ucy5pbXBsLm1hcC5vcmRlcmVkLm11dGFibGUu\n"
+ "VW5tb2RpZmlhYmxlTXV0YWJsZU9yZGVyZWRNYXAAAAAAAAAAAQIAAUwACGRlbGVnYXRldAAzTG9y\n"
+ "Zy9lY2xpcHNlL2NvbGxlY3Rpb25zL2FwaS9tYXAvTXV0YWJsZU9yZGVyZWRNYXA7eHBzcgBCb3Jn\n"
+ "LmVjbGlwc2UuY29sbGVjdGlvbnMuaW1wbC5tYXAub3JkZXJlZC5tdXRhYmxlLk9yZGVyZWRNYXBB\n"
+ "ZGFwdGVyAAAAAAAAAAECAAFMAAhkZWxlZ2F0ZXQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAF2phdmEu\n"
+ "dXRpbC5MaW5rZWRIYXNoTWFwNMBOXBBswPsCAAFaAAthY2Nlc3NPcmRlcnhyABFqYXZhLnV0aWwu\n"
+ "SGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAB3CAAA\n"
+ "ABAAAAAAeAA=",
UnmodifiableMutableOrderedMap.of(OrderedMaps.adapt(new LinkedHashMap<>())));
if (this.isBeforeJava21())
{
Verify.assertSerializedForm(
1L,
"rO0ABXNyAE5vcmcuZWNsaXBzZS5jb2xsZWN0aW9ucy5pbXBsLm1hcC5vcmRlcmVkLm11dGFibGUu\n"
+ "VW5tb2RpZmlhYmxlTXV0YWJsZU9yZGVyZWRNYXAAAAAAAAAAAQIAAUwACGRlbGVnYXRldAAzTG9y\n"
+ "Zy9lY2xpcHNlL2NvbGxlY3Rpb25zL2FwaS9tYXAvTXV0YWJsZU9yZGVyZWRNYXA7eHBzcgBCb3Jn\n"
+ "LmVjbGlwc2UuY29sbGVjdGlvbnMuaW1wbC5tYXAub3JkZXJlZC5tdXRhYmxlLk9yZGVyZWRNYXBB\n"
+ "ZGFwdGVyAAAAAAAAAAECAAFMAAhkZWxlZ2F0ZXQAD0xqYXZhL3V0aWwvTWFwO3hwc3IAF2phdmEu\n"
+ "dXRpbC5MaW5rZWRIYXNoTWFwNMBOXBBswPsCAAFaAAthY2Nlc3NPcmRlcnhyABFqYXZhLnV0aWwu\n"
+ "SGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAB3CAAA\n"
+ "ABAAAAAAeAA=",
UnmodifiableMutableOrderedMap.of(OrderedMaps.adapt(new LinkedHashMap<>())));
}
}

private boolean isBeforeJava21()
{
String javaVersion = System.getProperty("java.version").replace("-ea", "");
String[] javaVersionParts = javaVersion.split("\\.");
int version = Integer.parseInt(javaVersionParts[0]);
return version < 21;
}
}

0 comments on commit afe2a98

Please sign in to comment.