org.eclipse.jdt.internal.core.dom.rewrite.imports.ReorderingImportAdder Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2015 Google Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* 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;
}
}