Skip to content

Commit

Permalink
Java 21 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
phax committed Mar 26, 2024
1 parent 38c36b4 commit eb661f9
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 57 deletions.
8 changes: 5 additions & 3 deletions ph-cli/src/main/java/com/helger/cli/ParsedCmdLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ public boolean hasOption (@Nullable final String sOption)
public String getValue (@Nonnull final IOptionBase aOption)
{
final ICommonsList <String> aValues = _find (aOption);
return aValues == null ? null : aValues.getFirst ();
return aValues == null ? null : aValues.getFirstOrNull ();
}

@Nullable
public String getValue (@Nullable final String sOption)
{
final ICommonsList <String> aValues = _find (sOption);
return aValues == null ? null : aValues.getFirst ();
return aValues == null ? null : aValues.getFirstOrNull ();
}

@Nullable
Expand Down Expand Up @@ -142,6 +142,8 @@ public ICommonsList <String> unknownTokens ()
@Override
public String toString ()
{
return new ToStringGenerator (null).append ("Params", m_aParams).append ("UnknownTokens", m_aUnknownTokens).getToString ();
return new ToStringGenerator (null).append ("Params", m_aParams)
.append ("UnknownTokens", m_aUnknownTokens)
.getToString ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public ELEMENTTYPE pop ()
{
if (isEmpty ())
throw new EmptyStackException ();
return removeLast ();
return removeLastOrNull ();
}

/**
Expand All @@ -109,7 +109,7 @@ public ELEMENTTYPE peek ()
{
if (isEmpty ())
throw new EmptyStackException ();
return getLast ();
return getLastOrNull ();
}

/**
Expand Down Expand Up @@ -138,7 +138,7 @@ public ELEMENTTYPE firstElement ()
{
if (isEmpty ())
throw new EmptyStackException ();
return getFirst ();
return getFirstOrNull ();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,24 +149,42 @@ default <DSTTYPE extends ELEMENTTYPE> ICommonsList <DSTTYPE> getAllInstanceOf (@
}

/**
* Note: this methods conflicts with a method added in Java SE 21 in class
* ArrayList. Therefore {@link #getFirstOrNull()} should be used instead.
*
* @return The first element of the list or <code>null</code> if the list is
* empty.
* @see #getFirst(Object)
* @see #findFirst(Predicate)
* @deprecated Use {@link #getFirstOrNull()} instead
*/
@Nullable
@Deprecated (forRemoval = true, since = "11.1.5")
default ELEMENTTYPE getFirst ()
{
return getFirst (null);
}

/**
* @return The first element of the list or <code>null</code> if the list is
* empty.
* @see #getFirst(Object)
* @see #findFirst(Predicate)
* @since 11.1.5
*/
@Nullable
default ELEMENTTYPE getFirstOrNull ()
{
return getFirst (null);
}

/**
* @param aDefault
* The default value to be returned if this list is empty. May be
* <code>null</code>.
* @return The first element of the list or the provided default value if the
* list is empty.
* @see #getFirst()
* @see #getFirstOrNull()
* @see #findFirst(Predicate)
*/
@Nullable
Expand All @@ -176,23 +194,40 @@ default ELEMENTTYPE getFirst (@Nullable final ELEMENTTYPE aDefault)
}

/**
* Note: this methods conflicts with a method added in Java SE 21 in class
* ArrayList. Therefore {@link #getLastOrNull()} should be used instead.
*
* @return The last element of the list or <code>null</code> if the list is
* empty.
* @see #getLast(Object)
* @deprecated Use {@link #getLastOrNull()} instead
*/
@Nullable
@Deprecated (forRemoval = true, since = "11.1.5")
default ELEMENTTYPE getLast ()
{
return getLast (null);
}

/**
* @return The last element of the list or <code>null</code> if the list is
* empty.
* @see #getLast(Object)
* @since 11.1.5
*/
@Nullable
default ELEMENTTYPE getLastOrNull ()
{
return getLast (null);
}

