fix
[fedora-idea.git] / java / compiler / impl / src / com / intellij / packaging / impl / artifacts / PackagingElementPath.java
blob9552836bc767c2357f08ff09a9a6cd7e2aefe203
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.packaging.impl.artifacts;
18 import com.intellij.packaging.elements.ComplexPackagingElement;
19 import com.intellij.packaging.elements.CompositePackagingElement;
20 import com.intellij.packaging.elements.PackagingElement;
21 import com.intellij.util.SmartList;
22 import com.intellij.util.StringBuilderSpinAllocator;
23 import org.jetbrains.annotations.NotNull;
24 import org.jetbrains.annotations.Nullable;
26 import java.util.List;
28 /**
29 * @author nik
31 public class PackagingElementPath {
32 public static final PackagingElementPath EMPTY = new PackagingElementPath(null, null);
33 private final PackagingElementPath myParentPath;
34 private final PackagingElement<?> myLastElement;
36 private PackagingElementPath(PackagingElementPath parentPath, PackagingElement<?> lastElement) {
37 myParentPath = parentPath;
38 myLastElement = lastElement;
41 public PackagingElementPath appendComplex(ComplexPackagingElement<?> element) {
42 return new PackagingElementPath(this, element);
45 public PackagingElementPath appendComposite(CompositePackagingElement<?> element) {
46 return new PackagingElementPath(this, element);
49 @Nullable
50 public PackagingElementPath getParentPath() {
51 return myParentPath;
54 public PackagingElement<?> getLastElement() {
55 return myLastElement;
58 @NotNull
59 public String getPathString() {
60 return getPathString("/");
63 @NotNull
64 public String getPathString(String separator) {
65 final StringBuilder builder = StringBuilderSpinAllocator.alloc();
66 try {
67 final List<CompositePackagingElement<?>> parents = getParents();
68 for (int i = parents.size() - 1; i >= 0; i--) {
69 builder.append(parents.get(i).getName());
70 if (i > 0) {
71 builder.append(separator);
74 return builder.toString();
76 finally {
77 StringBuilderSpinAllocator.dispose(builder);
81 public List<CompositePackagingElement<?>> getParents() {
82 List<CompositePackagingElement<?>> result = new SmartList<CompositePackagingElement<?>>();
83 PackagingElementPath path = this;
84 while (path != EMPTY) {
85 if (path.myLastElement instanceof CompositePackagingElement<?>) {
86 result.add((CompositePackagingElement)path.myLastElement);
88 path = path.myParentPath;
90 return result;
93 public List<PackagingElement<?>> getAllElements() {
94 List<PackagingElement<?>> result = new SmartList<PackagingElement<?>>();
95 PackagingElementPath path = this;
96 while (path != EMPTY) {
97 result.add(path.myLastElement);
98 path = path.myParentPath;
100 return result;
103 @Nullable
104 public CompositePackagingElement<?> getLastParent() {
105 PackagingElementPath path = this;
106 while (path != EMPTY) {
107 if (path.myLastElement instanceof CompositePackagingElement<?>) {
108 return (CompositePackagingElement)path.myLastElement;
110 path = path.myParentPath;
112 return null;
115 public boolean isEmpty() {
116 return myParentPath == null;