001/* 002 * (C) Copyright 2020 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Thomas Roger <troger@nuxeo.com> 018 */ 019 020package org.nuxeo.ecm.platform.el; 021 022import java.util.Arrays; 023import java.util.List; 024import java.util.stream.Stream; 025 026/** 027 * Helper functions injected in the {@link ExpressionContext} instance. 028 * 029 * @since 11.2 030 */ 031public class Functions { 032 033 /** 034 * Returns {@code true} if the given {@code arr} contains the given {@code element}. 035 * <p> 036 * Always returns {@code false} if the given {@code arr} is {@code null}. 037 */ 038 public static boolean arrayContains(Object[] arr, Object element) { 039 if (arr == null) { 040 return false; 041 } 042 return Arrays.asList(arr).contains(element); 043 } 044 045 /** 046 * Returns {@code true} if the given {@code arr} contains all the given {@code elements}. 047 * <p> 048 * Always returns {@code false} if the given {@code arr} is {@code null}. 049 */ 050 public static boolean arrayContainsAll(Object[] arr, Object... elements) { 051 if (arr == null || elements == null) { 052 return false; 053 } 054 return Arrays.asList(arr).containsAll(Arrays.asList(elements)); 055 } 056 057 /** 058 * Returns {@code true} if the given {@code arr} contains one of the given {@code elements}. 059 * <p> 060 * Always returns {@code false} if the given {@code arr} is {@code null}. 061 */ 062 public static boolean arrayContainsAny(Object[] arr, Object... elements) { 063 if (arr == null || elements == null) { 064 return false; 065 } 066 List<Object> list = Arrays.asList(arr); 067 return Stream.of(elements).anyMatch(list::contains); 068 } 069 070 /** 071 * Returns {@code true} if the given {@code arr} contains none of the given {@code elements}. 072 * <p> 073 * Always returns {@code false} if the given {@code arr} is {@code null}. 074 */ 075 public static boolean arrayContainsNone(Object[] arr, Object... elements) { 076 if (arr == null || elements == null) { 077 return false; 078 } 079 List<Object> list = Arrays.asList(arr); 080 return Stream.of(elements).noneMatch(list::contains); 081 } 082 083 private Functions() { 084 // helper class 085 } 086}