Java tutorial
//package com.java2s; /** * Copyright (C) 2014-2017 Philip Helger (www.helger.com) * philip[at]helger[dot]com * * 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. */ import java.util.List; import java.util.function.Predicate; import javax.annotation.Nonnegative; import javax.annotation.Nullable; public class Main { /** * Safe list element accessor method. * * @param aList * The list to extract from. May be <code>null</code>. * @param nIndex * The index to access. Should be ≥ 0. * @return <code>null</code> if the element cannot be accessed. * @param <ELEMENTTYPE> * The type of elements on the list. */ @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getAtIndex(@Nullable final List<? extends ELEMENTTYPE> aList, final int nIndex) { return getAtIndex(aList, nIndex, null); } /** * Safe list element accessor method. * * @param aList * The list to extract from. May be <code>null</code>. * @param nIndex * The index to access. Should be ≥ 0. * @param aDefault * The value to be returned, if the index is out of bounds. * @return The default parameter if the element cannot be accessed. * @param <ELEMENTTYPE> * The type of elements on the list. */ @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getAtIndex(@Nullable final List<? extends ELEMENTTYPE> aList, final int nIndex, @Nullable final ELEMENTTYPE aDefault) { return aList != null && nIndex >= 0 && nIndex < aList.size() ? aList.get(nIndex) : aDefault; } @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getAtIndex(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nonnegative final int nIndex) { return getAtIndex(aCollection, nIndex, null); } @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getAtIndex(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nonnegative final int nIndex, @Nullable final ELEMENTTYPE aDefault) { if (nIndex >= 0) { int nCurIndex = 0; for (final ELEMENTTYPE aElement : aCollection) { if (nCurIndex == nIndex) return aElement; ++nCurIndex; } } return aDefault; } @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getAtIndex(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nullable final Predicate<? super ELEMENTTYPE> aFilter, @Nonnegative final int nIndex) { return getAtIndex(aCollection, aFilter, nIndex, null); } @Nullable public static <ELEMENTTYPE> ELEMENTTYPE getAtIndex(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection, @Nullable final Predicate<? super ELEMENTTYPE> aFilter, @Nonnegative final int nIndex, @Nullable final ELEMENTTYPE aDefault) { if (aFilter == null) return getAtIndex(aCollection, nIndex, aDefault); if (nIndex >= 0) { int nCurIndex = 0; for (final ELEMENTTYPE aElement : aCollection) if (aFilter.test(aElement)) { if (nCurIndex == nIndex) return aElement; ++nCurIndex; } } return aDefault; } }