diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/utils/NamespaceSortingUtils.java b/js/js.translator/src/org/jetbrains/k2js/translate/utils/NamespaceSortingUtils.java new file mode 100644 index 00000000000..e9d4290f551 --- /dev/null +++ b/js/js.translator/src/org/jetbrains/k2js/translate/utils/NamespaceSortingUtils.java @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.k2js.translate.utils; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor; +import org.jetbrains.jet.lang.descriptors.NamespaceDescriptorParent; + +import java.util.List; + +/** + * @author Pavel Talanov + */ +public final class NamespaceSortingUtils { + + private NamespaceSortingUtils() { + } + + @NotNull + public static List sortNamespacesUsingQualificationOrder + (@NotNull List namespaceDescriptors) { + PartiallyOrderedSet namespaceDescriptorPartiallyOrderedSet + = new PartiallyOrderedSet(namespaceDescriptors, qualificationOrder()); + return namespaceDescriptorPartiallyOrderedSet.partiallySortedElements(); + } + + @NotNull + private static PartiallyOrderedSet.Order qualificationOrder() { + return new PartiallyOrderedSet.Order() { + @Override + public boolean firstDependsOnSecond(@NotNull NamespaceDescriptor first, @NotNull NamespaceDescriptor second) { + NamespaceDescriptorParent containingDeclaration = first.getContainingDeclaration(); + if (containingDeclaration == null) { + return false; + } + return containingDeclaration.equals(second); + } + }; + } +}