/**
* @param aDefault
* The default value to be returned if this list is empty. May be
* <code>null</code>.
* @return The last element of the list or <code>null</code> if the list is
* empty.
* @see #getLast()
* @see #getLastOrNull()
*/
@Nullable
default ELEMENTTYPE getLast (@Nullable final ELEMENTTYPE aDefault)
Expand Down Expand Up @@ -277,25 +312,58 @@ default ELEMENTTYPE removeAndReturnElementAtIndex (final int nIndex)
}

/**
* Remove the first element of the list.
* Remove the first element of the list.<br>
* Note: this methods conflicts with a method added in Java SE 21 in class
* ArrayList. Therefore {@link #removeFirstOrNull()} should be used instead.
*
* @return <code>null</code> if the list is empty or the previously contained
* element at index 0.
* @deprecated Use {@link #removeFirstOrNull()} instead
*/
@Nullable
@Deprecated (forRemoval = true, since = "11.1.5")
default ELEMENTTYPE removeFirst ()
{
return removeFirstOrNull ();
}

/**
* Remove the first element of the list.
*
* @return <code>null</code> if the list is empty or the previously contained
* element at index 0.
* @since 11.1.5
*/
@Nullable
default ELEMENTTYPE removeFirstOrNull ()
{
return isEmpty () ? null : remove (0);
}

/**
* Remove the last element of the list.
* Remove the last element of the list.<br>
* Note: this methods conflicts with a method added in Java SE 21 in class
* ArrayList. Therefore {@link #removeLastOrNull()} should be used instead.
*
* @return <code>null</code> if the list is empty or the previously contained
* element at index <code>size()-1</code>.
*/
@Nullable
@Deprecated (forRemoval = true, since = "11.1.5")
default ELEMENTTYPE removeLast ()
{
return removeLastOrNull ();
}

