001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.util.graph.transformer; 020 021import java.util.Collection; 022 023import org.eclipse.aether.RepositoryException; 024import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictContext; 025import org.eclipse.aether.util.graph.transformer.ConflictResolver.ConflictItem; 026import org.eclipse.aether.util.graph.transformer.ConflictResolver.OptionalitySelector; 027 028/** 029 * An optionality selector for use with {@link ConflictResolver}. In general, this selector only marks a dependency as 030 * optional if all its occurrences are optional. If however a direct dependency is involved, its optional flag is 031 * selected. 032 */ 033public final class SimpleOptionalitySelector extends OptionalitySelector { 034 035 /** 036 * Creates a new instance of this optionality selector. 037 */ 038 public SimpleOptionalitySelector() {} 039 040 @Override 041 public void selectOptionality(ConflictContext context) throws RepositoryException { 042 boolean optional = chooseEffectiveOptionality(context.getItems()); 043 context.setOptional(optional); 044 } 045 046 private boolean chooseEffectiveOptionality(Collection<ConflictItem> items) { 047 boolean optional = true; 048 for (ConflictItem item : items) { 049 if (item.getDepth() <= 1) { 050 return item.getDependency().isOptional(); 051 } 052 if ((item.getOptionalities() & ConflictItem.OPTIONAL_FALSE) != 0) { 053 optional = false; 054 } 055 } 056 return optional; 057 } 058}