001/* 002 * Copyright 2024 Anyware Services 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 */ 016package org.ametys.core; 017 018import java.util.Set; 019 020import org.apache.avalon.framework.component.Component; 021 022/** 023 * Helper for contexts. 024 */ 025public class AmetysContextHelper implements Component 026{ 027 /** The Avalon Role */ 028 public static final String ROLE = AmetysContextHelper.class.getName(); 029 030 /** 031 * Check if the two sets are acquainted with each other: at least one context in both sets 032 * @param contextsSet1 The first set of context 033 * @param contextsSet2 The second set of context 034 * @return <code>true</code> if the two sets are acquainted, false otherwise 035 */ 036 public boolean areRelated(Set<String> contextsSet1, Set<String> contextsSet2) 037 { 038 // The two sets are acquainted if at least one context is in both sets 039 return contextsSet1.stream().anyMatch(context -> contextsSet2.contains(context)); 040 } 041 042 /** 043 * Check if a set of contexts is acquainted with the given context 044 * @param setOfContexts The set of contexts 045 * @param contextToCheck The context to check 046 * @return <code>true</code> if the set is acquainted with the given context, <code>false</code> otherwise 047 */ 048 public boolean isRelatedWithContext(Set<String> setOfContexts, String contextToCheck) 049 { 050 return setOfContexts != null && setOfContexts.contains(contextToCheck); 051 } 052 053 /** 054 * Check if a set of contexts is acquainted with the given contexts 055 * @param setOfContexts The set of contexts 056 * @param contextsToCheck The contexts to check 057 * @return <code>true</code> if the set is acquainted with all the given contexts, <code>false</code> otherwise 058 */ 059 public boolean isRelatedWithAllContexts(Set<String> setOfContexts, Set<String> contextsToCheck) 060 { 061 return setOfContexts != null 062 && contextsToCheck != null 063 // True if the set is acquainted with every contexts to check 064 && contextsToCheck.stream() 065 .allMatch(contextToCheck -> isRelatedWithContext(setOfContexts, contextToCheck)); 066 } 067}