/**
* Remove the last element of the list.
*
* @return <code>null</code> if the list is empty or the previously contained
* element at index <code>size()-1</code>.
* @since 11.1.5
*/
@Nullable
default ELEMENTTYPE removeLastOrNull ()
{
final int nSize = size ();
return nSize == 0 ? null : remove (nSize - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public String getFirstHeaderValue (@Nullable final String sName)
{
final ICommonsList <String> aValues = _getHeaderListCaseInsensitive (sName);
if (aValues != null)
return aValues.getFirst ();
return aValues.getFirstOrNull ();
}
return null;
}
Expand Down Expand Up @@ -651,7 +651,8 @@ public boolean isEmpty ()
* @see #getUnifiedValue(String,boolean)
* @since 9.3.6
*/
public void forEachSingleHeader (@Nonnull final BiConsumer <? super String, ? super String> aConsumer, final boolean bUnifyValue)
public void forEachSingleHeader (@Nonnull final BiConsumer <? super String, ? super String> aConsumer,
final boolean bUnifyValue)
{
forEachSingleHeader (aConsumer, bUnifyValue, DEFAULT_QUOTE_IF_NECESSARY);
}
Expand Down Expand Up @@ -726,7 +727,9 @@ public void forEachHeaderLine (@Nonnull final Consumer <? super String> aConsume
final String sKey = aEntry.getKey ();
for (final String sValue : aEntry.getValue ())
{
final String sHeaderLine = sKey + SEPARATOR_KEY_VALUE + (bUnifyValue ? getUnifiedValue (sValue, bQuoteIfNecessary) : sValue);
final String sHeaderLine = sKey +
SEPARATOR_KEY_VALUE +
(bUnifyValue ? getUnifiedValue (sValue, bQuoteIfNecessary) : sValue);
aConsumer.accept (sHeaderLine);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public final File next ()
throw new NoSuchElementException ();

// Get and remove the first element
final File aFile = m_aFilesLeft.removeFirst ();
final File aFile = m_aFilesLeft.removeFirstOrNull ();

m_nLevel = _getLevel (aFile) - m_nStartLevel;
if (aFile.isDirectory ())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public ClassList (@Nonnull final Class <?> aClass)
aOpenSrc.add (aClass);
while (!aOpenSrc.isEmpty ())
{
final Class <?> aCurClass = aOpenSrc.removeFirst ();
final Class <?> aCurClass = aOpenSrc.removeFirstOrNull ();
aUniqueOrderedClasses.add (aCurClass);

// Add super-classes and interfaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static <T> ICommonsList <T> getAllSPIImplementations (@Nonnull final Clas
final ServiceLoader <T> aServiceLoader = ServiceLoader.<T> load (aSPIClass, aClassLoader);
final ICommonsList <T> ret = new CommonsArrayList <> ();

for (T aInstance : aServiceLoader)
for (final T aInstance : aServiceLoader)
{
try
{
Expand Down Expand Up @@ -259,6 +259,6 @@ public static <T> T getFirstSPIImplementation (@Nonnull final Class <T> aSPIClas
" - using the first one. Details: " +
aAll);
}
return aAll.getFirst ();
return aAll.getFirstOrNull ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static ICommonsList <String> splitByVariables (@Nonnull @Nonempty final String s
if ((ret.size () % 2) == 1)
{
// Append to last text block
ret.setLast (ret.getLast () + sRestToAdd);
ret.setLast (ret.getLastOrNull () + sRestToAdd);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public void testGetFirstAndLast ()
{
final ICommonsList <String> aTest = new CommonsArrayList <> ();
assertTrue (aTest.isEmpty ());
assertNull (aTest.getFirst ());
assertNull (aTest.getLast ());
assertNull (aTest.removeFirst ());
assertNull (aTest.removeLast ());
assertNull (aTest.getFirstOrNull ());
assertNull (aTest.getLastOrNull ());
assertNull (aTest.removeFirstOrNull ());
assertNull (aTest.removeLastOrNull ());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ public void testSimpleValue ()
assertEquals (1, h.size ());
assertNotNull (h.getAllHeaderLines (true));
assertEquals (1, h.getAllHeaderLines (true).size ());
assertEquals ("key1: val1", h.getAllHeaderLines (true).getFirst ());
assertEquals ("key1: val1", h.getAllHeaderLines (true).getFirstOrNull ());
assertNotNull (h.getAllHeaderLines (false));
assertEquals (1, h.getAllHeaderLines (false).size ());
assertEquals ("key1: val1", h.getAllHeaderLines (false).getFirst ());
assertEquals ("key1: val1", h.getAllHeaderLines (false).getFirstOrNull ());
assertNotNull (h.getAllHeaderNames ());
assertEquals (1, h.getAllHeaderNames ().size ());
assertEquals ("key1", h.getAllHeaderNames ().getFirst ());
Expand All @@ -76,7 +76,7 @@ public void testSimpleValue ()
assertEquals ("key1", h.getAllHeaders ().getFirstKey ());
assertNotNull (h.getAllHeaderValues ("key1"));
assertEquals (1, h.getAllHeaderValues ("key1").size ());
assertEquals ("val1", h.getAllHeaderValues ("key1").getFirst ());
assertEquals ("val1", h.getAllHeaderValues ("key1").getFirstOrNull ());

CommonsTestHelper.testGetClone (h);
}
Expand All @@ -93,12 +93,12 @@ public void testMultipleSimpleValues ()
assertEquals (3, h.size ());
assertNotNull (h.getAllHeaderLines (true));
assertEquals (3, h.getAllHeaderLines (true).size ());
assertEquals ("key1: val1", h.getAllHeaderLines (true).getFirst ());
assertEquals ("key1: val1", h.getAllHeaderLines (true).getFirstOrNull ());
assertEquals ("key2: val2", h.getAllHeaderLines (true).get (1));
assertEquals ("key3: val3", h.getAllHeaderLines (true).get (2));
assertNotNull (h.getAllHeaderLines (false));
assertEquals (3, h.getAllHeaderLines (false).size ());
assertEquals ("key1: val1", h.getAllHeaderLines (false).getFirst ());
assertEquals ("key1: val1", h.getAllHeaderLines (false).getFirstOrNull ());
assertEquals ("key2: val2", h.getAllHeaderLines (false).get (1));
assertEquals ("key3: val3", h.getAllHeaderLines (false).get (2));
assertNotNull (h.getAllHeaderNames ());
Expand All @@ -114,17 +114,17 @@ public void testMultipleSimpleValues ()

assertNotNull (h.getAllHeaderValues ("key1"));
assertEquals (1, h.getAllHeaderValues ("key1").size ());
assertEquals ("val1", h.getAllHeaderValues ("key1").getFirst ());
assertEquals ("val1", h.getAllHeaderValues ("key1").getFirstOrNull ());
assertNotNull (h.getAllHeaderValues ("key2"));
assertEquals (1, h.getAllHeaderValues ("key2").size ());
assertEquals ("val2", h.getAllHeaderValues ("key2").getFirst ());
assertEquals ("val2", h.getAllHeaderValues ("key2").getFirstOrNull ());
assertNotNull (h.getAllHeaderValues ("key3"));
assertEquals (1, h.getAllHeaderValues ("key3").size ());
assertEquals ("val3", h.getAllHeaderValues ("key3").getFirst ());
assertEquals ("val3", h.getAllHeaderValues ("key3").getFirstOrNull ());

assertNotNull (h.getAllHeaderValues ("KEY1"));
assertEquals (1, h.getAllHeaderValues ("kEy1").size ());
assertEquals ("val1", h.getAllHeaderValues ("keY1").getFirst ());
assertEquals ("val1", h.getAllHeaderValues ("keY1").getFirstOrNull ());

CommonsTestHelper.testGetClone (h);
}
Expand All @@ -141,12 +141,12 @@ public void testMultiValue ()
assertEquals (1, h.size ());
assertNotNull (h.getAllHeaderLines (true));
assertEquals (3, h.getAllHeaderLines (true).size ());
assertEquals ("key1: val1", h.getAllHeaderLines (true).getFirst ());
assertEquals ("key1: val1", h.getAllHeaderLines (true).getFirstOrNull ());
assertEquals ("key1: val2", h.getAllHeaderLines (true).get (1));
assertEquals ("key1: val3", h.getAllHeaderLines (true).get (2));
assertNotNull (h.getAllHeaderLines (false));
assertEquals (3, h.getAllHeaderLines (false).size ());
assertEquals ("key1: val1", h.getAllHeaderLines (false).getFirst ());
assertEquals ("key1: val1", h.getAllHeaderLines (false).getFirstOrNull ());
assertEquals ("key1: val2", h.getAllHeaderLines (false).get (1));
assertEquals ("key1: val3", h.getAllHeaderLines (false).get (2));
assertNotNull (h.getAllHeaderNames ());
Expand All @@ -157,7 +157,7 @@ public void testMultiValue ()
assertEquals ("key1", h.getAllHeaders ().getFirstKey ());
assertNotNull (h.getAllHeaderValues ("key1"));
assertEquals (3, h.getAllHeaderValues ("key1").size ());
assertEquals ("val1", h.getAllHeaderValues ("key1").getFirst ());
assertEquals ("val1", h.getAllHeaderValues ("key1").getFirstOrNull ());
assertEquals ("val2", h.getAllHeaderValues ("key1").get (1));
assertEquals ("val3", h.getAllHeaderValues ("key1").get (2));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public static String getPeriodText (final int nYears,
aSB.append (aTextProvider.getComma ());
aSB.append (aParts.get (i));
}
return aSB.append (aTextProvider.getAnd ()).append (aParts.getLast ()).toString ();
return aSB.append (aTextProvider.getAnd ()).append (aParts.getLastOrNull ()).toString ();
}

@Nonnull
Expand Down
Loading

0 comments on commit eb661f9

Please sign in to comment.