org.eclipse.jdt.internal.core.dom.rewrite.imports.ReorderingImportAdder Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2015 Google Inc and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* John Glassmyer - import group sorting is broken - https://bugs.eclipse.org/430303
*******************************************************************************/
package org.eclipse.jdt.internal.core.dom.rewrite.imports;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Totally sorts new and existing imports together, discarding the order of existing imports
* and omitting duplicate entries.
*/
final class ReorderingImportAdder implements ImportAdder {
private final Comparator importComparator;
ReorderingImportAdder(Comparator importComparator) {
this.importComparator = importComparator;
}
@Override
public List addImports(Collection existingImports, Collection importsToAdd) {
int setCapacity = 2 * (existingImports.size() + importsToAdd.size());
Set uniqueImportsWithAdditions = new HashSet<>(setCapacity);
uniqueImportsWithAdditions.addAll(existingImports);
uniqueImportsWithAdditions.addAll(importsToAdd);
List sortedImports = new ArrayList<>(uniqueImportsWithAdditions);
Collections.sort(sortedImports, this.importComparator);
return sortedImports;
}
}