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