001/* 002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others 003 * 004 * This program and the accompanying materials are made available under the 005 * terms of the Eclipse Public License 2.0 which is available at 006 * http://www.eclipse.org/legal/epl-2.0. 007 * 008 * SPDX-License-Identifier: EPL-2.0 009 */ 010 011package org.eclipse.golo.runtime; 012 013import java.util.NoSuchElementException; 014import static java.util.Arrays.copyOfRange; 015 016public final class ArrayHelper { 017 018 private ArrayHelper() { 019 throw new UnsupportedOperationException("Don't instantiate utility classes"); 020 } 021 022 public static Object head(Object[] array) { 023 if (array.length == 0) { 024 return null; 025 } 026 return array[0]; 027 } 028 029 public static Object[] tail(Object[] array) { 030 if (array.length >= 1) { 031 return copyOfRange(array, 1, array.length); 032 } 033 return new Object[0]; 034 } 035 036 public static Object first(Object[] array) { 037 if (array.length == 0) { 038 throw new NoSuchElementException("Empty array"); 039 } 040 return array[0]; 041 } 042 043 public static Object last(Object[] array) { 044 if (array.length == 0) { 045 throw new NoSuchElementException("Empty array"); 046 } 047 return array[array.length - 1]; 048 } 049 050 public static boolean isEmpty(Object[] array) { 051 return array.length == 0; 052 } 053}