001/** 002 * Copyright (C) 2006-2024 Talend Inc. - www.talend.com 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.talend.sdk.component.runtime.manager.xbean; 017 018import static lombok.AccessLevel.PRIVATE; 019 020import java.util.Collection; 021import java.util.stream.Stream; 022 023import org.apache.xbean.finder.filter.Filter; 024import org.apache.xbean.finder.filter.FilterList; 025import org.apache.xbean.finder.filter.Filters; 026import org.apache.xbean.finder.filter.PrefixFilter; 027 028import lombok.NoArgsConstructor; 029 030// "Filters" but xbean already took it 031@NoArgsConstructor(access = PRIVATE) 032public class FilterFactory { 033 034 public static Filter and(final Filter first, final Filter second) { 035 if (Stream 036 .of(first, second) 037 .anyMatch(f -> !FilterList.class.isInstance(f) 038 || !FilterList.class.cast(f).getFilters().stream().allMatch(PrefixFilter.class::isInstance))) { 039 throw new IllegalArgumentException("And only works with filter list of prefix filters"); // for optims 040 } 041 final FilterList list1 = FilterList.class.cast(first); 042 final FilterList list2 = FilterList.class.cast(second); 043 return Filters 044 .prefixes(Stream 045 .of(list1.getFilters(), list2.getFilters()) 046 .flatMap(Collection::stream) 047 .map(PrefixFilter.class::cast) 048 .map(PrefixFilter::getPrefix) 049 .distinct() 050 .toArray(String[]::new)); 051 } 052}